CustomFieldValueCreateScope: use get_argument_or_attribute for member_id
- Read member_id via Ash.Changeset.get_argument_or_attribute/2 so it works when set as attribute or argument - Remove unused require Logger - Document member_id source in moduledoc
This commit is contained in:
parent
ba8c12f3ea
commit
5d82769d2d
1 changed files with 10 additions and 3 deletions
|
|
@ -8,6 +8,14 @@ defmodule Mv.Authorization.Checks.CustomFieldValueCreateScope do
|
||||||
(PermissionSets + :linked/:all) but only implements strict_check, so it
|
(PermissionSets + :linked/:all) but only implements strict_check, so it
|
||||||
never adds a filter.
|
never adds a filter.
|
||||||
|
|
||||||
|
## member_id source
|
||||||
|
|
||||||
|
The check reads `member_id` from the create changeset via
|
||||||
|
`Ash.Changeset.get_argument_or_attribute/2`, so it works when member_id
|
||||||
|
is set as an attribute or as an action argument. The CustomFieldValue
|
||||||
|
resource's default create action must accept and require `member_id`
|
||||||
|
(e.g. via `default_accept [:value, :member_id, :custom_field_id]`).
|
||||||
|
|
||||||
Used in CustomFieldValue policies:
|
Used in CustomFieldValue policies:
|
||||||
policy action_type(:create) do
|
policy action_type(:create) do
|
||||||
authorize_if Mv.Authorization.Checks.CustomFieldValueCreateScope
|
authorize_if Mv.Authorization.Checks.CustomFieldValueCreateScope
|
||||||
|
|
@ -15,7 +23,6 @@ defmodule Mv.Authorization.Checks.CustomFieldValueCreateScope do
|
||||||
"""
|
"""
|
||||||
use Ash.Policy.Check
|
use Ash.Policy.Check
|
||||||
alias Mv.Authorization.PermissionSets
|
alias Mv.Authorization.PermissionSets
|
||||||
require Logger
|
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def describe(_opts),
|
def describe(_opts),
|
||||||
|
|
@ -53,8 +60,8 @@ defmodule Mv.Authorization.Checks.CustomFieldValueCreateScope do
|
||||||
defp get_create_member_id(authorizer) do
|
defp get_create_member_id(authorizer) do
|
||||||
changeset = authorizer.changeset || authorizer.subject
|
changeset = authorizer.changeset || authorizer.subject
|
||||||
|
|
||||||
if changeset && function_exported?(Ash.Changeset, :get_attribute, 2) do
|
if changeset && function_exported?(Ash.Changeset, :get_argument_or_attribute, 2) do
|
||||||
Ash.Changeset.get_attribute(changeset, :member_id)
|
Ash.Changeset.get_argument_or_attribute(changeset, :member_id)
|
||||||
else
|
else
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue