Datacop (datacop v0.1.4)

An authorization library with Dataloader and Absinthe support.

Summary

Types

Any term which describes an action you want to perform. Usually atoms are used.

Usually your user struct

Option

Functions

Returns initialized dataloader struct with the source.

The same as permit/4, but returns a boolean.

Types

@type action() :: term()

Any term which describes an action you want to perform. Usually atoms are used.

@type actor() :: term()

Usually your user struct

@type 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.

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 \\ [])

@spec 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 \\ [])

@spec permit?(policy :: module(), action(), actor(), opts :: [option()]) :: boolean()

The same as permit/4, but returns a boolean.