Roles
A role is a name your code grants a user, like a member of a project. Access rules ask for one with to:, as Access Control shows. This page covers declaring roles, granting and revoking them, and who may do that.
The examples use the entity types from Queries, where a project has an owner and tasks, and a task has a project and an optional assignee.
Entity Roles
An entity type declares its roles with role, and its rules can ask for them:
defmodule MyApp.Project do
use Hologram.Entity
# ...the fields from Queries
role :member
allow :read, to: :member
allow :update, to: :member, when: archived == false
end
A role is held on one entity, so a member of one project is a member of that project alone. Code grants it with Hologram.Auth.grant_role, which takes the user entity or a user ID, the role and the entity:
Hologram.Auth.grant_role(user, :member, project)
The user may then read the project, and change it while it isn't archived. A role name belongs to its entity type, so two entity types can each have a :member role, and they're two different roles. Role names are kept apart from field names, so a role can share its name with a field.
A grant is an entity of the Hologram.Auth.RoleGrant type, with one for each role a user holds somewhere. A query reads grants like any other entities, as Reading Grants below shows, but only grant_role and revoke_role write them.
Extending Roles
A role can extend others with extends:, which takes a role or a list of them. A user who holds it counts as holding each role it extends:
role :member
role :manager, extends: :member
allow :read, to: :member
allow :update, to: :member, when: archived == false
allow :delete, to: :manager, when: none(tasks)
A manager is a member too, so a manager may read and change the project as a member may, and only a manager may delete it. A manager grant is enough, with no member grant beside it.
A role declared in a policy and again in the entity type is one role. Its extends: lists add up, and granted_to:, below, comes from the last declaration that has it. So a policy can say that a manager is a member, and an entity type that takes the policy can add role :manager, granted_to: :creator without repeating extends:.
Creator Grants
granted_to: :creator grants a role to the user who creates the entity, in the same write:
role :manager, extends: :member, granted_to: :creator
allow :create, when: owner_id == user_id()
A user who creates a project becomes its manager, so they can read it, change it and grant roles on it right away. Without the grant the project would have no members, and its creator couldn't even read it. A create in an action grants the role in the browser too, so the creator's live reads show the new project at once.
The creator gets the role even when the entity type lets nobody grant roles, since the declaration grants it, not the creator. Grant Gates below covers the lines that say who may grant roles. Code with no signed-in user, like a seed, creates the entity with no grant, since there's nobody to give the role to.
Type-Wide Roles
A role can be held on every entity of a type, like an auditor who may read every project:
role :auditor
allow :read, to: :auditor
Granting the role on the entity type instead of one entity makes it type-wide:
Hologram.Auth.grant_role(user, :auditor, MyApp.Project)
A type-wide role counts on each entity of the type, so to: :auditor holds for every project, the ones created later included. A rule on another entity type asks for it as {role, entity type}, like to: {:auditor, MyApp.Project} on an invoice. Only trusted code can grant or revoke a type-wide role, as Granting and Revoking below explains.
Global Roles
A global role is held app-wide, on no entity. It's a module that uses Hologram.Role, and it can extend other global roles:
defmodule MyApp.Support do
use Hologram.Role
end
defmodule MyApp.Admin do
use Hologram.Role, extends: MyApp.Support
end
The module is the role's name everywhere, in a rule like allow :read, to: MyApp.Admin and in the grant, which takes no entity:
Hologram.Auth.grant_role(user, MyApp.Admin)
An admin is a support user too, so a rule for MyApp.Support lets admins in. A global role is held by a user, so a role module fails the build when the app has no user entity type, the one marked with user: true in Entities. As with a type-wide role, only trusted code can grant or revoke a global role.
Granting and Revoking
grant_role and revoke_role take the user entity or a user ID, then the role, then an entity, or an entity type for a type-wide role, or nothing for a global role. They return :ok, or raise a Hologram.AccessDeniedError when the user acting may not do it. Granting a role the user already holds keeps the grant as it is, and revoking one they don't hold does nothing.
They work in actions and in server code, and they act for the signed-in user, as writes do in Writing Data. An action can grant a role to the user someone picks:
defmodule MyApp.MemberPicker do
use Hologram.Component
alias Hologram.Auth
alias MyApp.Project
alias MyApp.User
prop :project, Project
prop :users, [User]
def template do
~HOLO"""
{%for user <- @users}
<button $click={:invite, user_id: user.id, project: @project}>
Invite {user.name}
</button>
{/for}
"""
end
def action(:invite, %{user_id: user_id, project: project}, component) do
Auth.grant_role(user_id, :member, project)
component
end
end
In an action the grant goes into the action's batch, as a write does, so a list of the project's grants shows it at once. The server checks it again, and if it refuses the batch, the browser undoes the grant with the rest of it.
A user may always revoke their own role, which is how a member leaves a project:
def action(:leave, %{user: user, project: project}, component) do
Auth.revoke_role(user, :member, project)
component
end
Sometimes the server should grant a role that the signed-in user's rules don't let them grant. A user who opens an invite link isn't a manager, so they can't make themselves a member. Once a command has checked the link's token, it passes trust: true, and the grant skips the rules. The option works the same for type-wide and global roles:
Hologram.Auth.grant_role(user, :member, project, trust: true)
Hologram.Auth.grant_role(user, MyApp.Admin, :global, trust: true)
The option goes only in the fourth argument, so a command that passes the browser's params as the entity can't have them claim trust. Browser code acts for the signed-in user, so it can't claim trust at all. These grants record no granter, since the server gave them.
Trusted code is server code that claims trust, and code that runs outside a session, like seeds, Mix tasks, the console and your own plugs, which grants and revokes anything without the option. In a session, a visitor can't grant or revoke anything, and a type-wide or global role needs trust: true.
Grant Gates
An entity type's :grant_role and :revoke_role lines say who may grant and revoke its roles. Holding a role doesn't let a user grant it, and with no lines, only trusted code can. A line can name the role it covers, written as {:grant_role, role}:
allow {:grant_role, :member}, to: :manager
A manager may now make someone a member. Nobody may make someone a manager, since no line covers :manager, and a member may grant nothing, since the line asks for a manager.
A :grant_role line without a role in it covers the role its holders hold and every role that role extends:
allow :grant_role, to: :manager
allow :revoke_role, to: :manager
With these lines, a manager may grant and revoke both :manager and :member. A line written this way never lets anyone grant a role above their own.
A global role can hold a gate too. It isn't one of the entity type's roles, so no extends chain limits it, and Hologram ranks it above all of them:
allow :grant_role, to: MyApp.Admin
An admin may grant every role a project declares, with no role on the project itself. To let admins grant only some roles, name them, like allow {:grant_role, :member}, to: MyApp.Admin.
A {:grant_role, role} line isn't limited this way, since it says exactly who may grant what. allow {:grant_role, :manager}, to: :member lets members make managers. Without it, Hologram refuses that grant, and its error names the line to add if you meant it. :revoke_role lines take the same forms. A gate's answer holds until the grant lands, so a revoke of the role it rests on can't slip in between.
The gates don't stop a project from losing its last manager. That's up to your app, and a global role or trusted code can still grant roles on a project nobody manages.
can? asks a gate about one role, so a template can show the picker only to the users who may invite:
def template do
~HOLO"""
{%if can?(@current_user, {:grant_role, :member}, @project)}
<MyApp.MemberPicker project={@project} users={@users} />
{/if}
"""
end
Reading Grants
A query reads grants like any other entities, from the Hologram.Auth.RoleGrant type. A grant has these fields:
user_idis the user who holds the role.entity_typeis the entity type the role is held on, ornilfor a global role.entity_idis the entity the role is held on, ornilfor a type-wide or global role.roleis the role's name, or the module of a global role.granted_by_idis the signed-in user who granted the role or created the entity, ornilwhen trusted code granted it.
A user can always read their own grants. To let a user read other people's grants on an entity, give its type a :read_roles line:
allow :read_roles, to: :member
Now a project's members can see who else is a member. The users its gates let grant or revoke roles can read its grants too, since nobody should change a list they can't see. A live read lists the grants on one project:
derived :grants, [RoleGrant], fn project_id ->
RoleGrant
|> filter(entity_type == MyApp.Project and entity_id in [^project_id, nil])
|> sort(created_at)
end
The nil beside the ID matters. A type-wide grant has no entity ID, and can? counts it on every project, so a filter on the ID alone would leave its holders out of the list. The entity type keeps out global grants, whose entity type is nil too.
Each grant names its holder by user_id, and include(user) loads the holders' user entities. Those reads follow the user entity type's own rules, so a holder the reader may not read comes back as nil. The user entity type in Entities lets each user read only themselves, so with it, every grant but the reader's own has a nil user.
Grants change only through grant_role and revoke_role, and creating one with DB.create or changing one with DB.update raises a Hologram.MisuseError. A user who holds two roles on an entity has two grants, and to change a user's role, you revoke one and grant the other.
has_role? Is Not a Permission Check
has_role? answers whether a user holds a role. Pages and components import it, and it takes the user entity, a user ID or nil for a visitor, then the role, then the entity, or nothing for a global role:
def template do
~HOLO"""
{%if has_role?(@current_user, :manager, @project)}
<span class="badge">Manager</span>
{/if}
"""
end
A role counts as every role it extends, so a manager holds :member too, and a type-wide grant counts on each entity of its type. Given the entity type instead of an entity, as in has_role?(user, :auditor, MyApp.Project), it asks about the type-wide grant alone. In a template it answers the same in the browser as on the server, since the server sends the grants the answer needs with the page.
Use it to show who someone is, like a badge, a leave button or whether they've joined. To decide whether someone may do something, ask can?. has_role? doesn't read the rules, so a delete button shown to managers keeps showing to managers alone after you let admins delete projects too. can?(user, :delete, project) follows the rules as they change.