VoileWeb.Auth.Authorization (Voile v0.1.26)

Copy Markdown View Source

Authorization module for checking user permissions in the Voile application.

Summary

Functions

Check if user is authenticated (from socket or conn assigns).

Authorize a user for a permission or raise an error.

Authorize that at least one permission in the list is granted for the given subject (user, conn, or socket) or raise UnauthorizedError.

Returns true if any of the given permission names are granted for the provided subject (user, conn, or socket).

Check if a user has a specific permission.

Get the current user from socket or conn assigns.

Explicitly deny a permission for a user (overrides role permissions).

Get all permissions for a user (including role and direct permissions).

Grant a direct permission to a user.

Check if a user has the super_admin role.

Remove a role assignment from a user.

Functions

assign_role(user_id, role_id, opts \\ [])

Assign a role to a user.

Options

  • :scope_type - "global", "collection", or "item" (default: "global")
  • :scope_id - ID of the scoped resource (required for collection/item scope)
  • :glam_type - "Gallery", "Library", "Archive", or "Museum" (optional, for GLAM curator roles)
  • :assigned_by_id - ID of the user assigning the role
  • :expires_at - DateTime when the role assignment expires

Examples

# Assign librarian role for Library collections only
assign_role(user_id, librarian_role_id, glam_type: "Library")

# Assign role to specific collection
assign_role(user_id, role_id, scope_type: "collection", scope_id: collection_id)

authenticated?(socket_or_conn)

Check if user is authenticated (from socket or conn assigns).

Examples

authenticated?(socket)
authenticated?(conn)

authorize!(user_or_socket_or_conn, permission_name, opts \\ [])

Authorize a user for a permission or raise an error.

Examples

iex> authorize!(user, "collections.create")
:ok

iex> authorize!(user, "collections.delete")
** (Glam.Authorization.UnauthorizedError) User does not have permission: collections.delete

authorize_any!(user_or_conn_or_socket, permission_names)

Authorize that at least one permission in the list is granted for the given subject (user, conn, or socket) or raise UnauthorizedError.

Example:

Authorization.authorize_any!(conn, ["metadata.manage", "metadata.edit"])

authorize_any?(user_or_conn_or_socket, permission_names)

Returns true if any of the given permission names are granted for the provided subject (user, conn, or socket).

can?(user_or_id, permission_name, opts \\ [])

Check if a user has a specific permission.

Examples

# Check global permission
iex> can?(user, "collections.create")
true

# Check scoped permission for a collection
iex> can?(user, "items.update", scope: {:collection, 5})
true

# Check scoped permission for an item
iex> can?(user, "items.delete", scope: {:item, 123})
false

current_user(socket_or_conn)

Get the current user from socket or conn assigns.

Returns nil if no user is authenticated.

Examples

user = current_user(socket)
user = current_user(conn)

deny_permission(user_id, permission_id, opts \\ [])

Explicitly deny a permission for a user (overrides role permissions).

get_user_permissions(user_id, opts \\ [])

Get all permissions for a user (including role and direct permissions).

grant_permission(user_id, permission_id, opts \\ [])

Grant a direct permission to a user.

is_node_admin?(user)

is_super_admin?(user_or_socket_or_conn)

Check if a user has the super_admin role.

Super admins have unrestricted access to all resources across all nodes/units.

Examples

iex> is_super_admin?(user)
true

iex> is_super_admin?(socket)
false

revoke_role(user_id, role_id, opts \\ [])

Remove a role assignment from a user.

verify_api_token(token)