3 use Drush\Log\LogLevel;
4 use Drush\User\UserList;
5 use Drush\User\UserListException;
9 * Drush User Management commands
12 function user_drush_help($section) {
14 case 'meta:user:title':
15 return dt('User commands');
16 case 'meta:user:summary':
17 return dt('Add, modify and delete users.');
22 * Implementation of hook_drush_command().
24 function user_drush_command() {
25 $options_common = array(
27 'description' => 'A comma delimited list of uids of users to operate on.',
28 'example-value' => '3,5',
29 'value' => 'required',
32 'description' => 'A comma delimited list of user names of users to operate on.',
33 'example-value' => 'foo',
34 'value' => 'required',
37 'description' => 'A comma delimited list of user mail addresses of users to operate on.',
38 'example-value' => 'me@example.com',
39 'value' => 'required',
43 $items['user-information'] = array(
44 'description' => 'Print information about the specified user(s).',
45 'aliases' => array('uinf'),
47 'drush user-information 2,3,someguy,somegal,billgates@microsoft.com' =>
48 'Display information about the listed users.',
51 'users' => 'A comma delimited list of uids, user names, or email addresses.',
53 'required-arguments' => TRUE,
54 'outputformat' => array(
55 'default' => 'key-value-list',
56 'pipe-format' => 'csv',
57 'field-labels' => array(
59 'name' => 'User name',
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',
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',
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',
88 $items['user-block'] = array(
89 'description' => 'Block the specified user(s).',
90 'aliases' => array('ublk'),
92 'users' => 'A comma delimited list of uids, user names, or email addresses.',
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',
98 'options' => $options_common,
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.',
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',
110 'options' => $options_common,
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.',
119 'required-arguments' => 1,
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',
124 'options' => $options_common,
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.',
133 'required-arguments' => 1,
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',
138 'options' => $options_common,
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'
146 'required-arguments' => TRUE,
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',
152 'password' => 'The password for the new account',
153 'mail' => 'The email address for the new account',
155 'outputformat' => $items['user-information']['outputformat'],
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',
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,
168 'drush user-cancel username' =>
169 'Cancel the user account with the name username and anonymize all content created by that user.',
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.'
178 'required-arguments' => TRUE,
181 'description' => 'The new password for the account.',
183 'example-value' => 'foo',
187 'drush user-password someuser --password="correct horse battery staple"' =>
188 'Set the password for the username someuser. @see xkcd.com/936',
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.',
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.',
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.',
216 * Implements hook_drush_help_alter().
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.';
228 * Get a version specific UserSingle class.
231 * @return \Drush\User\UserSingleBase
233 * @see drush_get_class().
235 function drush_usersingle_get_class($account) {
236 return drush_get_class('Drush\User\UserSingle', array($account));
240 * Get a version specific User class.
242 * @return \Drush\User\UserVersion
244 * @see drush_get_class().
246 function drush_user_get_class() {
247 return drush_get_class('Drush\User\User');
251 * Command callback. Prints information about the specified user(s).
253 function drush_user_information($users) {
254 $userlist = new UserList($users);
255 $info = $userlist->each('info');
260 * Block the specified user(s).
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);
269 * Unblock the specified user(s).
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);
278 * Add a role to the specified user accounts.
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);
289 * Remove a role from the specified user accounts.
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);
300 * Creates a new user account.
302 function drush_user_create($name) {
303 $userversion = drush_user_get_class();
304 $mail = drush_get_option('mail');
305 $pass = drush_get_option('password');
313 if (!drush_get_context('DRUSH_SIMULATE')) {
314 if ($account = $userversion->create($new_user)) {
315 return array($account->id() => $account->info());
318 return drush_set_error("Could not create a new user account with the name " . $name . ".");
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)));
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)));
336 * Cancels a user account.
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())));
344 if (drush_confirm('Cancel user account?: ')) {
348 drush_log(dt('Cancelled user(s): !users', array('!users' => $userlist->names())),LogLevel::SUCCESS);
352 * Sets the password for the account with the given username
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.
360 $pass = drush_prompt(dt('Password'), NULL, TRUE, TRUE);
362 foreach ($userlist->accounts as $account) {
363 $userlist->each('password', array($pass));
365 drush_log(dt('Changed password for !users', array('!users' => $userlist->names())), LogLevel::SUCCESS);
370 * Displays a one time login link for the given user.
372 function drush_user_login($inputs = '', $path = NULL) {
373 $args = func_get_args();
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.');
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'];
389 if (!drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
390 // Fail gracefully if unable to bootstrap Drupal.
391 // drush_bootstrap() has already logged an error.
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) {
403 // Try to load a user from provided options and arguments.
405 $userlist = new UserList($inputs);
407 catch (UserListException $e) {
408 // No user option or argument was passed, so we default to uid 1.
409 $userlist = new UserList(1);
411 $links = $userlist->each('passResetUrl', array($path));
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);