Cando (cando v0.2.0)

View Source

An Elixir library for managing permissions.

Usage

Implement the Cando.Permission protocol for your user or subject structs to define custom permission logic.

defmodule MyApp.User do
  defstruct [:id, :role]

  defimpl Cando.Permission do
    def can?(user, _action), do: user.role == :admin
    def can?(_user, _action), do: false
  end
end

Then you can check permissions like this:

Cando.can?(%MyApp.User{id: 1, role: :admin), :edit_post)  # true
Cando.can?(%MyApp.User{id: 2, role: :guest), :edit_post)  # false

Summary

Functions

Raises a PermissionError if the subject does not have permission to perform the specified action.

Checks if the given subject has permission to perform the specified action.

Raises a PermissionError if the subject has permission to perform the specified action.

Checks if the given subject does not have permission to perform the specified action.

Functions

can!(subject, action)

Raises a PermissionError if the subject does not have permission to perform the specified action.

can?(subject, action)

Checks if the given subject has permission to perform the specified action.

cannot!(subject, action)

Raises a PermissionError if the subject has permission to perform the specified action.

cannot?(subject, action)

Checks if the given subject does not have permission to perform the specified action.