Skip to content

Commit 6227aa4

Browse files
committed
Issue #2045275 by Berdir: Convert user properties to methods.
1 parent e29fe15 commit 6227aa4

39 files changed

+239
-226
lines changed

lib/Drupal/user/Access/RoleAccessCheck.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ public function access(Route $route, Request $request) {
3838

3939
$explode_and = array_filter(array_map('trim', explode('+', $rid_string)));
4040
if (count($explode_and) > 1) {
41-
$diff = array_diff($explode_and, $account->roles);
41+
$diff = array_diff($explode_and, $account->getRoles());
4242
if (empty($diff)) {
4343
return static::ALLOW;
4444
}
4545
}
4646
else {
4747
$explode_or = array_filter(array_map('trim', explode(',', $rid_string)));
48-
$intersection = array_intersect($explode_or, $account->roles);
48+
$intersection = array_intersect($explode_or, $account->getRoles());
4949
if (!empty($intersection)) {
5050
return static::ALLOW;
5151
}

lib/Drupal/user/AccountFormController.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function form(array $form, array &$form_state) {
4242
'#required' => TRUE,
4343
'#attributes' => array('class' => array('username'), 'autocorrect' => 'off', 'autocomplete' => 'off', 'autocapitalize' => 'off',
4444
'spellcheck' => 'false'),
45-
'#default_value' => (!$register ? $account->name : ''),
45+
'#default_value' => (!$register ? $account->getUsername() : ''),
4646
'#access' => ($register || ($user->id() == $account->id() && user_access('change own username')) || $admin),
4747
'#weight' => -10,
4848
);
@@ -54,8 +54,8 @@ public function form(array $form, array &$form_state) {
5454
'#type' => 'email',
5555
'#title' => t('E-mail address'),
5656
'#description' => t('A valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.'),
57-
'#required' => !(empty($account->mail) && user_access('administer users')),
58-
'#default_value' => (!$register ? $account->mail : ''),
57+
'#required' => !(!$account->getEmail() && user_access('administer users')),
58+
'#default_value' => (!$register ? $account->getEmail() : ''),
5959
'#attributes' => array('autocomplete' => 'off'),
6060
);
6161

@@ -117,10 +117,10 @@ public function form(array $form, array &$form_state) {
117117
}
118118

119119
if ($admin) {
120-
$status = isset($account->status) ? $account->status : 1;
120+
$status = $account->isActive();
121121
}
122122
else {
123-
$status = $register ? $config->get('register') == USER_REGISTER_VISITORS : $account->status;
123+
$status = $register ? $config->get('register') == USER_REGISTER_VISITORS : $account->isActive();
124124
}
125125

126126
$form['account']['status'] = array(
@@ -148,7 +148,7 @@ public function form(array $form, array &$form_state) {
148148
$form['account']['roles'] = array(
149149
'#type' => 'checkboxes',
150150
'#title' => t('Roles'),
151-
'#default_value' => (!$register ? $account->roles : array()),
151+
'#default_value' => (!$register ? $account->getRoles() : array()),
152152
'#options' => $roles,
153153
'#access' => $roles && user_access('administer permissions'),
154154
DRUPAL_AUTHENTICATED_RID => $checkbox_authenticated,
@@ -171,9 +171,9 @@ public function form(array $form, array &$form_state) {
171171
$form['signature_settings']['signature'] = array(
172172
'#type' => 'text_format',
173173
'#title' => t('Signature'),
174-
'#default_value' => isset($account->signature) ? $account->signature : '',
174+
'#default_value' => $account->getSignature(),
175175
'#description' => t('Your signature will be publicly displayed at the end of your comments.'),
176-
'#format' => isset($account->signature_format) ? $account->signature_format : NULL,
176+
'#format' => $account->getSignatureFormat(),
177177
);
178178

179179
$user_preferred_langcode = $register ? $language_interface->id : $account->getPreferredLangcode();

lib/Drupal/user/Form/UserPasswordForm.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ public function buildForm(array $form, array &$form_state, Request $request = NU
8989
// Allow logged in users to request this also.
9090
if ($user->isAuthenticated()) {
9191
$form['name']['#type'] = 'value';
92-
$form['name']['#value'] = $user->mail;
92+
$form['name']['#value'] = $user->getEmail();
9393
$form['mail'] = array(
9494
'#prefix' => '<p>',
95-
'#markup' => t('Password reset instructions will be mailed to %email. You must log out to use the password reset link in the e-mail.', array('%email' => $user->mail)),
95+
'#markup' => t('Password reset instructions will be mailed to %email. You must log out to use the password reset link in the e-mail.', array('%email' => $user->getEmail())),
9696
'#suffix' => '</p>',
9797
);
9898
}
@@ -135,7 +135,7 @@ public function submitForm(array &$form, array &$form_state) {
135135
// Mail one time login URL and instructions using current language.
136136
$mail = _user_mail_notify('password_reset', $account->getBCEntity(), $langcode);
137137
if (!empty($mail)) {
138-
watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
138+
watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->getUsername(), '%email' => $account->getEmail()));
139139
drupal_set_message(t('Further instructions have been sent to your e-mail address.'));
140140
}
141141

lib/Drupal/user/Plugin/Action/BlockUser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class BlockUser extends ActionBase {
2727
*/
2828
public function execute($account = NULL) {
2929
// Skip blocking user if they are already blocked.
30-
if ($account !== FALSE && $account->status->value == 1) {
30+
if ($account !== FALSE && $account->isActive()) {
3131
// For efficiency manually save the original account before applying any
3232
// changes.
3333
$account->original = clone $account;
34-
$account->status = 0;
34+
$account->block();
3535
$account->save();
3636
}
3737
}

lib/Drupal/user/Plugin/Action/UnblockUser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class UnblockUser extends ActionBase {
2727
*/
2828
public function execute($account = NULL) {
2929
// Skip unblocking user if they are already unblocked.
30-
if ($account !== FALSE && $account->status->value == 0) {
31-
$account->status = 1;
30+
if ($account !== FALSE && $account->isBlocked()) {
31+
$account->activate();
3232
$account->save();
3333
}
3434
}

lib/Drupal/user/Plugin/Core/Entity/User.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function postSave(EntityStorageControllerInterface $storage_controller, $
114114
}
115115

116116
// Update user roles if changed.
117-
if ($this->roles->getValue() != $this->original->roles->getValue()) {
117+
if ($this->getRoles() != $this->original->getRoles()) {
118118
$storage_controller->deleteUserRoles(array($this->id()));
119119
$storage_controller->saveRoles($this);
120120
}
@@ -133,7 +133,7 @@ public function postSave(EntityStorageControllerInterface $storage_controller, $
133133
}
134134
else {
135135
// Save user roles.
136-
if (count($this->roles) > 1) {
136+
if (count($this->getRoles()) > 1) {
137137
$storage_controller->saveRoles($this);
138138
}
139139
}
@@ -247,6 +247,7 @@ public function getPassword() {
247247
*/
248248
public function setPassword($password) {
249249
$this->get('pass')->value = $password;
250+
return $this;
250251
}
251252

252253
/**
@@ -261,6 +262,7 @@ public function getEmail() {
261262
*/
262263
public function setEmail($mail) {
263264
$this->get('mail')->value = $mail;
265+
return $this;
264266
}
265267

266268
/**
@@ -303,6 +305,7 @@ public function getLastAccessedTime() {
303305
*/
304306
public function setLastAccessTime($timestamp) {
305307
$this->get('access')->value = $timestamp;
308+
return $this;
306309
}
307310

308311
/**
@@ -317,6 +320,7 @@ public function getLastLoginTime() {
317320
*/
318321
public function setLastLoginTime($timestamp) {
319322
$this->get('login')->value = $timestamp;
323+
return $this;
320324
}
321325

322326
/**
@@ -413,4 +417,12 @@ public function getUsername() {
413417
return $name;
414418
}
415419

420+
/**
421+
* {@inheritdoc}
422+
*/
423+
public function setUsername($username) {
424+
$this->set('name', $username);
425+
return $this;
426+
}
427+
416428
}

lib/Drupal/user/Plugin/views/access/Role.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Role extends AccessPluginBase {
3535
* {@inheritdoc}
3636
*/
3737
public function access(AccountInterface $account) {
38-
return user_access('access all views', $account) || array_intersect(array_filter($this->options['role']), $account->roles);
38+
return user_access('access all views', $account) || array_intersect(array_filter($this->options['role']), $account->getRoles());
3939
}
4040

4141
/**

lib/Drupal/user/Plugin/views/filter/Name.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ protected function valueForm(&$form, &$form_state) {
2727
$result = entity_load_multiple_by_properties('user', array('uid' => $this->value));
2828
foreach ($result as $account) {
2929
if ($account->id()) {
30-
$values[] = $account->name->value;
30+
$values[] = $account->getUsername();
3131
}
3232
else {
3333
$values[] = 'Anonymous'; // Intentionally NOT translated.
@@ -130,7 +130,7 @@ function validate_user_strings(&$form, $values) {
130130

131131
$result = entity_load_multiple_by_properties('user', array('name' => $args));
132132
foreach ($result as $account) {
133-
unset($missing[strtolower($account->name->value)]);
133+
unset($missing[strtolower($account->getUsername())]);
134134
$uids[] = $account->id();
135135
}
136136

lib/Drupal/user/RegisterFormController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function submit(array $form, array &$form_state) {
9090
*/
9191
public function save(array $form, array &$form_state) {
9292
$account = $this->entity;
93-
$pass = $account->pass;
93+
$pass = $account->getPassword();
9494
$admin = $form_state['values']['administer_users'];
9595
$notify = !empty($form_state['values']['notify']);
9696

@@ -109,25 +109,25 @@ public function save(array $form, array &$form_state) {
109109
// New administrative account without notification.
110110
$uri = $account->uri();
111111
if ($admin && !$notify) {
112-
drupal_set_message(t('Created a new user account for <a href="@url">%name</a>. No e-mail has been sent.', array('@url' => url($uri['path'], $uri['options']), '%name' => $account->name)));
112+
drupal_set_message(t('Created a new user account for <a href="@url">%name</a>. No e-mail has been sent.', array('@url' => url($uri['path'], $uri['options']), '%name' => $account->getUsername())));
113113
}
114114
// No e-mail verification required; log in user immediately.
115-
elseif (!$admin && !config('user.settings')->get('verify_mail') && $account->status) {
115+
elseif (!$admin && !config('user.settings')->get('verify_mail') && $account->isActive()) {
116116
_user_mail_notify('register_no_approval_required', $account);
117117
user_login_finalize($account);
118118
drupal_set_message(t('Registration successful. You are now logged in.'));
119119
$form_state['redirect'] = '';
120120
}
121121
// No administrator approval required.
122-
elseif ($account->status || $notify) {
123-
if (empty($account->mail) && $notify) {
124-
drupal_set_message(t('The new user <a href="@url">%name</a> was created without an email address, so no welcome message was sent.', array('@url' => url($uri['path'], $uri['options']), '%name' => $account->name)));
122+
elseif ($account->isActive() || $notify) {
123+
if (!$account->getEmail() && $notify) {
124+
drupal_set_message(t('The new user <a href="@url">%name</a> was created without an email address, so no welcome message was sent.', array('@url' => url($uri['path'], $uri['options']), '%name' => $account->getUsername())));
125125
}
126126
else {
127127
$op = $notify ? 'register_admin_created' : 'register_no_approval_required';
128128
if (_user_mail_notify($op, $account)) {
129129
if ($notify) {
130-
drupal_set_message(t('A welcome message with further instructions has been e-mailed to the new user <a href="@url">%name</a>.', array('@url' => url($uri['path'], $uri['options']), '%name' => $account->name)));
130+
drupal_set_message(t('A welcome message with further instructions has been e-mailed to the new user <a href="@url">%name</a>.', array('@url' => url($uri['path'], $uri['options']), '%name' => $account->getUsername())));
131131
}
132132
else {
133133
drupal_set_message(t('A welcome message with further instructions has been sent to your e-mail address.'));

lib/Drupal/user/Tests/UserAdminListingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testUserListing() {
3939
}
4040
// Create a blocked user.
4141
$account = $this->drupalCreateUser()->getNGEntity();
42-
$account->status = 0;
42+
$account->block();
4343
$account->save();
4444
$accounts[$account->label()] = $account;
4545

0 commit comments

Comments
 (0)