Version 1
[yaffs-website] / vendor / drush / drush / lib / Drush / User / UserSingleBase.php
1 <?php
2
3 namespace Drush\User;
4
5 abstract class UserSingleBase {
6
7   // A Drupal user entity.
8   public $account;
9
10   public function __construct($account) {
11     $this->account = $account;
12   }
13
14   /**
15    * A flatter and simpler array presentation of a Drupal $user object.
16    *
17    * @return array
18    */
19   public function info() {
20     return array(
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,
37     );
38   }
39
40   /**
41    * Block a user from login.
42    */
43   public function block() {
44     $this->account->block();
45     $this->account->save();
46   }
47
48   /**
49    * Unblock a user from login.
50    */
51   public function unblock() {
52     $this->account->get('status')->value = 1;
53     $this->account->save();
54   }
55
56   /**
57    * Add a role to the current user.
58    *
59    * @param $rid
60    *   A role ID.
61    */
62   public function addRole($rid) {
63     $this->account->addRole($rid);
64     $this->account->save();
65   }
66
67   /**
68    * Remove a role from the current user.
69    *
70    * @param $rid
71    *   A role ID.
72    */
73   public function removeRole($rid) {
74     $this->account->removeRole($rid);
75     $this->account->save();
76   }
77
78   /**
79    * Block a user and remove or reassign their content.
80    */
81   public function cancel() {
82       if (drush_get_option('delete-content')) {
83         user_cancel(array(), $this->id(), 'user_cancel_delete');
84       }
85       else {
86         user_cancel(array(), $this->id(), 'user_cancel_reassign');
87       }
88       // I got the following technique here: http://drupal.org/node/638712
89       $batch =& batch_get();
90       $batch['progressive'] = FALSE;
91       batch_process();
92   }
93
94   /**
95    * Change a user's password.
96    *
97    * @param $password
98    */
99   public function password($password) {
100     $this->account->setPassword($password);
101     $this->account->save();
102   }
103
104   /**
105    * Build a one time login link.
106    *
107    * @param string $path
108    * @return string
109    */
110   public function passResetUrl($path = '') {
111     $url = user_pass_reset_url($this->account) . '/login';
112     if ($path) {
113       $url .= '?destination=' . $path;
114     }
115     return $url;
116   }
117
118   /**
119    * Get a user's name.
120    * @return string
121    */
122   public function getUsername() {
123     return $this->account->getUsername();
124   }
125
126   /**
127    * Return an id from a Drupal user account.
128    * @return int
129    */
130   public function id() {
131     return $this->account->id();
132   }
133 }