Skip to content

Instantly share code, notes, and snippets.

@feymartynov
Created November 24, 2018 01:10
Show Gist options
  • Save feymartynov/fa1103cf36f3e3826553e1a7ab9b1bfa to your computer and use it in GitHub Desktop.
Save feymartynov/fa1103cf36f3e3826553e1a7ab9b1bfa to your computer and use it in GitHub Desktop.
ExOperation article: DSL extension
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