Newer
Older
<?php
/**
* @file
Angie Byron
committed
* Definition of Drupal\user\Plugin\Core\Entity\User.
*/
Angie Byron
committed
namespace Drupal\user\Plugin\Core\Entity;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Entity\EntityMalformedException;
Alex Pott
committed
use Drupal\Core\Entity\EntityNG;
use Drupal\Core\Entity\Annotation\EntityType;
Angie Byron
committed
use Drupal\Core\Annotation\Translation;
Alex Pott
committed
use Drupal\user\UserBCDecorator;
use Drupal\user\UserInterface;
Alex Pott
committed
use Drupal\Core\Language\Language;
/**
* Defines the user entity class.
Angie Byron
committed
*
* @EntityType(
Angie Byron
committed
* id = "user",
* label = @Translation("User"),
* module = "user",
* controllers = {
* "storage" = "Drupal\user\UserStorageController",
* "access" = "Drupal\user\UserAccessController",
* "render" = "Drupal\Core\Entity\EntityRenderController",
* "form" = {
Alex Pott
committed
* "default" = "Drupal\user\ProfileFormController",
* "register" = "Drupal\user\RegisterFormController"
* },
* "translation" = "Drupal\user\ProfileTranslationController"
Angie Byron
committed
* },
* base_table = "users",
* uri_callback = "user_uri",
* route_base_path = "admin/config/people/accounts",
Angie Byron
committed
* label_callback = "user_label",
* fieldable = TRUE,
Angie Byron
committed
* translatable = TRUE,
Angie Byron
committed
* entity_keys = {
* "id" = "uid",
* "uuid" = "uuid"
Alex Pott
committed
* },
* links = {
* "canonical" = "/user/{user}",
* "edit-form" = "/user/{user}/edit"
Angie Byron
committed
* }
* )
*/
Alex Pott
committed
class User extends EntityNG implements UserInterface {
/**
Alex Pott
committed
* {@inheritdoc}
*/
public function id() {
Alex Pott
committed
return $this->get('uid')->value;
}
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* {@inheritdoc}
*/
static function preCreate(EntityStorageControllerInterface $storage_controller, array &$values) {
if (!isset($values['created'])) {
$values['created'] = REQUEST_TIME;
}
// Users always have the authenticated user role.
$values['roles'][] = DRUPAL_AUTHENTICATED_RID;
}
/**
* {@inheritdoc}
*/
public function preSave(EntityStorageControllerInterface $storage_controller) {
// Update the user password if it has changed.
if ($this->isNew() || ($this->pass->value && $this->pass->value != $this->original->pass->value)) {
// Allow alternate password hashing schemes.
$this->pass->value = \Drupal::service('password')->hash(trim($this->pass->value));
// Abort if the hashing failed and returned FALSE.
if (!$this->pass->value) {
throw new EntityMalformedException('The entity does not have a password.');
}
}
if (!$this->isNew()) {
// If the password is empty, that means it was not changed, so use the
// original password.
if (empty($this->pass->value)) {
$this->pass->value = $this->original->pass->value;
}
}
// Store account cancellation information.
foreach (array('user_cancel_method', 'user_cancel_notify') as $key) {
if (isset($this->{$key})) {
\Drupal::service('user.data')->set('user', $this->id(), substr($key, 5), $this->{$key});
}
}
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) {
if ($update) {
// If the password has been changed, delete all open sessions for the
// user and recreate the current one.
if ($this->pass->value != $this->original->pass->value) {
drupal_session_destroy_uid($this->id());
Dries Buytaert
committed
if ($this->id() == $GLOBALS['user']->id()) {
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
drupal_session_regenerate();
}
}
// Update user roles if changed.
if ($this->roles->getValue() != $this->original->roles->getValue()) {
$storage_controller->deleteUserRoles(array($this->id()));
$storage_controller->saveRoles($this);
}
// If the user was blocked, delete the user's sessions to force a logout.
if ($this->original->status->value != $this->status->value && $this->status->value == 0) {
drupal_session_destroy_uid($this->id());
}
// Send emails after we have the new user object.
if ($this->status->value != $this->original->status->value) {
// The user's status is changing; conditionally send notification email.
$op = $this->status->value == 1 ? 'status_activated' : 'status_blocked';
_user_mail_notify($op, $this->getBCEntity());
}
}
else {
// Save user roles.
if (count($this->roles) > 1) {
$storage_controller->saveRoles($this);
}
}
}
/**
* {@inheritdoc}
*/
public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) {
$uids = array_keys($entities);
\Drupal::service('user.data')->delete(NULL, $uids);
$storage_controller->deleteUserRoles($uids);
}
Alex Pott
committed
/**
* {@inheritdoc}
*/
public function getBCEntity() {
if (!isset($this->bcEntity)) {
// Initialize field definitions so that we can pass them by reference.
$this->getPropertyDefinitions();
$this->bcEntity = new UserBCDecorator($this, $this->fieldDefinitions);
}
return $this->bcEntity;
}
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* {@inheritdoc}
*/
public function getRoles() {
$roles = array();
foreach ($this->get('roles') as $role) {
$roles[] = $role->value;
}
return $roles;
}
/**
* {@inheritdoc}
*/
public function getSecureSessionId() {
return NULL;
}
/**
* {@inheritdoc}
*/
public function getSessionData() {
return array();
}
/**
* {@inheritdoc}
*/
public function getSessionId() {
return NULL;
}
/**
* {@inheritdoc}
*/
public function hasRole($rid) {
return in_array($rid, $this->getRoles());
}
/**
* {@inheritdoc}
*/
public function addRole($rid) {
$roles = $this->getRoles();
$roles[] = $rid;
$this->set('roles', array_unique($roles));
}
/**
* {@inheritdoc}
*/
public function removeRole($rid) {
$this->set('roles', array_diff($this->getRoles(), array($rid)));
}
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/**
* {@inheritdoc}
*/
public function getPassword() {
return $this->get('pass')->value;
}
/**
* {@inheritdoc}
*/
public function setPassword($password) {
$this->get('pass')->value = $password;
}
/**
* {@inheritdoc}
*/
public function getEmail() {
return $this->get('mail')->value;
}
/**
* {@inheritdoc}
*/
public function setEmail($mail) {
$this->get('mail')->value = $mail;
}
/**
* {@inheritdoc}
*/
public function getDefaultTheme() {
return $this->get('theme')->value;
}
/**
* {@inheritdoc}
*/
public function getSignature() {
return $this->get('signature')->value;
}
/**
* {@inheritdoc}
*/
public function getSignatureFormat() {
return $this->get('signature_format')->value;
}
/**
* {@inheritdoc}
*/
public function getCreatedTime() {
return $this->get('created')->value;
}
/**
* {@inheritdoc}
*/
public function getLastAccessedTime() {
return $this->get('access')->value;
}
/**
* {@inheritdoc}
*/
public function setLastAccessTime($timestamp) {
$this->get('access')->value = $timestamp;
}
/**
* {@inheritdoc}
*/
public function getLastLoginTime() {
return $this->get('login')->value;
}
/**
* {@inheritdoc}
*/
public function setLastLoginTime($timestamp) {
$this->get('login')->value = $timestamp;
}
/**
* {@inheritdoc}
*/
public function isActive() {
return $this->get('status')->value == 1;
}
/**
* {@inheritdoc}
*/
public function isBlocked() {
return $this->get('status')->value == 0;
}
/**
* {@inheritdoc}
*/
public function activate() {
$this->get('status')->value = 1;
return $this;
}
/**
* {@inheritdoc}
*/
public function block() {
$this->get('status')->value = 0;
return $this;
}
/**
* {@inheritdoc}
*/
public function getTimeZone() {
return $this->get('timezone')->value;
}
/**
* {@inheritdoc}
*/
function getPreferredLangcode($default = NULL) {
$language_list = language_list();
$preferred_langcode = $this->get('preferred_langcode')->value;
if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) {
return $language_list[$preferred_langcode]->id;
}
else {
return $default ? $default : language_default()->id;
}
}
/**
* {@inheritdoc}
*/
function getPreferredAdminLangcode($default = NULL) {
$language_list = language_list();
$preferred_langcode = $this->get('preferred_admin_langcode')->value;
if (!empty($preferred_langcode) && isset($language_list[$preferred_langcode])) {
return $language_list[$preferred_langcode]->id;
}
else {
return $default ? $default : language_default()->id;
}
}
/**
* {@inheritdoc}
*/
public function getInitialEmail() {
return $this->get('init')->value;
}
/**
* {@inheritdoc}
*/
public function isAuthenticated() {
return $this->id() > 0;
}
/**
* {@inheritdoc}
*/
public function isAnonymous() {
return $this->id() == 0;
}
/**
* {@inheritdoc}
*/
public function getUsername() {
$name = $this->get('name')->value ?: \Drupal::config('user.settings')->get('anonymous');
\Drupal::moduleHandler()->alter('user_format_name', $name, $this);
return $name;
}