class UserAccessControlHandler in Drupal 10
Same name and namespace in other branches
- 8 core/modules/user/src/UserAccessControlHandler.php \Drupal\user\UserAccessControlHandler
- 9 core/modules/user/src/UserAccessControlHandler.php \Drupal\user\UserAccessControlHandler
Defines the access control handler for the user entity type.
Hierarchy
- class \Drupal\Core\Entity\EntityHandlerBase uses DependencySerializationTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityAccessControlHandler implements EntityAccessControlHandlerInterface
- class \Drupal\user\UserAccessControlHandler
- class \Drupal\Core\Entity\EntityAccessControlHandler implements EntityAccessControlHandlerInterface
Expanded class hierarchy of UserAccessControlHandler
See also
1 file declares its use of UserAccessControlHandler
- UserAccessControlHandlerTest.php in core/
modules/ user/ tests/ src/ Unit/ UserAccessControlHandlerTest.php
File
- core/
modules/ user/ src/ UserAccessControlHandler.php, line 19
Namespace
Drupal\userView source
class UserAccessControlHandler extends EntityAccessControlHandler {
/**
* Allow access to user label.
*
* @var bool
*/
protected $viewLabelOperation = TRUE;
/**
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\user\UserInterface $entity*/
// We don't treat the user label as privileged information, so this check
// has to be the first one in order to allow labels for all users to be
// viewed, including the special anonymous user.
if ($operation === 'view label') {
return AccessResult::allowed();
}
// The anonymous user's profile can neither be viewed, updated nor deleted.
if ($entity
->isAnonymous()) {
return AccessResult::forbidden();
}
// Administrators can view/update/delete all user profiles.
if ($account
->hasPermission('administer users')) {
return AccessResult::allowed()
->cachePerPermissions();
}
switch ($operation) {
case 'view':
// Only allow view access if the account is active.
if ($account
->hasPermission('access user profiles') && $entity
->isActive()) {
return AccessResult::allowed()
->cachePerPermissions()
->addCacheableDependency($entity);
}
elseif ($account
->id() == $entity
->id()) {
return AccessResult::allowed()
->cachePerUser();
}
else {
return AccessResultNeutral::neutral("The 'access user profiles' permission is required and the user must be active.")
->cachePerPermissions()
->addCacheableDependency($entity);
}
break;
case 'update':
// Users can always edit their own account.
$access_result = AccessResult::allowedIf($account
->id() == $entity
->id())
->cachePerUser();
if (!$access_result
->isAllowed() && $access_result instanceof AccessResultReasonInterface) {
$access_result
->setReason("Users can only update their own account, unless they have the 'administer users' permission.");
}
return $access_result;
case 'delete':
// Users with 'cancel account' permission can cancel their own account.
return AccessResult::allowedIfHasPermission($account, 'cancel account')
->andIf(AccessResult::allowedIf($account
->id() == $entity
->id())
->cachePerUser());
}
// No opinion.
return AccessResult::neutral();
}
/**
* {@inheritdoc}
*/
protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
// Fields that are not implicitly allowed to administrative users.
$explicit_check_fields = [
'pass',
];
// Administrative users are allowed to edit and view all fields.
if (!in_array($field_definition
->getName(), $explicit_check_fields) && $account
->hasPermission('administer users')) {
return AccessResult::allowed()
->cachePerPermissions();
}
// Flag to indicate if this user entity is the own user account.
$is_own_account = $items ? $items
->getEntity()
->id() == $account
->id() : FALSE;
switch ($field_definition
->getName()) {
case 'name':
// Allow view access to anyone with access to the entity.
// The username field is editable during the registration process.
if ($operation == 'view' || $items && $items
->getEntity()
->isAnonymous()) {
return AccessResult::allowed()
->cachePerPermissions();
}
// Allow edit access for the own user name if the permission is
// satisfied.
if ($is_own_account && $account
->hasPermission('change own username')) {
return AccessResult::allowed()
->cachePerPermissions()
->cachePerUser();
}
else {
return AccessResult::neutral();
}
case 'mail':
// Only check for the 'view user email addresses' permission and a view
// operation. Use case fall-through for all other cases.
if ($operation == 'view' && $account
->hasPermission('view user email addresses')) {
return AccessResult::allowed()
->cachePerPermissions();
}
case 'preferred_langcode':
case 'preferred_admin_langcode':
case 'timezone':
// Allow view access to own mail address and other personalization
// settings.
if ($operation == 'view') {
return AccessResult::allowedIf($is_own_account)
->cachePerUser();
}
// Anyone that can edit the user can also edit this field.
return AccessResult::allowed()
->cachePerPermissions();
case 'pass':
// Allow editing the password, but not viewing it.
return $operation == 'edit' ? AccessResult::allowed() : AccessResult::forbidden();
case 'created':
// Allow viewing the created date, but not editing it.
return $operation == 'view' ? AccessResult::allowed() : AccessResult::neutral();
case 'roles':
case 'status':
case 'access':
case 'login':
case 'init':
return AccessResult::neutral();
}
return parent::checkFieldAccess($operation, $field_definition, $account, $items);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityAccessControlHandler:: |
protected | property | Stores calculated access check results. | |
EntityAccessControlHandler:: |
protected | property | Information about the entity type. | |
EntityAccessControlHandler:: |
protected | property | The entity type ID of the access control handler instance. | |
EntityAccessControlHandler:: |
public | function |
Checks access to an operation on a given entity or entity translation. Overrides EntityAccessControlHandlerInterface:: |
1 |
EntityAccessControlHandler:: |
protected | function | Performs create access checks. | 10 |
EntityAccessControlHandler:: |
public | function |
Checks access to create an entity. Overrides EntityAccessControlHandlerInterface:: |
1 |
EntityAccessControlHandler:: |
public | function |
Checks access to an operation on a given entity field. Overrides EntityAccessControlHandlerInterface:: |
|
EntityAccessControlHandler:: |
protected | function | Tries to retrieve a previously cached access value from the static cache. | |
EntityAccessControlHandler:: |
protected | function | Loads the current account object, if it does not exist yet. | |
EntityAccessControlHandler:: |
protected | function | We grant access to the entity if both of these conditions are met: | |
EntityAccessControlHandler:: |
public | function |
Clears all cached access checks. Overrides EntityAccessControlHandlerInterface:: |
|
EntityAccessControlHandler:: |
protected | function | Statically caches whether the given user has access. | |
EntityAccessControlHandler:: |
public | function | Constructs an access control handler instance. | 6 |
EntityHandlerBase:: |
protected | property | The module handler to invoke hooks on. | 5 |
EntityHandlerBase:: |
protected | function | Gets the module handler. | 5 |
EntityHandlerBase:: |
public | function | Sets the module handler for this handler. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 3 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UserAccessControlHandler:: |
protected | property |
Allow access to user label. Overrides EntityAccessControlHandler:: |
|
UserAccessControlHandler:: |
protected | function |
Performs access checks. Overrides EntityAccessControlHandler:: |
|
UserAccessControlHandler:: |
protected | function |
Default field access as determined by this access control handler. Overrides EntityAccessControlHandler:: |