cpjk / canary

:hatching_chick: Elixir authorization and resource-loading library for Plug applications.
MIT License
474 stars 51 forks source link

Extend list of actions that require model name resource #43

Closed jmufugi closed 7 years ago

jmufugi commented 8 years ago

Can you consider custom-extending list of actions that require model name in Canada.Can.can? implementation. (List is currently hard-coded to [:index, :new, :create]). For authorize_resource plug, and perhaps for other plugs, this can be defined by inserting additional option, say require_model: [:custom_action1, :custom_action2, ...]

Then you can modify authorize_resource/2 function in plugs.ex. As follows round about line 195:

`
require_model_list = if opts[:require_model], do: ([:index, :new, :create] ++ opts[:require_model]), else: [:index, :new, :create] resource = cond do is_persisted -> fetch_resource(conn, opts)

action in [:index, :new, :create] ->

  action in require_model_list ->
    opts[:model]
  true ->
    fetch_resource(conn, opts)
end

`

handle_not_found/2 may also need to be amended.

cpjk commented 8 years ago

@jmufugi Good point, and I like your solution. Do you want to submit a PR, and I will review it? :smiley:

jmufugi commented 8 years ago

Have submitted PR. rgds