Skip to content

User API ​

Add HasAccess to the user model.

php
use Maxiviper117\Access\Concerns\HasAccess;

class User extends Authenticatable
{
    use HasAccess;
}

Scoped API ​

php
$user->in($company)->assignRole('Owner');
$user->in($company)->removeRole('Owner');
$user->in($company)->hasRole('Owner');
$user->in($company)->hasAnyRole(['Owner', 'Admin']);

$user->in($company)->can(Permission::UsersInvite);
$user->in($company)->cannot(Permission::RolesManage);
$user->in($company)->hasPermission(Permission::CompanyUpdate);

$user->in($company)->givePermission(Permission::UsersInvite);
$user->in($company)->revokePermission(Permission::UsersInvite);

in(Model $scope) ​

Returns an access context for the given scope.

php
$context = $user->in($company);

All scoped checks and assignments are made through that context.

Scoped contexts can use roles defined for that scope and shared non-global role definitions. Roles marked global are listed and assigned only through the global API.

Role Methods ​

assignRole(BackedEnum|string|Role $role) : Assigns a role in the current scope.

removeRole(BackedEnum|string|Role $role) : Removes a role assignment from the current scope.

hasRole(BackedEnum|string|Role $role) : Checks whether the actor has an existing role assignment in the current scope. Passing a Role model requires that the model already belongs to the active scope.

hasAnyRole(array $roles) : Checks whether any listed role is assigned in the current scope. The array can contain BackedEnum, string, or Role instances.

roles() : Lists roles available in the current context. A scoped context includes shared non-global roles and roles for its exact scope. A global context includes app-wide global roles.

Role checks are available, but permission checks are preferred in policies.

Permission Methods ​

can(BackedEnum|string $permission) : Checks direct and role-derived permissions in the current scope.

cannot(BackedEnum|string $permission) : Negates can(...).

hasPermission(BackedEnum|string $permission) : Alias for can(...).

givePermission(BackedEnum|string $permission) : Assigns a direct scoped permission.

revokePermission(BackedEnum|string $permission) : Removes a direct scoped permission.

Global API ​

php
$user->assignGlobalRole('Platform Admin');
$user->removeGlobalRole('Platform Admin');
$user->hasGlobalRole('Platform Admin');

$user->canGlobally(Permission::SystemManage);
$user->giveGlobalPermission(Permission::SystemManage);

Global role assignments have no scope. They are stored with null scope_type and scope_id.

Global Role Methods ​

assignGlobalRole(BackedEnum|string|Role $role) : Assigns an app-wide global role.

removeGlobalRole(BackedEnum|string|Role $role) : Removes an app-wide global role.

hasGlobalRole(BackedEnum|string|Role $role) : Checks whether the actor has an app-wide global role.

canGlobally(BackedEnum|string $permission) : Checks direct and role-derived permissions globally (outside any scope).

giveGlobalPermission(BackedEnum|string $permission) : Assigns a direct global permission.

revokeGlobalPermission(BackedEnum|string $permission) : Removes a direct global permission.

Permission Maps ​

php
$user->in($company)->toArray([
    Permission::UsersInvite,
    Permission::RolesManage,
]);

Output:

php
[
    'users.invite' => true,
    'roles.manage' => false,
]