Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Session/UserSession.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\Session; | |
4 | |
5 /** | |
6 * An implementation of the user account interface for the global user. | |
7 * | |
8 * @todo: Change all properties to protected. | |
9 */ | |
10 class UserSession implements AccountInterface { | |
11 | |
12 /** | |
13 * User ID. | |
14 * | |
15 * @var int | |
16 */ | |
17 protected $uid = 0; | |
18 | |
19 /** | |
20 * List of the roles this user has. | |
21 * | |
22 * Defaults to the anonymous role. | |
23 * | |
24 * @var array | |
25 */ | |
26 protected $roles = [AccountInterface::ANONYMOUS_ROLE]; | |
27 | |
28 /** | |
29 * The Unix timestamp when the user last accessed the site. | |
30 * | |
31 * @var string. | |
32 */ | |
33 protected $access; | |
34 | |
35 /** | |
36 * The name of this account. | |
37 * | |
38 * @var string | |
39 */ | |
40 public $name = ''; | |
41 | |
42 /** | |
43 * The preferred language code of the account. | |
44 * | |
45 * @var string | |
46 */ | |
47 protected $preferred_langcode; | |
48 | |
49 /** | |
50 * The preferred administrative language code of the account. | |
51 * | |
52 * @var string | |
53 */ | |
54 protected $preferred_admin_langcode; | |
55 | |
56 /** | |
57 * The email address of this account. | |
58 * | |
59 * @var string | |
60 */ | |
61 protected $mail; | |
62 | |
63 /** | |
64 * The timezone of this account. | |
65 * | |
66 * @var string | |
67 */ | |
68 protected $timezone; | |
69 | |
70 /** | |
71 * Constructs a new user session. | |
72 * | |
73 * @param array $values | |
74 * Array of initial values for the user session. | |
75 */ | |
76 public function __construct(array $values = []) { | |
77 foreach ($values as $key => $value) { | |
78 $this->$key = $value; | |
79 } | |
80 } | |
81 | |
82 /** | |
83 * {@inheritdoc} | |
84 */ | |
85 public function id() { | |
86 return $this->uid; | |
87 } | |
88 | |
89 /** | |
90 * {@inheritdoc} | |
91 */ | |
92 public function getRoles($exclude_locked_roles = FALSE) { | |
93 $roles = $this->roles; | |
94 | |
95 if ($exclude_locked_roles) { | |
96 $roles = array_values(array_diff($roles, [AccountInterface::ANONYMOUS_ROLE, AccountInterface::AUTHENTICATED_ROLE])); | |
97 } | |
98 | |
99 return $roles; | |
100 } | |
101 | |
102 /** | |
103 * {@inheritdoc} | |
104 */ | |
105 public function hasPermission($permission) { | |
106 // User #1 has all privileges. | |
107 if ((int) $this->id() === 1) { | |
108 return TRUE; | |
109 } | |
110 | |
111 return $this->getRoleStorage()->isPermissionInRoles($permission, $this->getRoles()); | |
112 } | |
113 | |
114 /** | |
115 * {@inheritdoc} | |
116 */ | |
117 public function isAuthenticated() { | |
118 return $this->uid > 0; | |
119 } | |
120 | |
121 /** | |
122 * {@inheritdoc} | |
123 */ | |
124 public function isAnonymous() { | |
125 return $this->uid == 0; | |
126 } | |
127 | |
128 /** | |
129 * {@inheritdoc} | |
130 */ | |
131 public function getPreferredLangcode($fallback_to_default = TRUE) { | |
132 $language_list = \Drupal::languageManager()->getLanguages(); | |
133 if (!empty($this->preferred_langcode) && isset($language_list[$this->preferred_langcode])) { | |
134 return $language_list[$this->preferred_langcode]->getId(); | |
135 } | |
136 else { | |
137 return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()->getId() : ''; | |
138 } | |
139 } | |
140 | |
141 /** | |
142 * {@inheritdoc} | |
143 */ | |
144 public function getPreferredAdminLangcode($fallback_to_default = TRUE) { | |
145 $language_list = \Drupal::languageManager()->getLanguages(); | |
146 if (!empty($this->preferred_admin_langcode) && isset($language_list[$this->preferred_admin_langcode])) { | |
147 return $language_list[$this->preferred_admin_langcode]->getId(); | |
148 } | |
149 else { | |
150 return $fallback_to_default ? \Drupal::languageManager()->getDefaultLanguage()->getId() : ''; | |
151 } | |
152 } | |
153 | |
154 /** | |
155 * {@inheritdoc} | |
156 */ | |
157 public function getUsername() { | |
158 return $this->getAccountName(); | |
159 } | |
160 | |
161 /** | |
162 * {@inheritdoc} | |
163 */ | |
164 public function getAccountName() { | |
165 return $this->name; | |
166 } | |
167 | |
168 /** | |
169 * {@inheritdoc} | |
170 */ | |
171 public function getDisplayName() { | |
172 $name = $this->name ?: \Drupal::config('user.settings')->get('anonymous'); | |
173 \Drupal::moduleHandler()->alter('user_format_name', $name, $this); | |
174 return $name; | |
175 } | |
176 | |
177 /** | |
178 * {@inheritdoc} | |
179 */ | |
180 public function getEmail() { | |
181 return $this->mail; | |
182 } | |
183 | |
184 /** | |
185 * {@inheritdoc} | |
186 */ | |
187 public function getTimeZone() { | |
188 return $this->timezone; | |
189 } | |
190 | |
191 /** | |
192 * {@inheritdoc} | |
193 */ | |
194 public function getLastAccessedTime() { | |
195 return $this->access; | |
196 } | |
197 | |
198 /** | |
199 * Returns the role storage object. | |
200 * | |
201 * @return \Drupal\user\RoleStorageInterface | |
202 * The role storage object. | |
203 */ | |
204 protected function getRoleStorage() { | |
205 return \Drupal::entityManager()->getStorage('user_role'); | |
206 } | |
207 | |
208 } |