模块插头保护装置?

Guards for module plugs?

请原谅新手问题,但我发现了很多保护 function 插件的例子,例如:

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

但是我没有找到关于如何使用 module 插件执行此操作的示例:

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

我似乎到处移动 when action in [:protected_action] 要么给我一个语法错误,要么给我一个未定义的函数 when/2。我知道我在做一些愚蠢的事情,但我看不到什么!

求助!


phoenix 1.1.4

不傻!只是一些语法糖的结果。

Plugs take two arguments,第二个是选项的参数。在您的示例中,您希望将关键字列表作为该选项参数传递。

但是,syntactic sugar that lets your drop the square brackets 只有在关键字列表是函数中的最后一个参数时才有效。

而不是

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

尝试显式关键字列表语法:

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