5 abstract class UserSingleBase {
7 // A Drupal user entity.
10 public function __construct($account) {
11 $this->account = $account;
15 * A flatter and simpler array presentation of a Drupal $user object.
19 public function info() {
21 'uid' => $this->account->id(),
22 'name' => $this->account->getUsername(),
23 'password' => $this->account->getPassword(),
24 'mail' => $this->account->getEmail(),
25 'user_created' => $this->account->getCreatedTime(),
26 'created' => format_date($this->account->getCreatedTime()),
27 'user_access' => $this->account->getLastAccessedTime(),
28 'access' => format_date($this->account->getLastAccessedTime()),
29 'user_login' => $this->account->getLastLoginTime(),
30 'login' => format_date($this->account->getLastLoginTime()),
31 'user_status' => $this->account->get('status')->value,
32 'status' => $this->account->isActive() ? 'active' : 'blocked',
33 'timezone' => $this->account->getTimeZone(),
34 'roles' => $this->account->getRoles(),
35 'langcode' => $this->account->getPreferredLangcode(),
36 'uuid' => $this->account->uuid->value,
41 * Block a user from login.
43 public function block() {
44 $this->account->block();
45 $this->account->save();
49 * Unblock a user from login.
51 public function unblock() {
52 $this->account->get('status')->value = 1;
53 $this->account->save();
57 * Add a role to the current user.
62 public function addRole($rid) {
63 $this->account->addRole($rid);
64 $this->account->save();
68 * Remove a role from the current user.
73 public function removeRole($rid) {
74 $this->account->removeRole($rid);
75 $this->account->save();
79 * Block a user and remove or reassign their content.
81 public function cancel() {
82 if (drush_get_option('delete-content')) {
83 user_cancel(array(), $this->id(), 'user_cancel_delete');
86 user_cancel(array(), $this->id(), 'user_cancel_reassign');
88 // I got the following technique here: http://drupal.org/node/638712
89 $batch =& batch_get();
90 $batch['progressive'] = FALSE;
95 * Change a user's password.
99 public function password($password) {
100 $this->account->setPassword($password);
101 $this->account->save();
105 * Build a one time login link.
107 * @param string $path
110 public function passResetUrl($path = '') {
111 $url = user_pass_reset_url($this->account) . '/login';
113 $url .= '?destination=' . $path;
122 public function getUsername() {
123 return $this->account->getUsername();
127 * Return an id from a Drupal user account.
130 public function id() {
131 return $this->account->id();