Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / shortcut / src / ShortcutSetStorage.php
1 <?php
2
3 namespace Drupal\shortcut;
4
5 use Drupal\Component\Uuid\UuidInterface;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Config\Entity\ConfigEntityStorage;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Extension\ModuleHandlerInterface;
10 use Drupal\Core\Language\LanguageManagerInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Defines a storage for shortcut_set entities.
16  */
17 class ShortcutSetStorage extends ConfigEntityStorage implements ShortcutSetStorageInterface {
18
19   /**
20    * The module handler.
21    *
22    * @var \Drupal\Core\Extension\ModuleHandlerInterface
23    */
24   protected $moduleHandler;
25
26   /**
27    * Constructs a ShortcutSetStorageController object.
28    *
29    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_info
30    *   The entity info for the entity type.
31    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
32    *   The config factory service.
33    * @param \Drupal\Component\Uuid\UuidInterface $uuid_service
34    *   The UUID service.
35    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
36    *   The module handler.
37    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
38    *   The language manager.
39    */
40   public function __construct(EntityTypeInterface $entity_info, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, ModuleHandlerInterface $module_handler, LanguageManagerInterface $language_manager) {
41     parent::__construct($entity_info, $config_factory, $uuid_service, $language_manager);
42
43     $this->moduleHandler = $module_handler;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_info) {
50     return new static(
51       $entity_info,
52       $container->get('config.factory'),
53       $container->get('uuid'),
54       $container->get('module_handler'),
55       $container->get('language_manager')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function deleteAssignedShortcutSets(ShortcutSetInterface $entity) {
63     // First, delete any user assignments for this set, so that each of these
64     // users will go back to using whatever default set applies.
65     db_delete('shortcut_set_users')
66       ->condition('set_name', $entity->id())
67       ->execute();
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function assignUser(ShortcutSetInterface $shortcut_set, $account) {
74     db_merge('shortcut_set_users')
75       ->key('uid', $account->id())
76       ->fields(['set_name' => $shortcut_set->id()])
77       ->execute();
78     drupal_static_reset('shortcut_current_displayed_set');
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function unassignUser($account) {
85     $deleted = db_delete('shortcut_set_users')
86       ->condition('uid', $account->id())
87       ->execute();
88     return (bool) $deleted;
89   }
90
91   /**
92    * {@inheritdoc}
93    */
94   public function getAssignedToUser($account) {
95     $query = db_select('shortcut_set_users', 'ssu');
96     $query->fields('ssu', ['set_name']);
97     $query->condition('ssu.uid', $account->id());
98     return $query->execute()->fetchField();
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function countAssignedUsers(ShortcutSetInterface $shortcut_set) {
105     return db_query('SELECT COUNT(*) FROM {shortcut_set_users} WHERE set_name = :name', [':name' => $shortcut_set->id()])->fetchField();
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function getDefaultSet(AccountInterface $account) {
112     // Allow modules to return a default shortcut set name. Since we can only
113     // have one, we allow the last module which returns a valid result to take
114     // precedence. If no module returns a valid set, fall back on the site-wide
115     // default, which is the lowest-numbered shortcut set.
116     $suggestions = array_reverse($this->moduleHandler->invokeAll('shortcut_default_set', [$account]));
117     $suggestions[] = 'default';
118     $shortcut_set = NULL;
119     foreach ($suggestions as $name) {
120       if ($shortcut_set = $this->load($name)) {
121         break;
122       }
123     }
124
125     return $shortcut_set;
126   }
127
128 }