Permit.Phoenix.Actions (permit_phoenix v0.3.0)

View Source

Provides default action groupings and singular actions for Phoenix controllers and live views. This module is used as a default implementation that can be overridden in individual modules.

Also, allows inferring actions from a Phoenix router module for convenience, so that the actions module does not need to repeat action names already living in controllers.

Example:

defmodule MyApp.Router do
  # ...

  get("/items/:id", MyApp.ItemController, :view)
end

defmodule MyApp.Actions do
  # Merge the actions from the router into the default grouping schema.
  use Permit.Phoenix.Actions, router: MyApp.Router
end

defmodule MyApp.Permissions do
  # Use the actions module to define permissions.
  use Permit.Permissions, actions_module: MyApp.Actions

  def can(:admin = _role) do
    permit()
    |> all(Item)
  end

  # The `view` action is automatically added to the grouping schema
  # and hence available as a `view/2`function when defining permissions.
  def can(:owner = _role) do
    permit()
    |> view(Item)
    |> all(Item, fn user, item -> item.owner_id == user.id end)
  end

end

Summary

Functions

Convenience function defining the basic CRUD (create, read, update, delete) actions.

Convenience function returning actions that are singular in the most basic CRUD setup, in which case all of: :create, :read, :update and :delete are singular.

Returns the default action grouping schema for Phoenix applications.

Returns the list of actions that operate on a single resource.

Functions

actions_from_router(router_module)

crud_grouping()

Convenience function defining the basic CRUD (create, read, update, delete) actions.

crud_singular()

Convenience function returning actions that are singular in the most basic CRUD setup, in which case all of: :create, :read, :update and :delete are singular.

grouping_schema()

Returns the default action grouping schema for Phoenix applications.

merge_from_router(grouping_schema, router_module)

singular_actions()

Returns the list of actions that operate on a single resource.