2017-04-20 51 views
0

我在我的凤凰应用中使用canary libphoenix canary not_found handler

我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 

我abilities.ex:

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 

而且unauthorized_handler工作正常。但是not_found_handler不起作用。

[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. 

如果我添加到我的abilities.ex此功能:

def can?(user, action, nil) do 
    IO.puts "check ability for nil" 
    false 
    end 

如果未找到资源,我会叫handle_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 
+0

你能发布完整的错误信息吗?它还应该印出主题,行动和资源的实际价值,这将有助于使问题更清楚。 – Dogbert

+0

@Dogbert,加了 –

+1

自从我使用加那利以来,它已经有一段时间了。我建议你尝试返回一个无资源的'true'。我相信这会允许找不到的代码运行。查看[Canary Plugs]的实现(https://github.com/cpjk/canary/blob/master/lib/canary/plugs.ex)获取更多信息。 –

回答

1

如果为can?(_, _, nil)功能的语句返回true,这将允许授权传递和handle_not_found插件正确设置conn