模块插头的防护装置?

nee*_*zer 9 elixir phoenix-framework

请原谅新手问题,但我发现了许多保护功能插头的例子:

plug :assign_welcome_message, "Hi!" when action in [:index, :show]
Run Code Online (Sandbox Code Playgroud)

但我没有找到如何使用模块插件执行此操作的示例:

plug Guardian.Plug.EnsurePermissions,
  handler: Mp.Api.AuthController,
  admin: [:dashboard] when action in [:protected_action]
Run Code Online (Sandbox Code Playgroud)

我似乎移动的when action in [:protected_action]任何地方或者给我一个语法错误或一个未定义的函数when/2.我知道我做的事情很愚蠢,但我看不清楚!

救命!


phoenix 1.1.4

The*_*sor 18

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

插件有两个参数,第二个是选项的参数.在您的示例中,您希望将关键字列表作为该选项参数传递.

但是,如果关键字列表是函数中的最后一个参数,则只允许删除方括号语法糖.

代替

plug Guardian.Plug.EnsurePermissions,
  handler: Mp.Api.AuthController,
  admin: [:dashboard] when action in [:protected_action]
Run Code Online (Sandbox Code Playgroud)

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

plug Guardian.Plug.EnsurePermissions,
  [handler: Mp.Api.AuthController,
  admin: [:dashboard]] when action in [:protected_action]
Run Code Online (Sandbox Code Playgroud)