Skip to content

Commit 245ad2c

Browse files
author
Chris Job
committed
Support returning a resource from authorize
Applied coryodaniel's PR. Reference here: schrockwell#54
1 parent fad5855 commit 245ad2c

File tree

5 files changed

+10
-4
lines changed

5 files changed

+10
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ end
5151

5252
To implement a policy, add `@behaviour Bodyguard.Policy` to a context, then define `authorize(action, user, params)` callbacks, which must return:
5353

54-
* `:ok` or `true` to permit an action
54+
* `:ok`, `{:ok, resource}`, or true to permit an action
5555
* `:error`, `{:error, reason}`, or `false` to deny an action
5656

5757
Don't use these callbacks directly - instead, go through `Bodyguard.permit/4`. This will convert any keyword-list `params` into a map, and will coerce the callback result into a strict `:ok` or `{:error, reason}` result. The default failure `reason` is `:unauthorized` unless specified otherwise in the callback.

lib/bodyguard.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ defmodule Bodyguard do
172172
# Coerce auth results
173173
defp resolve_result(true), do: :ok
174174
defp resolve_result(:ok), do: :ok
175+
defp resolve_result({:ok, resource}), do: {:ok, resource}
175176
defp resolve_result(false), do: {:error, @default_error}
176177
defp resolve_result(:error), do: {:error, @default_error}
177178
defp resolve_result({:error, reason}), do: {:error, reason}

lib/bodyguard/policy.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Bodyguard.Policy do
1111
@behaviour Bodyguard.Policy
1212
1313
def authorize(action, user, params) do
14-
# Return :ok or true to permit
14+
# Return :ok {:ok, resource}, or true to permit
1515
# Return :error, {:error, reason}, or false to deny
1616
end
1717
end
@@ -37,12 +37,12 @@ defmodule Bodyguard.Policy do
3737
3838
"""
3939

40-
@type auth_result :: :ok | :error | {:error, reason :: any} | true | false
40+
@type auth_result :: :ok | {:ok, resource :: any} | :error | {:error, reason :: any} | true | false
4141

4242
@doc """
4343
Callback to authorize a user's action.
4444
45-
To permit an action, return `:ok` or `true`. To deny, return `:error`,
45+
To permit an action, return `:ok`, `{:ok, resource}`, or `true`. To deny, return `:error`,
4646
`{:error, reason}`, or `false`.
4747
4848
The `action` is whatever user-specified contextual action is being authorized.

test/bodyguard/policy_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule PolicyTest do
1414
test "authorizing via helper", %{context: context, user: user} do
1515
assert :ok = Bodyguard.permit(context, :action, user)
1616
assert :ok = Bodyguard.permit(context, :ok_boolean, user)
17+
assert {:ok, resource} = Bodyguard.permit(context, :ok_with_resource, user)
1718
assert {:error, :unauthorized} = Bodyguard.permit(context, :fail, user)
1819

1920
assert {:error, %{key: :value}} =

test/test_helper.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ defmodule TestContext do
3131
true
3232
end
3333

34+
def authorize(:ok_with_resource, _user, _params) do
35+
{:ok, %{id: 1, name: "foo"}}
36+
end
37+
3438
def authorize(:fail_boolean, _user, _params) do
3539
false
3640
end

0 commit comments

Comments
 (0)