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