Created
November 24, 2018 01:10
-
-
Save feymartynov/fa1103cf36f3e3826553e1a7ab9b1bfa to your computer and use it in GitHub Desktop.
ExOperation article: DSL extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def authorize(operation, txn_key, action, opts \\ []) do | |
user = opts[:user] || operation.context.user | |
ExOperation.Operation.step(operation, {:authorize, txn_key}, fn | |
%{^txn_key => struct} -> | |
policy = (opts[:policy] || struct.__struct__) |> Module.concat(Policy) | |
with :ok <- Bodyguard.authorize(policy, action, user, struct: struct) do | |
{:ok, :authorized} | |
end | |
_ -> | |
{:ok, :noop} | |
end) | |
end | |
operation | |
|> find(:entity, schema: MyApp.Entity) | |
|> authorize(:entity, :update) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment