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