Skip to content

Commit

Permalink
Merge pull request #26 from samullen/master
Browse files Browse the repository at this point in the history
Add @fallback_to_any to handle unexpected types
jarednorman authored May 7, 2019
2 parents e9915c9 + a0905ce commit fab0c7f
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/canada/can.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defprotocol Canada.Can do
@fallback_to_any true

@doc "Evaluates permissions"
def can?(subject, action, resource)
end
12 changes: 12 additions & 0 deletions test/canada_test.exs
Original file line number Diff line number Diff line change
@@ -16,6 +16,10 @@ defimpl Canada.Can, for: User do
def can?(%User{verified: verified}, :create, Post), do: verified
end

defimpl Canada.Can, for: Any do
def can?(_subject, _action, _resource), do: false
end

defmodule CanadaTest do
use ExUnit.Case
import Canada, only: [can?: 2]
@@ -55,4 +59,12 @@ defmodule CanadaTest do
assert user() |> can?(create(Post))
refute other_user() |> can?(create(Post))
end

describe "authorizing 'Any' other resource" do
test "accepts any other resource" do
refute nil |> can?(touch(Post))
refute "" |> can?(touch(Post))
refute %{} |> can?(touch(Post))
end
end
end

0 comments on commit fab0c7f

Please sign in to comment.