Drupal investigation

UserAccessControlHandler.php 5.1KB

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