Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guards for module plugs?

Pardon the newbie question, but I've found plenty of examples of guarding function plugs like so:

plug :assign_welcome_message, "Hi!" when action in [:index, :show]

But I have found no examples of how to do this with module plugs:

plug Guardian.Plug.EnsurePermissions,
  handler: Mp.Api.AuthController,
  admin: [:dashboard] when action in [:protected_action]

Everywhere I seem to move when action in [:protected_action] either gives me a syntax error or an undefined function when/2. I know I'm doing something stupid but I can't see what!

Help!


phoenix 1.1.4

like image 433
neezer Avatar asked Feb 29 '16 05:02

neezer


1 Answers

Not stupid! Just a result of some syntactic sugar.

Plugs take two arguments, the second is an argument for options. In your example, you want to pass a keyword list as that options argument.

However, the syntactic sugar that lets your drop the square brackets only works if the keyword list is the last argument in the function.

Instead of

plug Guardian.Plug.EnsurePermissions,
  handler: Mp.Api.AuthController,
  admin: [:dashboard] when action in [:protected_action]

Try the explicit keyword list syntax:

plug Guardian.Plug.EnsurePermissions,
  [handler: Mp.Api.AuthController,
  admin: [:dashboard]] when action in [:protected_action]
like image 188
The Brofessor Avatar answered Nov 14 '22 04:11

The Brofessor