View Source LiveGuard.Allowed protocol (Live Guard v0.1.8)

By this protocol you can implement allowed?/4 functions.

Summary

Types

t()

A user struct or nil when the user is not authenticated.

Functions

By this function you can protect the LiveView lifecycle stages.

Types

@type t() :: struct() | nil

A user struct or nil when the user is not authenticated.

Functions

Link to this function

allowed?(user, live_view_module, stage, stage_inputs)

View Source
@spec allowed?(
  user :: struct() | nil,
  live_view_module :: module(),
  stage :: :mount,
  stage_inputs ::
    {params :: Phoenix.LiveView.unsigned_params() | :not_mounted_at_router,
     session :: map(), socket :: Phoenix.LiveView.Socket.t()}
) :: boolean()
@spec allowed?(
  user :: struct() | nil,
  live_view_module :: module(),
  stage :: :handle_params,
  stage_inputs ::
    {unsigned_params :: Phoenix.LiveView.unsigned_params(), uri :: String.t(),
     socket :: Phoenix.LiveView.Socket.t()}
) :: boolean()
@spec allowed?(
  user :: struct() | nil,
  live_view_module :: module(),
  stage :: :handle_event,
  stage_inputs ::
    {event :: binary(), unsigned_params :: Phoenix.LiveView.unsigned_params(),
     socket :: Phoenix.LiveView.Socket.t()}
) :: boolean()
@spec allowed?(
  user :: struct() | nil,
  live_view_module :: module(),
  stage :: :handle_info,
  stage_inputs :: {msg :: term(), socket :: Phoenix.LiveView.Socket.t()}
) :: boolean()
@spec allowed?(
  user :: struct() | nil,
  live_view_module :: module(),
  stage :: :handle_async,
  stage_inputs ::
    {name :: atom(), async_fun_result :: {:ok | :exit, term()},
     socket :: Phoenix.LiveView.Socket.t()}
) :: boolean()

By this function you can protect the LiveView lifecycle stages.

You can pattern match by the user, LiveView module, LiveView lifecycle stage and LiveView lifecycle stage inputs. You can put this file anywhere but /lib/my_app_web/live/abilities.ex is recommended.

It must return a boolean.

Example

# /lib/my_app_web/live/abilities.ex

defimpl LiveGuard.Allowed, for: User do
  @before_compile {LiveGuard, :before_compile_allowed}

  def allowed?(
        %User{role: role},
        MyModuleLive,
        :handle_event,
        {"delete_item", _unsigned_params, _socket}
      )
      when role in [:viewer, :customer],
      do: false

  # other `allowed?/4` functions...
end

Note: As you can see, you don't have to define catch-all allowed?/4 function because we used @before_compile {LiveGuard, :before_compile_allowed} hook. It returns true. This is optional.

If the user is not authenticated you can add the following implementation as below:

defimpl LiveGuard.Allowed, for: Atom do
  @before_compile {LiveGuard, :before_compile_allowed}

  def allowed?(nil, MyModuleLive, :handle_event, {"delete_item", _unsigned_params, _socket}),
    do: false

  # other `allowed?/4` functions...
end