Elixir 凤凰金丝雀没有找到

Elixir 凤凰金丝雀没有找到,elixir,phoenix-framework,Elixir,Phoenix Framework,我在我的phoenix应用程序中使用 我在config.exs中写道: config :canary, repo: MyApp.Repo, unauthorized_handler: {MyApp.Helpers, :handle_unauthorized}, not_found_handler: {MyApp.Helpers, :handle_not_found} 并创建了帮助程序: defmodule MyApp.Helpers do use Phoenix.C

我在我的phoenix应用程序中使用

我在config.exs中写道:

config :canary, repo: MyApp.Repo,
      unauthorized_handler: {MyApp.Helpers, :handle_unauthorized},
      not_found_handler: {MyApp.Helpers, :handle_not_found}
并创建了帮助程序:

defmodule MyApp.Helpers do
  use Phoenix.Controller

  require Logger
  import Plug.Conn

  def handle_unauthorized(conn) do
    conn
    |> put_status(401)
    |> put_view(MyApp.ErrorView)
    |> render("401.json")
    |> halt
  end

  def handle_not_found(conn) do
    conn
    |> put_status(404)
    |> put_view(MyApp.ErrorView)
    |> render("404.json")
    |> halt
  end
end
我的能力。例如:

defimpl Canada.Can, for: MyApp.User do
  alias MyApp.Operation
  alias MyApp.User

  def can?(user, action, operation = %Operation{})  when action in [:show, :update, :delete] do
    IO.puts "check ability for operation"
    if operation.user_id == user.id do
      IO.puts "success"
      true
    else
      false
    end
  end

  def can?(subject, action, resource) do
    raise """
    Unimplemented authorization check for User!  To fix see below...

    Please implement `can?` for User in #{__ENV__.file}.

    The function should match:

    subject:  #{inspect subject}

    action:   #{action}

    resource: #{inspect resource}
    """
  end
end
未经授权的处理程序工作正常。但未找到\u处理程序不工作

[error] #PID<0.709.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /api/v1/operations/11
** (exit) an exception was raised:
    ** (RuntimeError) Unimplemented authorization check for User!  To fix see below...

Please implement `can?` for User in /home/mars/phoenix_projects/my_app/lib/abilities.ex.
如果找不到资源,我将被称为handle\u unauthorized

如果我离开就好了

def can?(user, action, operation = %Operation{}) when action in [:show, :update, :delete] do
那我就错了

(FunctionClauseError) no function clause matching in Canada.Can.MyApp.User.can?/3
我不明白如何为这种情况编写函数

Upd:完整错误消息:

[error] #PID<0.427.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /api/v1/operations/11
** (exit) an exception was raised:
    ** (RuntimeError) Unimplemented authorization check for User!  To fix see below...

Please implement `can?` for User in /home/mars/phoenix_projects/my_app/lib/abilities.ex.

The function should match:

subject:  %MyApp.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">, operations: [%MyApp.Operation{__meta__: #Ecto.Schema.Metadata<:loaded, "operations">, balance: 1.3e3, categories: #Ecto.Association.NotLoaded<association :categories is not loaded>, id: 1, inserted_at: ~N[2017-04-15 15:00:38.727984], name: "new name", updated_at: ~N[2017-04-15 15:45:08.860944], users: #Ecto.Association.NotLoaded<association :users is not loaded>}, %MyApp.Operation{__meta__: #Ecto.Schema.Metadata<:loaded, "operations">, balance: 0.0, categories: #Ecto.Association.NotLoaded<association :categories is not loaded>, id: 2, inserted_at: ~N[2017-04-15 15:00:59.396742], name: "test1", updated_at: ~N[2017-04-15 15:00:59.396747], users: #Ecto.Association.NotLoaded<association :users is not loaded>}, %MyApp.Operation{__meta__: #Ecto.Schema.Metadata<:loaded, "operations">, balance: 0.0, categories: #Ecto.Association.NotLoaded<association :categories is not loaded>, id: 3, inserted_at: ~N[2017-04-15 15:01:56.845078], name: "test0", updated_at: ~N[2017-04-15 15:01:56.845085], users: #Ecto.Association.NotLoaded<association :users is not loaded>}, %MyApp.Operation{__meta__: #Ecto.Schema.Metadata<:loaded, "operations">, balance: 0.0, categories: #Ecto.Association.NotLoaded<association :categories is not loaded>, id: 4, inserted_at: ~N[2017-04-15 15:04:25.647952], name: "testtt", updated_at: ~N[2017-04-15 15:04:25.647958], users: #Ecto.Association.NotLoaded<association :users is not loaded>}], confirmation_token: nil, confirmed: false, id: 1, inserted_at: ~N[2017-04-15 14:58:48.263515], login: "test user0", password: nil, password_confirmation: nil, password_hash: "$2b$12$8yt6WNBt5hyxZE1MqiZtX.YRuReOuy4zom1imQvK2Q.Dw6pc8iNSG", reset_password_token: nil, sessions: #Ecto.Association.NotLoaded<association :sessions is not loaded>, updated_at: ~N[2017-04-15 14:58:48.268564]}

