7adad5272cda190f831c67f152ef351b3d11e952
[yaffs-website] / vendor / drush / drush / commands / user / user.drush.inc
1 <?php
2
3 use Drush\Log\LogLevel;
4 use Drush\User\UserList;
5 use Drush\User\UserListException;
6
7 /**
8  * @file
9  * Drush User Management commands
10  */
11
12 function user_drush_help($section) {
13   switch ($section) {
14     case 'meta:user:title':
15       return dt('User commands');
16     case 'meta:user:summary':
17       return dt('Add, modify and delete users.');
18   }
19 }
20
21 /**
22  * Implementation of hook_drush_command().
23  */
24 function user_drush_command() {
25   $options_common = array(
26     'uid' => array(
27       'description' => 'A comma delimited list of uids of users to operate on.',
28       'example-value' => '3,5',
29       'value' => 'required',
30     ),
31     'name' => array(
32       'description' => 'A comma delimited list of user names of users to operate on.',
33       'example-value' => 'foo',
34       'value' => 'required',
35     ),
36     'mail' => array(
37       'description' => 'A comma delimited list of user mail addresses of users to operate on.',
38       'example-value' => 'me@example.com',
39       'value' => 'required',
40     )
41   );
42
43   $items['user-information'] = array(
44     'description' => 'Print information about the specified user(s).',
45     'aliases' => array('uinf'),
46     'examples' => array(
47       'drush user-information 2,3,someguy,somegal,billgates@microsoft.com' =>
48         'Display information about the listed users.',
49     ),
50     'arguments' => array(
51       'users' => 'A comma delimited list of uids, user names, or email addresses.',
52     ),
53     'required-arguments' => TRUE,
54     'outputformat' => array(
55       'default' => 'key-value-list',
56       'pipe-format' => 'csv',
57       'field-labels' => array(
58         'uid' => 'User ID',
59         'name' => 'User name',
60         'pass' => 'Password',
61         'mail' => 'User mail',
62         'theme' => 'User theme',
63         'signature' => 'Signature',
64         'signature_format' => 'Signature format',
65         'user_created' => 'User created',
66         'created' => 'Created',
67         'user_access' => 'User last access',
68         'access' => 'Last access',
69         'user_login' => 'User last login',
70         'login' => 'Last login',
71         'user_status' => 'User status',
72         'status' => 'Status',
73         'timezone' => 'Time zone',
74         'picture' => 'User picture',
75         'init' => 'Initial user mail',
76         'roles' => 'User roles',
77         'group_audience' => 'Group Audience',
78         'langcode' => 'Language code',
79         'uuid' => 'Uuid',
80       ),
81       'format-cell' => 'csv',
82       'fields-default' => array('uid', 'name', 'mail', 'roles', 'user_status'),
83       'fields-pipe' => array('name', 'uid', 'mail', 'status', 'roles'),
84       'fields-full' => array('uid', 'name', 'pass', 'mail', 'theme', 'signature', 'user_created', 'user_access', 'user_login', 'user_status', 'timezone', 'roles', 'group_audience', 'langcode', 'uuid'),
85       'output-data-type' => 'format-table',
86     ),
87   );
88   $items['user-block'] = array(
89     'description' => 'Block the specified user(s).',
90     'aliases' => array('ublk'),
91     'arguments' => array(
92       'users' => 'A comma delimited list of uids, user names, or email addresses.',
93     ),
94     'examples' => array(
95       'drush user-block 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
96         'Block the users with name, id, or email 5 or user3, uids 2 and 3, names someguy and somegal, and email address of billgates@microsoft.com',
97     ),
98     'options' => $options_common,
99   );
100   $items['user-unblock'] = array(
101     'description' => 'Unblock the specified user(s).',
102     'aliases' => array('uublk'),
103     'arguments' => array(
104       'users' => 'A comma delimited list of uids, user names, or email addresses.',
105     ),
106     'examples' => array(
107       'drush user-unblock 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
108         'Unblock the users with name, id, or email 5 or user3, uids 2 and 3, names someguy and somegal, and email address of billgates@microsoft.com',
109     ),
110     'options' => $options_common,
111   );
112   $items['user-add-role'] = array(
113     'description' => 'Add a role to the specified user accounts.',
114     'aliases' => array('urol'),
115     'arguments' => array(
116       'role' => 'The name of the role to add',
117       'users' => '(optional) A comma delimited list of uids, user names, or email addresses.',
118     ),
119     'required-arguments' => 1,
120     'examples' => array(
121       'drush user-add-role "power user" 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
122         'Add the "power user" role to the accounts with name, id, or email 5 or user3, uids 2 and 3, names someguy and somegal, and email address of billgates@microsoft.com',
123     ),
124     'options' => $options_common,
125   );
126   $items['user-remove-role'] = array(
127     'description' => 'Remove a role from the specified user accounts.',
128     'aliases' => array('urrol'),
129     'arguments' => array(
130       'role' => 'The name of the role to remove',
131       'users' => '(optional) A comma delimited list of uids, user names, or email addresses.',
132     ),
133     'required-arguments' => 1,
134     'examples' => array(
135       'drush user-remove-role "power user" 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
136         'Remove the "power user" role from the accounts with name, id, or email 5 or user3, uids 2 and 3, names someguy and somegal, and email address of billgates@microsoft.com',
137     ),
138     'options' => $options_common,
139   );
140   $items['user-create'] = array(
141     'description' => 'Create a user account with the specified name.',
142     'aliases' => array('ucrt'),
143     'arguments' => array(
144       'name' => 'The name of the account to add'
145     ),
146     'required-arguments' => TRUE,
147     'examples' => array(
148       'drush user-create newuser --mail="person@example.com" --password="letmein"' =>
149         'Create a new user account with the name newuser, the email address person@example.com, and the password letmein',
150     ),
151     'options' => array(
152       'password' => 'The password for the new account',
153       'mail' => 'The email address for the new account',
154     ),
155     'outputformat' => $items['user-information']['outputformat'],
156   );
157   $items['user-cancel'] = array(
158     'description' => 'Cancel a user account with the specified name.',
159     'aliases' => array('ucan'),
160     'arguments' => array(
161       'name' => 'The name of the account to cancel',
162     ),
163     // The `_user_cancel` method still references global $user.
164     // @todo remove once https://www.drupal.org/node/2163205 is in.
165     'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
166     'required-arguments' => TRUE,
167     'examples' => array(
168       'drush user-cancel username' =>
169         'Cancel the user account with the name username and anonymize all content created by that user.',
170     ),
171   );
172   $items['user-password'] = array(
173     'description' => '(Re)Set the password for the user account with the specified name.',
174     'aliases' => array('upwd'),
175     'arguments' => array(
176       'name' => 'The name of the account to modify.'
177     ),
178     'required-arguments' => TRUE,
179     'options' => array(
180       'password' => array(
181         'description' => 'The new password for the account.',
182         'required' => TRUE,
183         'example-value' => 'foo',
184       ),
185     ),
186     'examples' => array(
187       'drush user-password someuser --password="correct horse battery staple"' =>
188         'Set the password for the username someuser. @see xkcd.com/936',
189     ),
190   );
191   $items['user-login'] = array(
192     'description' => 'Display a one time login link for the given user account (defaults to uid 1).',
193     'aliases' => array('uli'),
194     'bootstrap' => DRUSH_BOOTSTRAP_NONE,
195     'handle-remote-commands' => TRUE,
196     'arguments' => array(
197       'user' => 'An optional uid, user name, or email address for the user to log in as. Default is to log in as uid 1. The uid/name/mail options take priority if specified.',
198       'path' => 'Optional path to redirect to after logging in.',
199     ),
200     'options' => array(
201       'browser' => 'Optional value denotes which browser to use (defaults to operating system default). Use --no-browser to suppress opening a browser.',
202       'uid' => 'A uid to log in as.',
203       'redirect-port' => 'A custom port for redirecting to (e.g. when running within a Vagrant environment)',
204       'name' => 'A user name to log in as.',
205       'mail' => 'A user mail address to log in as.',
206     ),
207     'examples' => array(
208       'drush user-login ryan node/add/blog' => 'Displays and opens default web browser (if configured or detected) for a one-time login link for the user with the username ryan and redirect to the path node/add/blog.',
209       'drush user-login --browser=firefox --mail=drush@example.org admin/settings/performance' => 'Open firefox web browser, login as the user with the e-mail address drush@example.org and redirect to the path admin/settings/performance.',
210     ),
211   );
212   return $items;
213 }
214
215 /**
216  * Implements hook_drush_help_alter().
217  */
218 function user_drush_help_alter(&$command) {
219   // Drupal 7+ only options.
220   if ($command['command'] == 'user-cancel' && drush_drupal_major_version() >= 7) {
221     $command['options']['delete-content'] = 'Delete all content created by the user';
222     $command['examples']['drush user-cancel --delete-content username'] =
223       'Cancel the user account with the name username and delete all content created by that user.';
224   }
225 }
226
227 /**
228  * Get a version specific UserSingle class.
229  *
230  * @param $account
231  * @return \Drush\User\UserSingleBase
232  *
233  * @see drush_get_class().
234  */
235 function drush_usersingle_get_class($account) {
236   return drush_get_class('Drush\User\UserSingle', array($account));
237 }
238
239 /**
240  * Get a version specific User class.
241  *
242  * @return \Drush\User\UserVersion
243  *
244  * @see drush_get_class().
245  */
246 function drush_user_get_class() {
247   return drush_get_class('Drush\User\User');
248 }
249
250 /**
251  * Command callback. Prints information about the specified user(s).
252  */
253 function drush_user_information($users) {
254   $userlist = new UserList($users);
255   $info = $userlist->each('info');
256   return $info;
257 }
258
259 /**
260  * Block the specified user(s).
261  */
262 function drush_user_block($users = '') {
263   $userlist = new UserList($users);
264   $userlist->each('block');
265   drush_log(dt('Blocked user(s): !users', array('!users' => $userlist->names())), LogLevel::SUCCESS);
266 }
267
268 /**
269  * Unblock the specified user(s).
270  */
271 function drush_user_unblock($users = '') {
272   $userlist = new UserList($users);
273   $userlist->each('unblock');
274   drush_log(dt('Unblocked user(s): !users', array('!users' => $userlist->names())), LogLevel::SUCCESS);
275 }
276
277 /**
278  * Add a role to the specified user accounts.
279  */
280 function drush_user_add_role($role, $users = '') {
281   // If role is not found, an exception gets thrown and handled by command invoke.
282   $role_object = drush_role_get_class($role);
283   $userlist = new UserList($users);
284   $userlist->each('addRole', array($role_object->rid));
285   drush_log(dt('Added role !role role to !users', array('!role' => $role, '!users' => $userlist->names())),LogLevel::SUCCESS);
286 }
287
288 /**
289  * Remove a role from the specified user accounts.
290  */
291 function drush_user_remove_role($role, $users = '') {
292   // If role is not found, an exception gets thrown and handled by command invoke.
293   $role_object = drush_role_get_class($role);
294   $userlist = new UserList($users);
295   $userlist->each('removeRole', array($role_object->rid));
296   drush_log(dt('Removed !role role from !users', array('!role' => $role, '!users' => $userlist->names())),LogLevel::SUCCESS);
297 }
298
299 /**
300  * Creates a new user account.
301  */
302 function drush_user_create($name) {
303   $userversion = drush_user_get_class();
304   $mail = drush_get_option('mail');
305   $pass = drush_get_option('password');
306   $new_user = array(
307     'name' => $name,
308     'pass' => $pass,
309     'mail' => $mail,
310     'access' => '0',
311     'status' => 1,
312   );
313   if (!drush_get_context('DRUSH_SIMULATE')) {
314     if ($account = $userversion->create($new_user)) {
315       return array($account->id() => $account->info());
316     }
317     else {
318       return drush_set_error("Could not create a new user account with the name " . $name . ".");
319     }
320   }
321 }
322
323 function drush_user_create_validate($name) {
324   $userversion = drush_user_get_class();
325   if ($mail = drush_get_option('mail')) {
326     if ($userversion->load_by_mail($mail)) {
327       return drush_set_error(dt('There is already a user account with the email !mail', array('!mail' => $mail)));
328     }
329   }
330   if ($userversion->load_by_name($name)) {
331     return drush_set_error(dt('There is already a user account with the name !name', array('!name' => $name)));
332   }
333 }
334
335 /**
336  * Cancels a user account.
337  */
338 function drush_user_cancel($inputs) {
339   $userlist = new UserList($inputs);
340   foreach ($userlist->accounts as $account) {
341     if (drush_get_option('delete-content') && drush_drupal_major_version() >= 7) {
342       drush_print(dt('All content created by !name will be deleted.', array('!name' => $account->getUsername())));
343     }
344     if (drush_confirm('Cancel user account?: ')) {
345       $account->cancel();
346     }
347   }
348   drush_log(dt('Cancelled user(s): !users', array('!users' => $userlist->names())),LogLevel::SUCCESS);
349 }
350
351 /**
352  * Sets the password for the account with the given username
353  */
354 function drush_user_password($inputs) {
355   $userlist = new UserList($inputs);
356   if (!drush_get_context('DRUSH_SIMULATE')) {
357     $pass = drush_get_option('password');
358     // If no password has been provided, prompt for one.
359     if (empty($pass)) {
360       $pass = drush_prompt(dt('Password'), NULL, TRUE, TRUE);
361     }
362     foreach ($userlist->accounts as $account) {
363       $userlist->each('password', array($pass));
364     }
365     drush_log(dt('Changed password for !users', array('!users' => $userlist->names())), LogLevel::SUCCESS);
366   }
367 }
368
369 /**
370  * Displays a one time login link for the given user.
371  */
372 function drush_user_login($inputs = '', $path = NULL) {
373   $args = func_get_args();
374
375   // Redispatch if called against a remote-host so a browser is started on the
376   // the *local* machine.
377   $alias = drush_get_context('DRUSH_TARGET_SITE_ALIAS');
378   if (drush_sitealias_is_remote_site($alias)) {
379     $return = drush_invoke_process($alias, 'user-login', $args, drush_redispatch_get_options(), array('integrate' => FALSE));
380     if ($return['error_status']) {
381       return drush_set_error('Unable to execute user login.');
382     }
383     else {
384       // Prior versions of Drupal returned a string so cast to an array if needed.
385       $links = is_string($return['object']) ? array($return['object']) : $return['object'];
386     }
387   }
388   else {
389     if (!drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
390       // Fail gracefully if unable to bootstrap Drupal.
391       // drush_bootstrap() has already logged an error.
392       return FALSE;
393     }
394
395     if (drush_get_option('uid', FALSE) || drush_get_option('name', FALSE) || drush_get_option('mail', FALSE)) {
396       // If we only have a single argument and one of the user options is passed,
397       // then we assume the argument is the path to open.
398       if (count($args) == 1) {
399         $path = $args[0];
400       }
401     }
402
403     // Try to load a user from provided options and arguments.
404     try {
405       $userlist = new UserList($inputs);
406     }
407     catch (UserListException $e) {
408       // No user option or argument was passed, so we default to uid 1.
409         $userlist = new UserList(1);
410     }
411     $links = $userlist->each('passResetUrl', array($path));
412   }
413   $port = drush_get_option('redirect-port', FALSE);
414   // There is almost always only one link so pick the first one for display and browser.
415   // The full array is sent on backend calls.
416   $first = current($links);
417   drush_start_browser($first, FALSE, $port);
418   drush_backend_set_result($links);
419   return $first;
420 }