Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / user / tests / modules / user_access_test / user_access_test.module
1 <?php
2
3 /**
4  * @file
5  * Dummy module implementing hook_user_access() to test if entity access is respected.
6  */
7
8 use Drupal\Core\Access\AccessResult;
9 use Drupal\Core\Field\FieldDefinitionInterface;
10 use Drupal\Core\Field\FieldItemListInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Drupal\user\Entity\User;
13
14 /**
15  * Implements hook_ENTITY_TYPE_access() for entity type "user".
16  */
17 function user_access_test_user_access(User $entity, $operation, $account) {
18   if ($entity->getUsername() == "no_edit" && $operation == "update") {
19     // Deny edit access.
20     return AccessResult::forbidden();
21   }
22   if ($entity->getUsername() == "no_delete" && $operation == "delete") {
23     // Deny delete access.
24     return AccessResult::forbidden();
25   }
26   return AccessResult::neutral();
27 }
28
29 /**
30  * Implements hook_entity_field_access().
31  */
32 function user_access_test_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
33   // Account with role sub-admin can view the status, init and mail fields for user with no roles.
34   if ($operation === 'view' && in_array($field_definition->getName(), ['status', 'init', 'mail'])) {
35     if (($items == NULL) || (count($items->getEntity()->getRoles()) == 1)) {
36       return AccessResult::allowedIfHasPermission($account, 'sub-admin');
37     }
38   }
39   return AccessResult::neutral();
40 }