【问题标题】:Guards for module plugs?模块插头的保护装置?
【发布时间】:2016-02-29 14:18:34
【问题描述】:

请原谅新手的问题,但我发现了很多像这样保护 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

【问题讨论】:

    标签: elixir phoenix-framework


    【解决方案1】:

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

    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]
    

    【讨论】:

    • 这在我的路由器管道中给了我相同的编译错误,但在控制器中有效。任何想法为什么?使用凤凰 1.3.0
    • @Johannes 我遇到了同样的问题。你找到解决办法了吗?
    • 如果您查看源代码,Phoenix.Controller.Pipeline 被编程为编译防护(如Plug.Builder)但Phoenix.Router 不是。
    猜你喜欢
    • 2012-01-31
    • 2011-07-09
    • 2015-11-10
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 2015-02-19
    • 1970-01-01
    相关资源
    最近更新 更多