action:   show

resource: nil

        (my_app) lib/abilities.ex:43: Canada.Can.MyApp.User.can?/3
        (canary) lib/canary/plugs.ex:214: Canary.Plugs.do_authorize_resource/2
        (canary) lib/canary/plugs.ex:187: Canary.Plugs.authorize_resource/2
        (canary) lib/canary/plugs.ex:274: Canary.Plugs.do_load_and_authorize_resource/2
        (my_app) web/controllers/v1/operation_controller.ex:1: MyApp.V1.OperationController.phoenix_controller_pipeline/2
        (my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.instrument/4
        (my_app) lib/phoenix/router.ex:261: MyApp.Router.dispatch/2
        (my_app) web/router.ex:1: MyApp.Router.do_call/2
        (my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.phoenix_pipeline/1
        (my_app) lib/plug/debugger.ex:123: MyApp.Endpoint."call (overridable 3)"/2
        (my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /home/mars/phoenix_projects/my_app/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
[错误]#运行MyApp的PID。终结点已终止
服务器:本地主机:4000(http)
请求:GET/api/v1/operations/11
**(退出)引发了一个异常:
**(RuntimeError)未对用户执行授权检查!要修复此问题,请参见下文。。。
请为/home/mars/phoenix_projects/my_app/lib/abilities.ex中的用户实现'can?'。
功能应匹配:
主题:%MyApp.User{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu.Schema.Metadata,余额:1.3e3,类别:{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu.eto.Association.NotLoaded,id:1,插入地址:~N[2017-04-15:00:38.727984],名称:“新名称”,更新地址:~N[2017-04-15:15:45:08.944],用户:{uuuuuuuuuuuuuuuuuuuuuuuu{uuuuu meta_uuuuu:#exto.Schema.Metadata,余额:0.0,类别:#exto.Association.NotLoaded,id:2,插入地址:~N[2017-04-15 15 15:00:59.396742],名称:“test1”,更新地址:~N[2017-04-15:00:59.396747],用户:#exto.Association.NotLoaded},%MyApp.Operation{{uuuuuuu meta_uuuuuuu:#exto.Schema.Metadata,余额:0.0,类别:#exto.Association.NotLoaded,id:3,插入地址:~N[2017-04-15 15 15:01:56.845078],名称:“test0”,更新地址:~N[2017-04-15:01:56.845085],用户:#exto.Association.NotLoaded},%MyApp.Operation{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu.meta.Schema.Metadata,余额:0.0,类别:uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu“测试用户0”,密码:nil,密码确认:nil,密码散列:“$2b$12$8yt6WNBt5hyxZE1MqiZtX.YRuReOuy4zom1imQvK2Q.Dw6pc8iNSG”,重置密码\u令牌:nil,会话:#Ecto.Association.NotLoaded,更新地址:~N[2017-04-15 14:58:48.268564]]
行动:表演
资源:无
(我的应用程序)lib/abilities.ex:43:Canada.Can.MyApp.User.Can?/3
(canary)lib/canary/plugs.ex:214:canary.plugs.do\u authorize\u resource/2
(canary)lib/canary/plugs.ex:187:canary.plugs.authorize_资源/2
(canary)lib/canary/plugs.ex:274:canary.plugs.do_load_和_authorize_资源/2
(my_app)web/controllers/v1/operation_controller.ex:1:MyApp.v1.OperationController.phoenix_controller_管道/2
(my_app)lib/my_app/endpoint.ex:1:MyApp.endpoint.instrument/4
(我的应用程序)lib/phoenix/router.ex:261:MyApp.router.dispatch/2
(my_应用程序)web/router.ex:1:MyApp.router.do_call/2
(my_app)lib/my_app/endpoint.ex:1:MyApp.endpoint.phoenix_管道/1
(my_app)lib/plug/debugger.ex:123:MyApp.Endpoint.“调用(可重写3)”/2
(my_app)lib/my_app/endpoint.ex:1:MyApp.endpoint.call/2
(plug)lib/plug/adapters/cowboy/handler.ex:15:plug.adapters.cowboy.handler.upgrade/4
(牛仔)/home/mars/phoenix_projects/my_app/deps/cowboy/src/cowboy_protocol.erl:442::cowboy_protocol.execute/4

如果您为
can返回true(,,nil)
function子句,这将允许通过授权和句柄未找到插头来正确设置
conn

您可以发布完整的错误消息吗?它还应该打印主题、操作和资源的实际值,这将有助于使这个问题更清楚。@Dogbert,我使用Canary已经有一段时间了。我建议您尝试返回
true
以获取nil资源。我相信这将允许未找到的代码运行。有关详细信息,请查看的工具。@StevePallen,谢谢,它可以工作!
[error] #PID<0.427.0> running MyApp.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /api/v1/operations/11
** (exit) an exception was raised:
    ** (RuntimeError) Unimplemented authorization check for User!  To fix see below...

Please implement `can?` for User in /home/mars/phoenix_projects/my_app/lib/abilities.ex.

The function should match:

subject:  %MyApp.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">, operations: [%MyApp.Operation{__meta__: #Ecto.Schema.Metadata<:loaded, "operations">, balance: 1.3e3, categories: #Ecto.Association.NotLoaded<association :categories is not loaded>, id: 1, inserted_at: ~N[2017-04-15 15:00:38.727984], name: "new name", updated_at: ~N[2017-04-15 15:45:08.860944], users: #Ecto.Association.NotLoaded<association :users is not loaded>}, %MyApp.Operation{__meta__: #Ecto.Schema.Metadata<:loaded, "operations">, balance: 0.0, categories: #Ecto.Association.NotLoaded<association :categories is not loaded>, id: 2, inserted_at: ~N[2017-04-15 15:00:59.396742], name: "test1", updated_at: ~N[2017-04-15 15:00:59.396747], users: #Ecto.Association.NotLoaded<association :users is not loaded>}, %MyApp.Operation{__meta__: #Ecto.Schema.Metadata<:loaded, "operations">, balance: 0.0, categories: #Ecto.Association.NotLoaded<association :categories is not loaded>, id: 3, inserted_at: ~N[2017-04-15 15:01:56.845078], name: "test0", updated_at: ~N[2017-04-15 15:01:56.845085], users: #Ecto.Association.NotLoaded<association :users is not loaded>}, %MyApp.Operation{__meta__: #Ecto.Schema.Metadata<:loaded, "operations">, balance: 0.0, categories: #Ecto.Association.NotLoaded<association :categories is not loaded>, id: 4, inserted_at: ~N[2017-04-15 15:04:25.647952], name: "testtt", updated_at: ~N[2017-04-15 15:04:25.647958], users: #Ecto.Association.NotLoaded<association :users is not loaded>}], confirmation_token: nil, confirmed: false, id: 1, inserted_at: ~N[2017-04-15 14:58:48.263515], login: "test user0", password: nil, password_confirmation: nil, password_hash: "$2b$12$8yt6WNBt5hyxZE1MqiZtX.YRuReOuy4zom1imQvK2Q.Dw6pc8iNSG", reset_password_token: nil, sessions: #Ecto.Association.NotLoaded<association :sessions is not loaded>, updated_at: ~N[2017-04-15 14:58:48.268564]}

action:   show

resource: nil

        (my_app) lib/abilities.ex:43: Canada.Can.MyApp.User.can?/3
        (canary) lib/canary/plugs.ex:214: Canary.Plugs.do_authorize_resource/2
        (canary) lib/canary/plugs.ex:187: Canary.Plugs.authorize_resource/2
        (canary) lib/canary/plugs.ex:274: Canary.Plugs.do_load_and_authorize_resource/2
        (my_app) web/controllers/v1/operation_controller.ex:1: MyApp.V1.OperationController.phoenix_controller_pipeline/2
        (my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.instrument/4
        (my_app) lib/phoenix/router.ex:261: MyApp.Router.dispatch/2
        (my_app) web/router.ex:1: MyApp.Router.do_call/2
        (my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.phoenix_pipeline/1
        (my_app) lib/plug/debugger.ex:123: MyApp.Endpoint."call (overridable 3)"/2
        (my_app) lib/my_app/endpoint.ex:1: MyApp.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) /home/mars/phoenix_projects/my_app/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4