function UserAccessTestHooks::userAccess

Implements hook_ENTITY_TYPE_access() for entity type "user".

File

core/modules/user/tests/modules/user_access_test/src/Hook/UserAccessTestHooks.php, line 22

Class

UserAccessTestHooks
Hook implementations for user_access_test.

Namespace

Drupal\user_access_test\Hook

Code

public function userAccess(User $entity, $operation, $account) {
    if ($entity->getAccountName() == "no_edit" && $operation == "update") {
        // Deny edit access.
        return AccessResult::forbidden();
    }
    if ($entity->getAccountName() == "no_delete" && $operation == "delete") {
        // Deny delete access.
        return AccessResult::forbidden();
    }
    // Account with role sub-admin can manage users with no roles.
    if (count($entity->getRoles()) == 1) {
        return AccessResult::allowedIfHasPermission($account, 'sub-admin');
    }
    return AccessResult::neutral();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.