Datacop (datacop v0.1.2)
An authorization library with Dataloader
and Absinthe
support.
Link to this section Summary
Types
Any term which describes an action you want to perform. Usually atoms are used.
Usually your user struct
Option
Link to this section Types
Link to this type
action()
Specs
action() :: term()
Any term which describes an action you want to perform. Usually atoms are used.
Link to this type
actor()
Specs
actor() :: term()
Usually your user struct
Link to this type
option()
Specs
option() :: {:subject, any()} | {:loader, Dataloader.t()}
Option
:subject
– any value you want to access in the authorize/3 callback.:loader
– an initialized Dataloader struct with loaded sources.
Link to this section Functions
Link to this function
default_loader(module)
Returns initialized dataloader struct with the source.
It requires data/0
function to be defined for the particular module.
Link to this function
permit(module, action, actor, opts \\ [])
Specs
permit(policy :: module(), action(), actor(), opts :: [option()]) :: :ok | {:error, Datacop.UnauthorizedError.t()}
Authorize an action.
Processes Datacop.Policy.authorize/3
result.
Examples
> Datacop.permit(MyApp.Accounts, :view_email, current_user, subject: other_user)
:ok
> Datacop.permit(MyApp.Accounts, :view_email, current_user, subject: other_user, loader: loader)
{:error, %Datacop.UnauthorizedError{message: "Unauthorized"}}
Link to this function
permit?(module, action, actor, opts \\ [])
Specs
The same as permit/4
, but returns a boolean
.