Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\user\Entity;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\ContentEntityBase;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityChangedTrait;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityStorageInterface;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@0
|
9 use Drupal\Core\Field\BaseFieldDefinition;
|
Chris@0
|
10 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
11 use Drupal\user\RoleInterface;
|
Chris@14
|
12 use Drupal\user\StatusItem;
|
Chris@14
|
13 use Drupal\user\TimeZoneItem;
|
Chris@0
|
14 use Drupal\user\UserInterface;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Defines the user entity class.
|
Chris@0
|
18 *
|
Chris@0
|
19 * The base table name here is plural, despite Drupal table naming standards,
|
Chris@0
|
20 * because "user" is a reserved word in many databases.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @ContentEntityType(
|
Chris@0
|
23 * id = "user",
|
Chris@0
|
24 * label = @Translation("User"),
|
Chris@17
|
25 * label_collection = @Translation("Users"),
|
Chris@17
|
26 * label_singular = @Translation("user"),
|
Chris@17
|
27 * label_plural = @Translation("users"),
|
Chris@17
|
28 * label_count = @PluralTranslation(
|
Chris@17
|
29 * singular = "@count user",
|
Chris@17
|
30 * plural = "@count users",
|
Chris@17
|
31 * ),
|
Chris@0
|
32 * handlers = {
|
Chris@0
|
33 * "storage" = "Drupal\user\UserStorage",
|
Chris@0
|
34 * "storage_schema" = "Drupal\user\UserStorageSchema",
|
Chris@0
|
35 * "access" = "Drupal\user\UserAccessControlHandler",
|
Chris@0
|
36 * "list_builder" = "Drupal\user\UserListBuilder",
|
Chris@0
|
37 * "views_data" = "Drupal\user\UserViewsData",
|
Chris@0
|
38 * "route_provider" = {
|
Chris@0
|
39 * "html" = "Drupal\user\Entity\UserRouteProvider",
|
Chris@0
|
40 * },
|
Chris@0
|
41 * "form" = {
|
Chris@0
|
42 * "default" = "Drupal\user\ProfileForm",
|
Chris@0
|
43 * "cancel" = "Drupal\user\Form\UserCancelForm",
|
Chris@0
|
44 * "register" = "Drupal\user\RegisterForm"
|
Chris@0
|
45 * },
|
Chris@0
|
46 * "translation" = "Drupal\user\ProfileTranslationHandler"
|
Chris@0
|
47 * },
|
Chris@0
|
48 * admin_permission = "administer users",
|
Chris@0
|
49 * base_table = "users",
|
Chris@0
|
50 * data_table = "users_field_data",
|
Chris@0
|
51 * label_callback = "user_format_name",
|
Chris@0
|
52 * translatable = TRUE,
|
Chris@0
|
53 * entity_keys = {
|
Chris@0
|
54 * "id" = "uid",
|
Chris@0
|
55 * "langcode" = "langcode",
|
Chris@0
|
56 * "uuid" = "uuid"
|
Chris@0
|
57 * },
|
Chris@0
|
58 * links = {
|
Chris@0
|
59 * "canonical" = "/user/{user}",
|
Chris@0
|
60 * "edit-form" = "/user/{user}/edit",
|
Chris@0
|
61 * "cancel-form" = "/user/{user}/cancel",
|
Chris@0
|
62 * "collection" = "/admin/people",
|
Chris@0
|
63 * },
|
Chris@0
|
64 * field_ui_base_route = "entity.user.admin_form",
|
Chris@0
|
65 * common_reference_target = TRUE
|
Chris@0
|
66 * )
|
Chris@0
|
67 */
|
Chris@0
|
68 class User extends ContentEntityBase implements UserInterface {
|
Chris@0
|
69
|
Chris@0
|
70 use EntityChangedTrait;
|
Chris@0
|
71
|
Chris@0
|
72 /**
|
Chris@0
|
73 * Stores a reference for a reusable anonymous user entity.
|
Chris@0
|
74 *
|
Chris@0
|
75 * @var \Drupal\user\UserInterface
|
Chris@0
|
76 */
|
Chris@0
|
77 protected static $anonymousUser;
|
Chris@0
|
78
|
Chris@0
|
79 /**
|
Chris@0
|
80 * {@inheritdoc}
|
Chris@0
|
81 */
|
Chris@0
|
82 public function isNew() {
|
Chris@0
|
83 return !empty($this->enforceIsNew) || $this->id() === NULL;
|
Chris@0
|
84 }
|
Chris@0
|
85
|
Chris@0
|
86 /**
|
Chris@0
|
87 * {@inheritdoc}
|
Chris@0
|
88 */
|
Chris@0
|
89 public function preSave(EntityStorageInterface $storage) {
|
Chris@0
|
90 parent::preSave($storage);
|
Chris@0
|
91
|
Chris@0
|
92 // Make sure that the authenticated/anonymous roles are not persisted.
|
Chris@0
|
93 foreach ($this->get('roles') as $index => $item) {
|
Chris@0
|
94 if (in_array($item->target_id, [RoleInterface::ANONYMOUS_ID, RoleInterface::AUTHENTICATED_ID])) {
|
Chris@0
|
95 $this->get('roles')->offsetUnset($index);
|
Chris@0
|
96 }
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 // Store account cancellation information.
|
Chris@0
|
100 foreach (['user_cancel_method', 'user_cancel_notify'] as $key) {
|
Chris@0
|
101 if (isset($this->{$key})) {
|
Chris@0
|
102 \Drupal::service('user.data')->set('user', $this->id(), substr($key, 5), $this->{$key});
|
Chris@0
|
103 }
|
Chris@0
|
104 }
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 /**
|
Chris@0
|
108 * {@inheritdoc}
|
Chris@0
|
109 */
|
Chris@0
|
110 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
|
Chris@0
|
111 parent::postSave($storage, $update);
|
Chris@0
|
112
|
Chris@0
|
113 if ($update) {
|
Chris@0
|
114 $session_manager = \Drupal::service('session_manager');
|
Chris@0
|
115 // If the password has been changed, delete all open sessions for the
|
Chris@0
|
116 // user and recreate the current one.
|
Chris@0
|
117 if ($this->pass->value != $this->original->pass->value) {
|
Chris@0
|
118 $session_manager->delete($this->id());
|
Chris@0
|
119 if ($this->id() == \Drupal::currentUser()->id()) {
|
Chris@0
|
120 \Drupal::service('session')->migrate();
|
Chris@0
|
121 }
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 // If the user was blocked, delete the user's sessions to force a logout.
|
Chris@0
|
125 if ($this->original->status->value != $this->status->value && $this->status->value == 0) {
|
Chris@0
|
126 $session_manager->delete($this->id());
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 // Send emails after we have the new user object.
|
Chris@0
|
130 if ($this->status->value != $this->original->status->value) {
|
Chris@0
|
131 // The user's status is changing; conditionally send notification email.
|
Chris@0
|
132 $op = $this->status->value == 1 ? 'status_activated' : 'status_blocked';
|
Chris@0
|
133 _user_mail_notify($op, $this);
|
Chris@0
|
134 }
|
Chris@0
|
135 }
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 /**
|
Chris@0
|
139 * {@inheritdoc}
|
Chris@0
|
140 */
|
Chris@0
|
141 public static function postDelete(EntityStorageInterface $storage, array $entities) {
|
Chris@0
|
142 parent::postDelete($storage, $entities);
|
Chris@0
|
143
|
Chris@0
|
144 $uids = array_keys($entities);
|
Chris@0
|
145 \Drupal::service('user.data')->delete(NULL, $uids);
|
Chris@0
|
146 }
|
Chris@0
|
147
|
Chris@0
|
148 /**
|
Chris@0
|
149 * {@inheritdoc}
|
Chris@0
|
150 */
|
Chris@0
|
151 public function getRoles($exclude_locked_roles = FALSE) {
|
Chris@0
|
152 $roles = [];
|
Chris@0
|
153
|
Chris@0
|
154 // Users with an ID always have the authenticated user role.
|
Chris@0
|
155 if (!$exclude_locked_roles) {
|
Chris@0
|
156 if ($this->isAuthenticated()) {
|
Chris@0
|
157 $roles[] = RoleInterface::AUTHENTICATED_ID;
|
Chris@0
|
158 }
|
Chris@0
|
159 else {
|
Chris@0
|
160 $roles[] = RoleInterface::ANONYMOUS_ID;
|
Chris@0
|
161 }
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 foreach ($this->get('roles') as $role) {
|
Chris@0
|
165 if ($role->target_id) {
|
Chris@0
|
166 $roles[] = $role->target_id;
|
Chris@0
|
167 }
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 return $roles;
|
Chris@0
|
171 }
|
Chris@0
|
172
|
Chris@0
|
173 /**
|
Chris@0
|
174 * {@inheritdoc}
|
Chris@0
|
175 */
|
Chris@0
|
176 public function hasRole($rid) {
|
Chris@0
|
177 return in_array($rid, $this->getRoles());
|
Chris@0
|
178 }
|
Chris@0
|
179
|
Chris@0
|
180 /**
|
Chris@0
|
181 * {@inheritdoc}
|
Chris@0
|
182 */
|
Chris@0
|
183 public function addRole($rid) {
|
Chris@0
|
184
|
Chris@0
|
185 if (in_array($rid, [RoleInterface::AUTHENTICATED_ID, RoleInterface::ANONYMOUS_ID])) {
|
Chris@0
|
186 throw new \InvalidArgumentException('Anonymous or authenticated role ID must not be assigned manually.');
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 $roles = $this->getRoles(TRUE);
|
Chris@0
|
190 $roles[] = $rid;
|
Chris@0
|
191 $this->set('roles', array_unique($roles));
|
Chris@0
|
192 }
|
Chris@0
|
193
|
Chris@0
|
194 /**
|
Chris@0
|
195 * {@inheritdoc}
|
Chris@0
|
196 */
|
Chris@0
|
197 public function removeRole($rid) {
|
Chris@0
|
198 $this->set('roles', array_diff($this->getRoles(TRUE), [$rid]));
|
Chris@0
|
199 }
|
Chris@0
|
200
|
Chris@0
|
201 /**
|
Chris@0
|
202 * {@inheritdoc}
|
Chris@0
|
203 */
|
Chris@0
|
204 public function hasPermission($permission) {
|
Chris@0
|
205 // User #1 has all privileges.
|
Chris@0
|
206 if ((int) $this->id() === 1) {
|
Chris@0
|
207 return TRUE;
|
Chris@0
|
208 }
|
Chris@0
|
209
|
Chris@0
|
210 return $this->getRoleStorage()->isPermissionInRoles($permission, $this->getRoles());
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 /**
|
Chris@0
|
214 * {@inheritdoc}
|
Chris@0
|
215 */
|
Chris@0
|
216 public function getPassword() {
|
Chris@0
|
217 return $this->get('pass')->value;
|
Chris@0
|
218 }
|
Chris@0
|
219
|
Chris@0
|
220 /**
|
Chris@0
|
221 * {@inheritdoc}
|
Chris@0
|
222 */
|
Chris@0
|
223 public function setPassword($password) {
|
Chris@0
|
224 $this->get('pass')->value = $password;
|
Chris@0
|
225 return $this;
|
Chris@0
|
226 }
|
Chris@0
|
227
|
Chris@0
|
228 /**
|
Chris@0
|
229 * {@inheritdoc}
|
Chris@0
|
230 */
|
Chris@0
|
231 public function getEmail() {
|
Chris@0
|
232 return $this->get('mail')->value;
|
Chris@0
|
233 }
|
Chris@0
|
234
|
Chris@0
|
235 /**
|
Chris@0
|
236 * {@inheritdoc}
|
Chris@0
|
237 */
|
Chris@0
|
238 public function setEmail($mail) {
|
Chris@0
|
239 $this->get('mail')->value = $mail;
|
Chris@0
|
240 return $this;
|
Chris@0
|
241 }
|
Chris@0
|
242
|
Chris@0
|
243 /**
|
Chris@0
|
244 * {@inheritdoc}
|
Chris@0
|
245 */
|
Chris@0
|
246 public function getCreatedTime() {
|
Chris@0
|
247 return $this->get('created')->value;
|
Chris@0
|
248 }
|
Chris@0
|
249
|
Chris@0
|
250 /**
|
Chris@0
|
251 * {@inheritdoc}
|
Chris@0
|
252 */
|
Chris@0
|
253 public function getLastAccessedTime() {
|
Chris@0
|
254 return $this->get('access')->value;
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * {@inheritdoc}
|
Chris@0
|
259 */
|
Chris@0
|
260 public function setLastAccessTime($timestamp) {
|
Chris@0
|
261 $this->get('access')->value = $timestamp;
|
Chris@0
|
262 return $this;
|
Chris@0
|
263 }
|
Chris@0
|
264
|
Chris@0
|
265 /**
|
Chris@0
|
266 * {@inheritdoc}
|
Chris@0
|
267 */
|
Chris@0
|
268 public function getLastLoginTime() {
|
Chris@0
|
269 return $this->get('login')->value;
|
Chris@0
|
270 }
|
Chris@0
|
271
|
Chris@0
|
272 /**
|
Chris@0
|
273 * {@inheritdoc}
|
Chris@0
|
274 */
|
Chris@0
|
275 public function setLastLoginTime($timestamp) {
|
Chris@0
|
276 $this->get('login')->value = $timestamp;
|
Chris@0
|
277 return $this;
|
Chris@0
|
278 }
|
Chris@0
|
279
|
Chris@0
|
280 /**
|
Chris@0
|
281 * {@inheritdoc}
|
Chris@0
|
282 */
|
Chris@0
|
283 public function isActive() {
|
Chris@0
|
284 return $this->get('status')->value == 1;
|
Chris@0
|
285 }
|
Chris@0
|
286
|
Chris@0
|
287 /**
|
Chris@0
|
288 * {@inheritdoc}
|
Chris@0
|
289 */
|
Chris@0
|
290 public function isBlocked() {
|
Chris@0
|
291 return $this->get('status')->value == 0;
|
Chris@0
|
292 }
|
Chris@0
|
293
|
Chris@0
|
294 /**
|
Chris@0
|
295 * {@inheritdoc}
|
Chris@0
|
296 */
|
Chris@0
|
297 public function activate() {
|
Chris@0
|
298 $this->get('status')->value = 1;
|
Chris@0
|
299 return $this;
|
Chris@0
|
300 }
|
Chris@0
|
301
|
Chris@0
|
302 /**
|
Chris@0
|
303 * {@inheritdoc}
|
Chris@0
|
304 */
|
Chris@0
|
305 public function block() {
|
Chris@0
|
306 $this->get('status')->value = 0;
|
Chris@0
|
307 return $this;
|
Chris@0
|
308 }
|
Chris@0
|
309
|
Chris@0
|
310 /**
|
Chris@0
|
311 * {@inheritdoc}
|
Chris@0
|
312 */
|
Chris@0
|
313 public function getTimeZone() {
|
Chris@0
|
314 return $this->get('timezone')->value;
|
Chris@0
|
315 }
|
Chris@0
|
316
|
Chris@0
|
317 /**
|
Chris@0
|
318 * {@inheritdoc}
|
Chris@0
|
319 */
|
Chris@0
|
320 public function getPreferredLangcode($fallback_to_default = TRUE) {
|
Chris@0
|
321 $language_list = $this->languageManager()->getLanguages();
|
Chris@0
|
322 $preferred_langcode = $this->get('preferred_langcode')->value;
|
Chris@0
|
323 if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) {
|
Chris@0
|
324 return $language_list[$preferred_langcode]->getId();
|
Chris@0
|
325 }
|
Chris@0
|
326 else {
|
Chris@0
|
327 return $fallback_to_default ? $this->languageManager()->getDefaultLanguage()->getId() : '';
|
Chris@0
|
328 }
|
Chris@0
|
329 }
|
Chris@0
|
330
|
Chris@0
|
331 /**
|
Chris@0
|
332 * {@inheritdoc}
|
Chris@0
|
333 */
|
Chris@0
|
334 public function getPreferredAdminLangcode($fallback_to_default = TRUE) {
|
Chris@0
|
335 $language_list = $this->languageManager()->getLanguages();
|
Chris@0
|
336 $preferred_langcode = $this->get('preferred_admin_langcode')->value;
|
Chris@0
|
337 if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) {
|
Chris@0
|
338 return $language_list[$preferred_langcode]->getId();
|
Chris@0
|
339 }
|
Chris@0
|
340 else {
|
Chris@0
|
341 return $fallback_to_default ? $this->languageManager()->getDefaultLanguage()->getId() : '';
|
Chris@0
|
342 }
|
Chris@0
|
343 }
|
Chris@0
|
344
|
Chris@0
|
345 /**
|
Chris@0
|
346 * {@inheritdoc}
|
Chris@0
|
347 */
|
Chris@0
|
348 public function getInitialEmail() {
|
Chris@0
|
349 return $this->get('init')->value;
|
Chris@0
|
350 }
|
Chris@0
|
351
|
Chris@0
|
352 /**
|
Chris@0
|
353 * {@inheritdoc}
|
Chris@0
|
354 */
|
Chris@0
|
355 public function isAuthenticated() {
|
Chris@0
|
356 return $this->id() > 0;
|
Chris@0
|
357 }
|
Chris@17
|
358
|
Chris@0
|
359 /**
|
Chris@0
|
360 * {@inheritdoc}
|
Chris@0
|
361 */
|
Chris@0
|
362 public function isAnonymous() {
|
Chris@0
|
363 return $this->id() == 0;
|
Chris@0
|
364 }
|
Chris@0
|
365
|
Chris@0
|
366 /**
|
Chris@0
|
367 * {@inheritdoc}
|
Chris@0
|
368 */
|
Chris@0
|
369 public function getUsername() {
|
Chris@18
|
370 @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
|
371 return $this->getAccountName();
|
Chris@0
|
372 }
|
Chris@0
|
373
|
Chris@0
|
374 /**
|
Chris@0
|
375 * {@inheritdoc}
|
Chris@0
|
376 */
|
Chris@0
|
377 public function getAccountName() {
|
Chris@0
|
378 return $this->get('name')->value ?: '';
|
Chris@0
|
379 }
|
Chris@0
|
380
|
Chris@0
|
381 /**
|
Chris@0
|
382 * {@inheritdoc}
|
Chris@0
|
383 */
|
Chris@0
|
384 public function getDisplayName() {
|
Chris@0
|
385 $name = $this->getAccountName() ?: \Drupal::config('user.settings')->get('anonymous');
|
Chris@0
|
386 \Drupal::moduleHandler()->alter('user_format_name', $name, $this);
|
Chris@0
|
387 return $name;
|
Chris@0
|
388 }
|
Chris@0
|
389
|
Chris@0
|
390 /**
|
Chris@0
|
391 * {@inheritdoc}
|
Chris@0
|
392 */
|
Chris@0
|
393 public function setUsername($username) {
|
Chris@0
|
394 $this->set('name', $username);
|
Chris@0
|
395 return $this;
|
Chris@0
|
396 }
|
Chris@0
|
397
|
Chris@0
|
398 /**
|
Chris@0
|
399 * {@inheritdoc}
|
Chris@0
|
400 */
|
Chris@0
|
401 public function setExistingPassword($password) {
|
Chris@0
|
402 $this->get('pass')->existing = $password;
|
Chris@0
|
403 }
|
Chris@0
|
404
|
Chris@0
|
405 /**
|
Chris@0
|
406 * {@inheritdoc}
|
Chris@0
|
407 */
|
Chris@0
|
408 public function checkExistingPassword(UserInterface $account_unchanged) {
|
Chris@0
|
409 return strlen($this->get('pass')->existing) > 0 && \Drupal::service('password')->check(trim($this->get('pass')->existing), $account_unchanged->getPassword());
|
Chris@0
|
410 }
|
Chris@0
|
411
|
Chris@0
|
412 /**
|
Chris@0
|
413 * Returns an anonymous user entity.
|
Chris@0
|
414 *
|
Chris@0
|
415 * @return \Drupal\user\UserInterface
|
Chris@0
|
416 * An anonymous user entity.
|
Chris@0
|
417 */
|
Chris@0
|
418 public static function getAnonymousUser() {
|
Chris@0
|
419 if (!isset(static::$anonymousUser)) {
|
Chris@0
|
420
|
Chris@0
|
421 // @todo Use the entity factory once available, see
|
Chris@0
|
422 // https://www.drupal.org/node/1867228.
|
Chris@0
|
423 $entity_manager = \Drupal::entityManager();
|
Chris@0
|
424 $entity_type = $entity_manager->getDefinition('user');
|
Chris@0
|
425 $class = $entity_type->getClass();
|
Chris@0
|
426
|
Chris@0
|
427 static::$anonymousUser = new $class([
|
Chris@0
|
428 'uid' => [LanguageInterface::LANGCODE_DEFAULT => 0],
|
Chris@0
|
429 'name' => [LanguageInterface::LANGCODE_DEFAULT => ''],
|
Chris@0
|
430 // Explicitly set the langcode to ensure that field definitions do not
|
Chris@0
|
431 // need to be fetched to figure out a default.
|
Chris@17
|
432 'langcode' => [LanguageInterface::LANGCODE_DEFAULT => LanguageInterface::LANGCODE_NOT_SPECIFIED],
|
Chris@0
|
433 ], $entity_type->id());
|
Chris@0
|
434 }
|
Chris@0
|
435 return clone static::$anonymousUser;
|
Chris@0
|
436 }
|
Chris@0
|
437
|
Chris@0
|
438 /**
|
Chris@0
|
439 * {@inheritdoc}
|
Chris@0
|
440 */
|
Chris@0
|
441 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
Chris@0
|
442 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
|
Chris@0
|
443 $fields = parent::baseFieldDefinitions($entity_type);
|
Chris@0
|
444
|
Chris@0
|
445 $fields['uid']->setLabel(t('User ID'))
|
Chris@0
|
446 ->setDescription(t('The user ID.'));
|
Chris@0
|
447
|
Chris@0
|
448 $fields['uuid']->setDescription(t('The user UUID.'));
|
Chris@0
|
449
|
Chris@0
|
450 $fields['langcode']->setLabel(t('Language code'))
|
Chris@0
|
451 ->setDescription(t('The user language code.'))
|
Chris@0
|
452 ->setDisplayOptions('form', ['region' => 'hidden']);
|
Chris@0
|
453
|
Chris@0
|
454 $fields['preferred_langcode'] = BaseFieldDefinition::create('language')
|
Chris@0
|
455 ->setLabel(t('Preferred language code'))
|
Chris@0
|
456 ->setDescription(t("The user's preferred language code for receiving emails and viewing the site."))
|
Chris@0
|
457 // @todo: Define this via an options provider once
|
Chris@0
|
458 // https://www.drupal.org/node/2329937 is completed.
|
Chris@0
|
459 ->addPropertyConstraints('value', [
|
Chris@0
|
460 'AllowedValues' => ['callback' => __CLASS__ . '::getAllowedConfigurableLanguageCodes'],
|
Chris@0
|
461 ]);
|
Chris@0
|
462
|
Chris@0
|
463 $fields['preferred_admin_langcode'] = BaseFieldDefinition::create('language')
|
Chris@0
|
464 ->setLabel(t('Preferred admin language code'))
|
Chris@0
|
465 ->setDescription(t("The user's preferred language code for viewing administration pages."))
|
Chris@0
|
466 // @todo: A default value of NULL is ignored, so we have to specify
|
Chris@0
|
467 // an empty field item structure instead. Fix this in
|
Chris@0
|
468 // https://www.drupal.org/node/2318605.
|
Chris@0
|
469 ->setDefaultValue([0 => ['value' => NULL]])
|
Chris@0
|
470 // @todo: Define this via an options provider once
|
Chris@0
|
471 // https://www.drupal.org/node/2329937 is completed.
|
Chris@0
|
472 ->addPropertyConstraints('value', [
|
Chris@0
|
473 'AllowedValues' => ['callback' => __CLASS__ . '::getAllowedConfigurableLanguageCodes'],
|
Chris@0
|
474 ]);
|
Chris@0
|
475
|
Chris@0
|
476 // The name should not vary per language. The username is the visual
|
Chris@0
|
477 // identifier for a user and needs to be consistent in all languages.
|
Chris@0
|
478 $fields['name'] = BaseFieldDefinition::create('string')
|
Chris@0
|
479 ->setLabel(t('Name'))
|
Chris@0
|
480 ->setDescription(t('The name of this user.'))
|
Chris@0
|
481 ->setRequired(TRUE)
|
Chris@0
|
482 ->setConstraints([
|
Chris@0
|
483 // No Length constraint here because the UserName constraint also covers
|
Chris@0
|
484 // that.
|
Chris@0
|
485 'UserName' => [],
|
Chris@0
|
486 'UserNameUnique' => [],
|
Chris@0
|
487 ]);
|
Chris@0
|
488 $fields['name']->getItemDefinition()->setClass('\Drupal\user\UserNameItem');
|
Chris@0
|
489
|
Chris@0
|
490 $fields['pass'] = BaseFieldDefinition::create('password')
|
Chris@0
|
491 ->setLabel(t('Password'))
|
Chris@0
|
492 ->setDescription(t('The password of this user (hashed).'))
|
Chris@0
|
493 ->addConstraint('ProtectedUserField');
|
Chris@0
|
494
|
Chris@0
|
495 $fields['mail'] = BaseFieldDefinition::create('email')
|
Chris@0
|
496 ->setLabel(t('Email'))
|
Chris@0
|
497 ->setDescription(t('The email of this user.'))
|
Chris@0
|
498 ->setDefaultValue('')
|
Chris@0
|
499 ->addConstraint('UserMailUnique')
|
Chris@0
|
500 ->addConstraint('UserMailRequired')
|
Chris@0
|
501 ->addConstraint('ProtectedUserField');
|
Chris@0
|
502
|
Chris@0
|
503 $fields['timezone'] = BaseFieldDefinition::create('string')
|
Chris@0
|
504 ->setLabel(t('Timezone'))
|
Chris@0
|
505 ->setDescription(t('The timezone of this user.'))
|
Chris@0
|
506 ->setSetting('max_length', 32)
|
Chris@0
|
507 // @todo: Define this via an options provider once
|
Chris@0
|
508 // https://www.drupal.org/node/2329937 is completed.
|
Chris@0
|
509 ->addPropertyConstraints('value', [
|
Chris@0
|
510 'AllowedValues' => ['callback' => __CLASS__ . '::getAllowedTimezones'],
|
Chris@0
|
511 ]);
|
Chris@14
|
512 $fields['timezone']->getItemDefinition()->setClass(TimeZoneItem::class);
|
Chris@0
|
513
|
Chris@0
|
514 $fields['status'] = BaseFieldDefinition::create('boolean')
|
Chris@0
|
515 ->setLabel(t('User status'))
|
Chris@0
|
516 ->setDescription(t('Whether the user is active or blocked.'))
|
Chris@0
|
517 ->setDefaultValue(FALSE);
|
Chris@14
|
518 $fields['status']->getItemDefinition()->setClass(StatusItem::class);
|
Chris@0
|
519
|
Chris@0
|
520 $fields['created'] = BaseFieldDefinition::create('created')
|
Chris@0
|
521 ->setLabel(t('Created'))
|
Chris@0
|
522 ->setDescription(t('The time that the user was created.'));
|
Chris@0
|
523
|
Chris@0
|
524 $fields['changed'] = BaseFieldDefinition::create('changed')
|
Chris@0
|
525 ->setLabel(t('Changed'))
|
Chris@0
|
526 ->setDescription(t('The time that the user was last edited.'))
|
Chris@0
|
527 ->setTranslatable(TRUE);
|
Chris@0
|
528
|
Chris@0
|
529 $fields['access'] = BaseFieldDefinition::create('timestamp')
|
Chris@0
|
530 ->setLabel(t('Last access'))
|
Chris@0
|
531 ->setDescription(t('The time that the user last accessed the site.'))
|
Chris@0
|
532 ->setDefaultValue(0);
|
Chris@0
|
533
|
Chris@0
|
534 $fields['login'] = BaseFieldDefinition::create('timestamp')
|
Chris@0
|
535 ->setLabel(t('Last login'))
|
Chris@0
|
536 ->setDescription(t('The time that the user last logged in.'))
|
Chris@0
|
537 ->setDefaultValue(0);
|
Chris@0
|
538
|
Chris@0
|
539 $fields['init'] = BaseFieldDefinition::create('email')
|
Chris@0
|
540 ->setLabel(t('Initial email'))
|
Chris@0
|
541 ->setDescription(t('The email address used for initial account creation.'))
|
Chris@0
|
542 ->setDefaultValue('');
|
Chris@0
|
543
|
Chris@0
|
544 $fields['roles'] = BaseFieldDefinition::create('entity_reference')
|
Chris@0
|
545 ->setLabel(t('Roles'))
|
Chris@0
|
546 ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
|
Chris@0
|
547 ->setDescription(t('The roles the user has.'))
|
Chris@0
|
548 ->setSetting('target_type', 'user_role');
|
Chris@0
|
549
|
Chris@0
|
550 return $fields;
|
Chris@0
|
551 }
|
Chris@0
|
552
|
Chris@0
|
553 /**
|
Chris@0
|
554 * Returns the role storage object.
|
Chris@0
|
555 *
|
Chris@0
|
556 * @return \Drupal\user\RoleStorageInterface
|
Chris@0
|
557 * The role storage object.
|
Chris@0
|
558 */
|
Chris@0
|
559 protected function getRoleStorage() {
|
Chris@0
|
560 return \Drupal::entityManager()->getStorage('user_role');
|
Chris@0
|
561 }
|
Chris@0
|
562
|
Chris@0
|
563 /**
|
Chris@0
|
564 * Defines allowed timezones for the field's AllowedValues constraint.
|
Chris@0
|
565 *
|
Chris@0
|
566 * @return string[]
|
Chris@0
|
567 * The allowed values.
|
Chris@0
|
568 */
|
Chris@0
|
569 public static function getAllowedTimezones() {
|
Chris@0
|
570 return array_keys(system_time_zones());
|
Chris@0
|
571 }
|
Chris@0
|
572
|
Chris@0
|
573 /**
|
Chris@0
|
574 * Defines allowed configurable language codes for AllowedValues constraints.
|
Chris@0
|
575 *
|
Chris@0
|
576 * @return string[]
|
Chris@0
|
577 * The allowed values.
|
Chris@0
|
578 */
|
Chris@0
|
579 public static function getAllowedConfigurableLanguageCodes() {
|
Chris@0
|
580 return array_keys(\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_CONFIGURABLE));
|
Chris@0
|
581 }
|
Chris@0
|
582
|
Chris@0
|
583 }
|