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@0
|
370 return $this->getAccountName();
|
Chris@0
|
371 }
|
Chris@0
|
372
|
Chris@0
|
373 /**
|
Chris@0
|
374 * {@inheritdoc}
|
Chris@0
|
375 */
|
Chris@0
|
376 public function getAccountName() {
|
Chris@0
|
377 return $this->get('name')->value ?: '';
|
Chris@0
|
378 }
|
Chris@0
|
379
|
Chris@0
|
380 /**
|
Chris@0
|
381 * {@inheritdoc}
|
Chris@0
|
382 */
|
Chris@0
|
383 public function getDisplayName() {
|
Chris@0
|
384 $name = $this->getAccountName() ?: \Drupal::config('user.settings')->get('anonymous');
|
Chris@0
|
385 \Drupal::moduleHandler()->alter('user_format_name', $name, $this);
|
Chris@0
|
386 return $name;
|
Chris@0
|
387 }
|
Chris@0
|
388
|
Chris@0
|
389 /**
|
Chris@0
|
390 * {@inheritdoc}
|
Chris@0
|
391 */
|
Chris@0
|
392 public function setUsername($username) {
|
Chris@0
|
393 $this->set('name', $username);
|
Chris@0
|
394 return $this;
|
Chris@0
|
395 }
|
Chris@0
|
396
|
Chris@0
|
397 /**
|
Chris@0
|
398 * {@inheritdoc}
|
Chris@0
|
399 */
|
Chris@0
|
400 public function setExistingPassword($password) {
|
Chris@0
|
401 $this->get('pass')->existing = $password;
|
Chris@0
|
402 }
|
Chris@0
|
403
|
Chris@0
|
404 /**
|
Chris@0
|
405 * {@inheritdoc}
|
Chris@0
|
406 */
|
Chris@0
|
407 public function checkExistingPassword(UserInterface $account_unchanged) {
|
Chris@0
|
408 return strlen($this->get('pass')->existing) > 0 && \Drupal::service('password')->check(trim($this->get('pass')->existing), $account_unchanged->getPassword());
|
Chris@0
|
409 }
|
Chris@0
|
410
|
Chris@0
|
411 /**
|
Chris@0
|
412 * Returns an anonymous user entity.
|
Chris@0
|
413 *
|
Chris@0
|
414 * @return \Drupal\user\UserInterface
|
Chris@0
|
415 * An anonymous user entity.
|
Chris@0
|
416 */
|
Chris@0
|
417 public static function getAnonymousUser() {
|
Chris@0
|
418 if (!isset(static::$anonymousUser)) {
|
Chris@0
|
419
|
Chris@0
|
420 // @todo Use the entity factory once available, see
|
Chris@0
|
421 // https://www.drupal.org/node/1867228.
|
Chris@0
|
422 $entity_manager = \Drupal::entityManager();
|
Chris@0
|
423 $entity_type = $entity_manager->getDefinition('user');
|
Chris@0
|
424 $class = $entity_type->getClass();
|
Chris@0
|
425
|
Chris@0
|
426 static::$anonymousUser = new $class([
|
Chris@0
|
427 'uid' => [LanguageInterface::LANGCODE_DEFAULT => 0],
|
Chris@0
|
428 'name' => [LanguageInterface::LANGCODE_DEFAULT => ''],
|
Chris@0
|
429 // Explicitly set the langcode to ensure that field definitions do not
|
Chris@0
|
430 // need to be fetched to figure out a default.
|
Chris@17
|
431 'langcode' => [LanguageInterface::LANGCODE_DEFAULT => LanguageInterface::LANGCODE_NOT_SPECIFIED],
|
Chris@0
|
432 ], $entity_type->id());
|
Chris@0
|
433 }
|
Chris@0
|
434 return clone static::$anonymousUser;
|
Chris@0
|
435 }
|
Chris@0
|
436
|
Chris@0
|
437 /**
|
Chris@0
|
438 * {@inheritdoc}
|
Chris@0
|
439 */
|
Chris@0
|
440 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
|
Chris@0
|
441 /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
|
Chris@0
|
442 $fields = parent::baseFieldDefinitions($entity_type);
|
Chris@0
|
443
|
Chris@0
|
444 $fields['uid']->setLabel(t('User ID'))
|
Chris@0
|
445 ->setDescription(t('The user ID.'));
|
Chris@0
|
446
|
Chris@0
|
447 $fields['uuid']->setDescription(t('The user UUID.'));
|
Chris@0
|
448
|
Chris@0
|
449 $fields['langcode']->setLabel(t('Language code'))
|
Chris@0
|
450 ->setDescription(t('The user language code.'))
|
Chris@0
|
451 ->setDisplayOptions('form', ['region' => 'hidden']);
|
Chris@0
|
452
|
Chris@0
|
453 $fields['preferred_langcode'] = BaseFieldDefinition::create('language')
|
Chris@0
|
454 ->setLabel(t('Preferred language code'))
|
Chris@0
|
455 ->setDescription(t("The user's preferred language code for receiving emails and viewing the site."))
|
Chris@0
|
456 // @todo: Define this via an options provider once
|
Chris@0
|
457 // https://www.drupal.org/node/2329937 is completed.
|
Chris@0
|
458 ->addPropertyConstraints('value', [
|
Chris@0
|
459 'AllowedValues' => ['callback' => __CLASS__ . '::getAllowedConfigurableLanguageCodes'],
|
Chris@0
|
460 ]);
|
Chris@0
|
461
|
Chris@0
|
462 $fields['preferred_admin_langcode'] = BaseFieldDefinition::create('language')
|
Chris@0
|
463 ->setLabel(t('Preferred admin language code'))
|
Chris@0
|
464 ->setDescription(t("The user's preferred language code for viewing administration pages."))
|
Chris@0
|
465 // @todo: A default value of NULL is ignored, so we have to specify
|
Chris@0
|
466 // an empty field item structure instead. Fix this in
|
Chris@0
|
467 // https://www.drupal.org/node/2318605.
|
Chris@0
|
468 ->setDefaultValue([0 => ['value' => NULL]])
|
Chris@0
|
469 // @todo: Define this via an options provider once
|
Chris@0
|
470 // https://www.drupal.org/node/2329937 is completed.
|
Chris@0
|
471 ->addPropertyConstraints('value', [
|
Chris@0
|
472 'AllowedValues' => ['callback' => __CLASS__ . '::getAllowedConfigurableLanguageCodes'],
|
Chris@0
|
473 ]);
|
Chris@0
|
474
|
Chris@0
|
475 // The name should not vary per language. The username is the visual
|
Chris@0
|
476 // identifier for a user and needs to be consistent in all languages.
|
Chris@0
|
477 $fields['name'] = BaseFieldDefinition::create('string')
|
Chris@0
|
478 ->setLabel(t('Name'))
|
Chris@0
|
479 ->setDescription(t('The name of this user.'))
|
Chris@0
|
480 ->setRequired(TRUE)
|
Chris@0
|
481 ->setConstraints([
|
Chris@0
|
482 // No Length constraint here because the UserName constraint also covers
|
Chris@0
|
483 // that.
|
Chris@0
|
484 'UserName' => [],
|
Chris@0
|
485 'UserNameUnique' => [],
|
Chris@0
|
486 ]);
|
Chris@0
|
487 $fields['name']->getItemDefinition()->setClass('\Drupal\user\UserNameItem');
|
Chris@0
|
488
|
Chris@0
|
489 $fields['pass'] = BaseFieldDefinition::create('password')
|
Chris@0
|
490 ->setLabel(t('Password'))
|
Chris@0
|
491 ->setDescription(t('The password of this user (hashed).'))
|
Chris@0
|
492 ->addConstraint('ProtectedUserField');
|
Chris@0
|
493
|
Chris@0
|
494 $fields['mail'] = BaseFieldDefinition::create('email')
|
Chris@0
|
495 ->setLabel(t('Email'))
|
Chris@0
|
496 ->setDescription(t('The email of this user.'))
|
Chris@0
|
497 ->setDefaultValue('')
|
Chris@0
|
498 ->addConstraint('UserMailUnique')
|
Chris@0
|
499 ->addConstraint('UserMailRequired')
|
Chris@0
|
500 ->addConstraint('ProtectedUserField');
|
Chris@0
|
501
|
Chris@0
|
502 $fields['timezone'] = BaseFieldDefinition::create('string')
|
Chris@0
|
503 ->setLabel(t('Timezone'))
|
Chris@0
|
504 ->setDescription(t('The timezone of this user.'))
|
Chris@0
|
505 ->setSetting('max_length', 32)
|
Chris@0
|
506 // @todo: Define this via an options provider once
|
Chris@0
|
507 // https://www.drupal.org/node/2329937 is completed.
|
Chris@0
|
508 ->addPropertyConstraints('value', [
|
Chris@0
|
509 'AllowedValues' => ['callback' => __CLASS__ . '::getAllowedTimezones'],
|
Chris@0
|
510 ]);
|
Chris@14
|
511 $fields['timezone']->getItemDefinition()->setClass(TimeZoneItem::class);
|
Chris@0
|
512
|
Chris@0
|
513 $fields['status'] = BaseFieldDefinition::create('boolean')
|
Chris@0
|
514 ->setLabel(t('User status'))
|
Chris@0
|
515 ->setDescription(t('Whether the user is active or blocked.'))
|
Chris@0
|
516 ->setDefaultValue(FALSE);
|
Chris@14
|
517 $fields['status']->getItemDefinition()->setClass(StatusItem::class);
|
Chris@0
|
518
|
Chris@0
|
519 $fields['created'] = BaseFieldDefinition::create('created')
|
Chris@0
|
520 ->setLabel(t('Created'))
|
Chris@0
|
521 ->setDescription(t('The time that the user was created.'));
|
Chris@0
|
522
|
Chris@0
|
523 $fields['changed'] = BaseFieldDefinition::create('changed')
|
Chris@0
|
524 ->setLabel(t('Changed'))
|
Chris@0
|
525 ->setDescription(t('The time that the user was last edited.'))
|
Chris@0
|
526 ->setTranslatable(TRUE);
|
Chris@0
|
527
|
Chris@0
|
528 $fields['access'] = BaseFieldDefinition::create('timestamp')
|
Chris@0
|
529 ->setLabel(t('Last access'))
|
Chris@0
|
530 ->setDescription(t('The time that the user last accessed the site.'))
|
Chris@0
|
531 ->setDefaultValue(0);
|
Chris@0
|
532
|
Chris@0
|
533 $fields['login'] = BaseFieldDefinition::create('timestamp')
|
Chris@0
|
534 ->setLabel(t('Last login'))
|
Chris@0
|
535 ->setDescription(t('The time that the user last logged in.'))
|
Chris@0
|
536 ->setDefaultValue(0);
|
Chris@0
|
537
|
Chris@0
|
538 $fields['init'] = BaseFieldDefinition::create('email')
|
Chris@0
|
539 ->setLabel(t('Initial email'))
|
Chris@0
|
540 ->setDescription(t('The email address used for initial account creation.'))
|
Chris@0
|
541 ->setDefaultValue('');
|
Chris@0
|
542
|
Chris@0
|
543 $fields['roles'] = BaseFieldDefinition::create('entity_reference')
|
Chris@0
|
544 ->setLabel(t('Roles'))
|
Chris@0
|
545 ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
|
Chris@0
|
546 ->setDescription(t('The roles the user has.'))
|
Chris@0
|
547 ->setSetting('target_type', 'user_role');
|
Chris@0
|
548
|
Chris@0
|
549 return $fields;
|
Chris@0
|
550 }
|
Chris@0
|
551
|
Chris@0
|
552 /**
|
Chris@0
|
553 * Returns the role storage object.
|
Chris@0
|
554 *
|
Chris@0
|
555 * @return \Drupal\user\RoleStorageInterface
|
Chris@0
|
556 * The role storage object.
|
Chris@0
|
557 */
|
Chris@0
|
558 protected function getRoleStorage() {
|
Chris@0
|
559 return \Drupal::entityManager()->getStorage('user_role');
|
Chris@0
|
560 }
|
Chris@0
|
561
|
Chris@0
|
562 /**
|
Chris@0
|
563 * Defines allowed timezones for the field's AllowedValues constraint.
|
Chris@0
|
564 *
|
Chris@0
|
565 * @return string[]
|
Chris@0
|
566 * The allowed values.
|
Chris@0
|
567 */
|
Chris@0
|
568 public static function getAllowedTimezones() {
|
Chris@0
|
569 return array_keys(system_time_zones());
|
Chris@0
|
570 }
|
Chris@0
|
571
|
Chris@0
|
572 /**
|
Chris@0
|
573 * Defines allowed configurable language codes for AllowedValues constraints.
|
Chris@0
|
574 *
|
Chris@0
|
575 * @return string[]
|
Chris@0
|
576 * The allowed values.
|
Chris@0
|
577 */
|
Chris@0
|
578 public static function getAllowedConfigurableLanguageCodes() {
|
Chris@0
|
579 return array_keys(\Drupal::languageManager()->getLanguages(LanguageInterface::STATE_CONFIGURABLE));
|
Chris@0
|
580 }
|
Chris@0
|
581
|
Chris@0
|
582 }
|