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