Users

Users are extensible stream entries that can be associated with multiples roles. Users have their own permissions that merge with those inherited from the roles they belong to.

User Fields

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

$user->email;

Same goes for decorated instances in Twig:

{{ user.email }}
Fields
Key Type Description

email

email

The login email address.

username

text

The login username.

password

text

The hashed login password.

roles

multiple relationship

The roles the user has.

display_name

text

The publicly displayable name.

first_name

text

The real first name.

last_name

text

The real last name.

permissions

textarea

The serialized user permission array.

last_login_at

datetime

The last login datetime.

last_activity_at

text

The datetime for the last action made by the user.

ip_address

text

The last IP address that accessed the user account.

Custom User Fields

Custom user fields assigned through the control panel are assigned directly to the users stream and can be accessed directly from the user object.

$user->favorite_color;

And in Twig:

{{ user.favorite_color }}

User Interface

This section will go over the features of the \Anomaly\UsersModule\User\Contract\UserInterface class.

UserInterface::hasRole()

The hasRole method ensures that the user has the given role.

Returns: boolean
Arguments
Key Required Type Default Description

$role

true

string

none

The role ID, slug, or interface.

Example
if (auth()->user()->hasRole('admin') {
    echo "User is an admin!";
}
Twig
{% if auth_user().hasRole('admin') %}
    User is an admin!
{% endif %}
UserInterface::hasAnyRole()

The hasAnyRole method ensures that the user has at least one of the provided roles.

Returns: boolean
Arguments
Key Required Type Default Description

$roles

true

mixed

none

An array of role IDs or slugs. A collection of roles can also be passed.

Example
if (auth()->user()->hasAnyRole(['admin', 'manager'])) {
    echo 'Hello ' . $user->display_name;
}
Twig
{% if auth().user().hasAnyRole(['admin', 'manager'])) %}
    Hello {{ auth_user().display_name }}
{% endif %}
UserInterface::isAdmin()

The isAdmin method returns if the user has the admin role or not.

Returns: boolean
Example
if ($user->isAdmin()) {
    echo "Hi Admin.";
}
Twig
Hello {{ auth_user().isAdmin() ? 'admin' : 'user' }}
UserInterface::hasPermission()

The hasPermission method verifies that the user has the permission.

Returns: boolean
Arguments
Key Required Type Default Description

$permission

true

string

none

The permission string.

$checkRoles

false

boolean

true

Check the users roles for the permission too.

Example
if (auth()->user()->hasPermission('vendor.module.example::example.test')) {
    // So something
}
Twig
{% if auth_user().hasPermission('vendor.module.example::example.test')) %}
    {# So something #}
{% endif %}
UserInterface::hasAnyPermission()

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

Returns: boolean
Arguments
Key Required Type Default Description

$permissions

true

array

none

The array of permissions.

$checkRoles

false

boolean

true

Check the users roles for the permission too.

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

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

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

User Presenter

This section will go over the \Anomaly\UsersModule\User\UserPresenter class that's returned in the view layer.

UserPresenter::name()

The name method returns the concatenated first and last name.

Returns: string
Example
$decorated->name();
Twig
Hi {{ user().name() }}
UserPresenter::gravatar()

The gravatar method returns a Gravatar image URL for the user.

Returns: string
Arguments
Key Required Type Default Description

$parameters

true

array

none

Gravatar URL parameters.

Example
$decorated->avatar(['d' => 'mm']);
Twig
{{ img(user().gravatar({'d': 'mm'})).class('img-rounded')|raw }}

User Repository

The \Anomaly\UsersModule\User\Contract\UserRepositoryInterface class helps you retrieve users from the database.

UserRepositoryInterface::findByEmail()

The findByEmail method finds a user by their email.

Returns: \Anomaly\UsersModule\User\Contract\UserInterface or null
Arguments
Key Required Type Default Description

$email

true

string

none

The users email.

Example
$user = $repository->findByEmail('[email protected]');
UserRepositoryInterface::findByUsername()

The findByUsername method finds a user by their username.

Returns: \Anomaly\UsersModule\User\Contract\UserInterface or null
Arguments
Key Required Type Default Description

$username

true

string

none

The username of the user.

Example
$user = $repository->findByUsername('ryanthepyro');
UserRepositoryInterface::findByCredentials()

The findByCredentials method finds a user by their login field and password.

Returns: \Anomaly\UsersModule\User\Contract\UserInterface or null
Arguments
Key Required Type Default Description

$credentials

true

array

none

The credentials array containing email/username and password.

Example
$user = $repository->findByCredentials(['email' => '[email protected]', 'password' => 'secret password']);