Search documentation of phoenix_kit

# PhoenixKit.Users.Role (phoenix_kit v1.6.19)

View Source

Role schema for PhoenixKit authorization system.

This schema defines user roles that can be assigned to users for authorization purposes.

## Fields

- `name`: Role name (unique, required for identification)
- `description`: Human-readable description of the role
- `is_system_role`: Whether this is a system-defined role that shouldn't be deleted

## System Roles

PhoenixKit includes three built-in system roles:

- Owner: System owner with full access (assigned to first user automatically)
- Admin: Administrator with elevated privileges
- User: Standard user with basic access (default for new users)

## Security Features

- System roles cannot be deleted
- Role names are unique
- Automatic assignment of User role to new registrations
- Automatic assignment of Owner role to first user

# Summary

## Types

t()

## Functions

changeset(role, attrs)

A role changeset for creating and updating roles.

system_role?(role_name)

Checks if a role name is a system role.

system_roles()

Returns the map of system role names.

# Types

# t()

    @type t() :: %PhoenixKit.Users.Role{
      __meta__: term(),
      description: String.t() | nil,
      id: integer() | nil,
      inserted_at: NaiveDateTime.t(),
      is_system_role: boolean(),
      name: String.t(),
      role_assignments: term(),
      updated_at: NaiveDateTime.t(),
      users: term()
    }

# Functions

# changeset(role, attrs)

A role changeset for creating and updating roles.

## Parameters

- `role`: The role struct to modify
- `attrs`: Attributes to update

## Examples

    iex> changeset(%Role{}, %{name: "Manager", description: "Department manager"})
    %Ecto.Changeset{valid?: true}

    iex> changeset(%Role{}, %{name: ""})
    %Ecto.Changeset{valid?: false}

# system_role?(role_name)

Checks if a role name is a system role.

## Examples

    iex> system_role?(system_roles().owner)
    true

    iex> system_role?("Owner")
    true

    iex> system_role?("Manager")
    false

# system_roles()

Returns the map of system role names.

## Examples

    iex> system_roles()
    %{owner: "Owner", admin: "Admin", user: "User"}