Roles

Roles are groups of users that define what the users has access to via role permissions. Roles can also be used as an inclusive test like i.e. "Does this user have the foo role?".

Role Fields

Below is a list of fields in the roles stream. Fields are accessed as attributes:

$role->slug;

Same goes for decorated instances in Twig:

{{ role.slug }}
Fields
Key Type Description

name

text

The name of the role.

slug

slug

The slug used for API access.

description

textarea

A description for the role.

permissions

textarea

A serialized array of role permissions.

Role Interface

This section will go over the features of the \Anomaly\UsersModule\Role\Contract\RoleInterface class.

RoleInterface::hasPermission()

The hasPermission method verifies that the role has the permission.

Returns: boolean
Arguments
Key Required Type Default Description

$permission

true

string

none

The permission string.

Example
if ($role->hasPermission('vendor.module.example::example.test')) {
    // Do something
}
Twig
{% if role.hasPermission('vendor.module.example::example.test') %}
    {# Do something #}
{% endif %}
RoleInterface::hasAnyPermission()

The hasAnyPermission method verifies that the role has at least one of the given permissions.

Returns: boolean
Arguments
Key Required Type Default Description

$permissions

true

array

none

The array of permissions.

Example
$hasPermission = $role->hasAnyPermission(
    ['vendor.module.example::example.test', 'vendor.module.example::widget.example']
);

if ($hasPermission) {
    // Do something
}
Twig
{% set hasPermission = role.hasAnyPermission(
    ['vendor.module.example::example.test', 'vendor.module.example::widget.example']
) %}

{% if hasPermission %}
    {# Do something #}
{% endif %}

Role Repository

The \Anomaly\UsersModule\Role\Contract\RoleRepositoryInterface class helps you retrieve roles from the database.

RoleRepositoryInterface::allButAdmin()

The allButAdmin method returns all roles but the admin one.

Returns: \Anomaly\UsersModule\Role\RoleCollection
Example
$roles = $repository->allButAdmin();
RoleRepositoryInterface::findBySlug()

The findBySlug method returns a role by it's slug.

Returns: \Anomaly\UsersModule\Role\Contract\RoleInterface or null
Arguments
Key Required Type Default Description

$slug

true

string

none

The slug of the role.

Example
$guest = $repository->findBySlug('guest');
RoleRepositoryInterface::findByPermission()

The findByPermission method returns all roles with the permission.

Returns: \Anomaly\UsersModule\Role\RoleCollection
Arguments
Key Required Type Default Description

$permission

true

string

none

The permission string.

Example
$roles = $repository->findByPermission('example.module.test::example.test');

// Search for partial-match permissions.
$roles = $repository->findByPermission('example.module.test::*');
RoleRepositoryInterface::updatePermissions()

The updatePermissions method updates the permissions for a role.

Returns: \Anomaly\UsersModule\Role\Contract\RoleInterface
Arguments
Key Required Type Default Description

$role

true

object

none

The role instance.

$permissions

true

array

none

The array of role permissions.

Example
$repository->updatePermissions(
    $role,
    [
        'example.module.test::example.test',
        'example.module.test::example.foo'
    ]
);