Issue #1479454 by Hugo Wetterberg, galooph, dawehner, andypost, marcingy, heyrocker...
[project/drupal.git] / core / modules / user / lib / Drupal / user / RoleStorageController.php
1 <?php
2
3 /**
4 * @file
5 * Contains Drupal\user\RoleStorageController.
6 */
7
8 namespace Drupal\user;
9
10 use Drupal\Core\Config\Entity\ConfigStorageController;
11 use Drupal\Core\Entity\EntityInterface;
12
13 /**
14 * Controller class for user roles.
15 */
16 class RoleStorageController extends ConfigStorageController {
17
18 /**
19 * Overrides ConfigStorageController::preSave().
20 */
21 public function preSave(EntityInterface $entity) {
22 if (!isset($entity->weight) && $roles = entity_load_multiple('user_role')) {
23 // Set a role weight to make this new role last.
24 $max = array_reduce($roles, function($max, $entity) {
25 return $max > $entity->weight ? $max : $entity->weight;
26 });
27 $entity->weight = $max + 1;
28 }
29 parent::preSave($entity);
30 }
31
32 /**
33 * Overrides ConfigStorageController::resetCache().
34 */
35 public function resetCache(array $ids = NULL) {
36 parent::resetCache($ids);
37
38 // Clear the user access cache.
39 drupal_static_reset('user_access');
40 drupal_static_reset('user_role_permissions');
41 }
42
43 /**
44 * Overrides ConfigStorageController::postDelete().
45 */
46 protected function postDelete($entities) {
47 $rids = array_keys($entities);
48
49 // Delete permission assignments.
50 db_delete('role_permission')
51 ->condition('rid', $rids)
52 ->execute();
53 // Remove the role from all users.
54 db_delete('users_roles')
55 ->condition('rid', $rids)
56 ->execute();
57 }
58
59 /**
60 * Overrides ConfigStorageController::attachLoad().
61 */
62 protected function attachLoad(&$queried_entities, $revision_id = FALSE) {
63 // Sort the queried roles by their weight.
64 uasort($queried_entities, 'Drupal\Core\Config\Entity\ConfigEntityBase::sort');
65
66 parent::attachLoad($queried_entities, $revision_id);
67 }
68
69 }