annotate core/modules/user/src/UserAccessControlHandler.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\user;
Chris@0 4
Chris@0 5 use Drupal\Core\Access\AccessResult;
Chris@0 6 use Drupal\Core\Access\AccessResultNeutral;
Chris@0 7 use Drupal\Core\Entity\EntityInterface;
Chris@0 8 use Drupal\Core\Entity\EntityAccessControlHandler;
Chris@0 9 use Drupal\Core\Field\FieldDefinitionInterface;
Chris@0 10 use Drupal\Core\Field\FieldItemListInterface;
Chris@0 11 use Drupal\Core\Session\AccountInterface;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Defines the access control handler for the user entity type.
Chris@0 15 *
Chris@0 16 * @see \Drupal\user\Entity\User
Chris@0 17 */
Chris@0 18 class UserAccessControlHandler extends EntityAccessControlHandler {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Allow access to user label.
Chris@0 22 *
Chris@0 23 * @var bool
Chris@0 24 */
Chris@0 25 protected $viewLabelOperation = TRUE;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * {@inheritdoc}
Chris@0 29 */
Chris@0 30 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
Chris@0 31 /** @var \Drupal\user\UserInterface $entity*/
Chris@0 32
Chris@0 33 // We don't treat the user label as privileged information, so this check
Chris@0 34 // has to be the first one in order to allow labels for all users to be
Chris@0 35 // viewed, including the special anonymous user.
Chris@0 36 if ($operation === 'view label') {
Chris@0 37 return AccessResult::allowed();
Chris@0 38 }
Chris@0 39
Chris@0 40 // The anonymous user's profile can neither be viewed, updated nor deleted.
Chris@0 41 if ($entity->isAnonymous()) {
Chris@0 42 return AccessResult::forbidden();
Chris@0 43 }
Chris@0 44
Chris@0 45 // Administrators can view/update/delete all user profiles.
Chris@0 46 if ($account->hasPermission('administer users')) {
Chris@0 47 return AccessResult::allowed()->cachePerPermissions();
Chris@0 48 }
Chris@0 49
Chris@0 50 switch ($operation) {
Chris@0 51 case 'view':
Chris@0 52 // Only allow view access if the account is active.
Chris@0 53 if ($account->hasPermission('access user profiles') && $entity->isActive()) {
Chris@0 54 return AccessResult::allowed()->cachePerPermissions()->addCacheableDependency($entity);
Chris@0 55 }
Chris@0 56 // Users can view own profiles at all times.
Chris@0 57 elseif ($account->id() == $entity->id()) {
Chris@0 58 return AccessResult::allowed()->cachePerUser();
Chris@0 59 }
Chris@0 60 else {
Chris@0 61 return AccessResultNeutral::neutral("The 'access user profiles' permission is required and the user must be active.");
Chris@0 62 }
Chris@0 63 break;
Chris@0 64
Chris@0 65 case 'update':
Chris@0 66 // Users can always edit their own account.
Chris@0 67 return AccessResult::allowedIf($account->id() == $entity->id())->cachePerUser();
Chris@0 68
Chris@0 69 case 'delete':
Chris@0 70 // Users with 'cancel account' permission can cancel their own account.
Chris@0 71 return AccessResult::allowedIf($account->id() == $entity->id() && $account->hasPermission('cancel account'))->cachePerPermissions()->cachePerUser();
Chris@0 72 }
Chris@0 73
Chris@0 74 // No opinion.
Chris@0 75 return AccessResult::neutral();
Chris@0 76 }
Chris@0 77
Chris@0 78 /**
Chris@0 79 * {@inheritdoc}
Chris@0 80 */
Chris@0 81 protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
Chris@0 82 // Fields that are not implicitly allowed to administrative users.
Chris@0 83 $explicit_check_fields = [
Chris@0 84 'pass',
Chris@0 85 ];
Chris@0 86
Chris@0 87 // Administrative users are allowed to edit and view all fields.
Chris@0 88 if (!in_array($field_definition->getName(), $explicit_check_fields) && $account->hasPermission('administer users')) {
Chris@0 89 return AccessResult::allowed()->cachePerPermissions();
Chris@0 90 }
Chris@0 91
Chris@0 92 // Flag to indicate if this user entity is the own user account.
Chris@0 93 $is_own_account = $items ? $items->getEntity()->id() == $account->id() : FALSE;
Chris@0 94 switch ($field_definition->getName()) {
Chris@0 95 case 'name':
Chris@0 96 // Allow view access to anyone with access to the entity. Anonymous
Chris@0 97 // users should be able to access the username field during the
Chris@0 98 // registration process, otherwise the username and email constraints
Chris@0 99 // are not checked.
Chris@0 100 if ($operation == 'view' || ($items && $account->isAnonymous() && $items->getEntity()->isAnonymous())) {
Chris@0 101 return AccessResult::allowed()->cachePerPermissions();
Chris@0 102 }
Chris@0 103 // Allow edit access for the own user name if the permission is
Chris@0 104 // satisfied.
Chris@0 105 if ($is_own_account && $account->hasPermission('change own username')) {
Chris@0 106 return AccessResult::allowed()->cachePerPermissions()->cachePerUser();
Chris@0 107 }
Chris@0 108 else {
Chris@0 109 return AccessResult::forbidden();
Chris@0 110 }
Chris@0 111
Chris@0 112 case 'preferred_langcode':
Chris@0 113 case 'preferred_admin_langcode':
Chris@0 114 case 'timezone':
Chris@0 115 case 'mail':
Chris@0 116 // Allow view access to own mail address and other personalization
Chris@0 117 // settings.
Chris@0 118 if ($operation == 'view') {
Chris@0 119 return $is_own_account ? AccessResult::allowed()->cachePerUser() : AccessResult::forbidden();
Chris@0 120 }
Chris@0 121 // Anyone that can edit the user can also edit this field.
Chris@0 122 return AccessResult::allowed()->cachePerPermissions();
Chris@0 123
Chris@0 124 case 'pass':
Chris@0 125 // Allow editing the password, but not viewing it.
Chris@0 126 return ($operation == 'edit') ? AccessResult::allowed() : AccessResult::forbidden();
Chris@0 127
Chris@0 128 case 'created':
Chris@0 129 // Allow viewing the created date, but not editing it.
Chris@0 130 return ($operation == 'view') ? AccessResult::allowed() : AccessResult::forbidden();
Chris@0 131
Chris@0 132 case 'roles':
Chris@0 133 case 'status':
Chris@0 134 case 'access':
Chris@0 135 case 'login':
Chris@0 136 case 'init':
Chris@0 137 return AccessResult::forbidden();
Chris@0 138 }
Chris@0 139
Chris@0 140 return parent::checkFieldAccess($operation, $field_definition, $account, $items);
Chris@0 141 }
Chris@0 142
Chris@0 143 }