Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Session/AccountProxy.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | af1871eacc83 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\Session; | |
4 | |
5 /** | |
6 * A proxied implementation of AccountInterface. | |
7 * | |
8 * The reason why we need an account proxy is that we don't want to have global | |
9 * state directly stored in the container. | |
10 * | |
11 * This proxy object avoids multiple invocations of the authentication manager | |
12 * which can happen if the current user is accessed in constructors. It also | |
13 * allows legacy code to change the current user where the user cannot be | |
14 * directly injected into dependent code. | |
15 */ | |
16 class AccountProxy implements AccountProxyInterface { | |
17 | |
18 /** | |
19 * The instantiated account. | |
20 * | |
21 * @var \Drupal\Core\Session\AccountInterface | |
22 */ | |
23 protected $account; | |
24 | |
25 /** | |
26 * Account id. | |
27 * | |
28 * @var int | |
29 */ | |
30 protected $id = 0; | |
31 | |
32 /** | |
33 * Initial account id. | |
34 * | |
35 * @var int | |
36 * | |
37 * @deprecated Scheduled for removal in Drupal 8.4.x. Use $this->id instead. | |
38 */ | |
39 protected $initialAccountId; | |
40 | |
41 /** | |
42 * {@inheritdoc} | |
43 */ | |
44 public function setAccount(AccountInterface $account) { | |
45 // If the passed account is already proxied, use the actual account instead | |
46 // to prevent loops. | |
47 if ($account instanceof static) { | |
48 $account = $account->getAccount(); | |
49 } | |
50 $this->account = $account; | |
51 $this->id = $account->id(); | |
52 date_default_timezone_set(drupal_get_user_timezone()); | |
53 } | |
54 | |
55 /** | |
56 * {@inheritdoc} | |
57 */ | |
58 public function getAccount() { | |
59 if (!isset($this->account)) { | |
60 if ($this->id) { | |
61 // After the container is rebuilt, DrupalKernel sets the initial | |
62 // account to the id of the logged in user. This is necessary in order | |
63 // to refresh the user account reference here. | |
64 $this->setAccount($this->loadUserEntity($this->id)); | |
65 } | |
66 else { | |
67 $this->account = new AnonymousUserSession(); | |
68 } | |
69 } | |
70 | |
71 return $this->account; | |
72 } | |
73 | |
74 /** | |
75 * {@inheritdoc} | |
76 */ | |
77 public function id() { | |
78 return $this->id; | |
79 } | |
80 | |
81 /** | |
82 * {@inheritdoc} | |
83 */ | |
84 public function getRoles($exclude_locked_roles = FALSE) { | |
85 return $this->getAccount()->getRoles($exclude_locked_roles); | |
86 } | |
87 | |
88 /** | |
89 * {@inheritdoc} | |
90 */ | |
91 public function hasPermission($permission) { | |
92 return $this->getAccount()->hasPermission($permission); | |
93 } | |
94 | |
95 /** | |
96 * {@inheritdoc} | |
97 */ | |
98 public function isAuthenticated() { | |
99 return $this->getAccount()->isAuthenticated(); | |
100 } | |
101 | |
102 /** | |
103 * {@inheritdoc} | |
104 */ | |
105 public function isAnonymous() { | |
106 return $this->getAccount()->isAnonymous(); | |
107 } | |
108 | |
109 /** | |
110 * {@inheritdoc} | |
111 */ | |
112 public function getPreferredLangcode($fallback_to_default = TRUE) { | |
113 return $this->getAccount()->getPreferredLangcode($fallback_to_default); | |
114 } | |
115 | |
116 /** | |
117 * {@inheritdoc} | |
118 */ | |
119 public function getPreferredAdminLangcode($fallback_to_default = TRUE) { | |
120 return $this->getAccount()->getPreferredAdminLangcode($fallback_to_default); | |
121 } | |
122 | |
123 /** | |
124 * {@inheritdoc} | |
125 */ | |
126 public function getUsername() { | |
127 return $this->getAccountName(); | |
128 } | |
129 | |
130 /** | |
131 * {@inheritdoc} | |
132 */ | |
133 public function getAccountName() { | |
134 return $this->getAccount()->getAccountName(); | |
135 } | |
136 | |
137 /** | |
138 * {@inheritdoc} | |
139 */ | |
140 public function getDisplayName() { | |
141 return $this->getAccount()->getDisplayName(); | |
142 } | |
143 | |
144 /** | |
145 * {@inheritdoc} | |
146 */ | |
147 public function getEmail() { | |
148 return $this->getAccount()->getEmail(); | |
149 } | |
150 | |
151 /** | |
152 * {@inheritdoc} | |
153 */ | |
154 public function getTimeZone() { | |
155 return $this->getAccount()->getTimeZone(); | |
156 } | |
157 | |
158 /** | |
159 * {@inheritdoc} | |
160 */ | |
161 public function getLastAccessedTime() { | |
162 return $this->getAccount()->getLastAccessedTime(); | |
163 } | |
164 | |
165 /** | |
166 * {@inheritdoc} | |
167 */ | |
168 public function setInitialAccountId($account_id) { | |
169 if (isset($this->account)) { | |
170 throw new \LogicException('AccountProxyInterface::setInitialAccountId() cannot be called after an account was set on the AccountProxy'); | |
171 } | |
172 | |
173 $this->id = $this->initialAccountId = $account_id; | |
174 } | |
175 | |
176 /** | |
177 * Load a user entity. | |
178 * | |
179 * The entity manager requires additional initialization code and cache | |
180 * clearing after the list of modules is changed. Therefore it is necessary to | |
181 * retrieve it as late as possible. | |
182 * | |
183 * Because of serialization issues it is currently not possible to inject the | |
184 * container into the AccountProxy. Thus it is necessary to retrieve the | |
185 * entity manager statically. | |
186 * | |
187 * @see https://www.drupal.org/node/2430447 | |
188 * | |
189 * @param int $account_id | |
190 * The id of an account to load. | |
191 * | |
192 * @return \Drupal\Core\Session\AccountInterface|null | |
193 * An account or NULL if none is found. | |
194 */ | |
195 protected function loadUserEntity($account_id) { | |
196 return \Drupal::entityManager()->getStorage('user')->load($account_id); | |
197 } | |
198 | |
199 } |