user.drush.inc

  1. 8.0.x commands/user/user.drush.inc
  2. 6.x commands/user/user.drush.inc
  3. 7.x commands/user/user.drush.inc
  4. 4.x commands/user/user.drush.inc
  5. 5.x commands/user/user.drush.inc
  6. master commands/user/user.drush.inc

Drush User Management commands

Functions

Namesort descending Description
drush_user_add_role Add a role to the specified user accounts.
drush_user_block Block the specified user(s).
drush_user_cancel Cancels a user account.
drush_user_create Creates a new user account.
drush_user_information Prints information about the specified user(s).
drush_user_login Displays a one time login link for the given user.
drush_user_password Sets the password for the account with the given username
drush_user_remove_role Remove a role from the specified user accounts.
drush_user_unblock Unblock the specified user(s).
user_drush_command Implementation of hook_drush_command().
user_drush_help
user_drush_help_alter Implements hook_drush_help_alter().
_drush_user_get_uid Get uid(s) from a uid, user name, or email address. Returns a uid, or FALSE if none found.
_drush_user_get_users_from_arguments Given a comma-separated list of users, return uids for users that match either by uid or email address.
_drush_user_get_users_from_options_and_arguments Return the list of matching uids given
_drush_user_print_info Print information about a given uid

File

commands/user/user.drush.inc
View source
  1. <?php
  2. // $Id:
  3. /**
  4. * @file Drush User Management commands
  5. */
  6. function user_drush_help($section) {
  7. switch ($section) {
  8. case 'meta:user:title':
  9. return dt('User commands');
  10. case 'meta:user:summary':
  11. return dt('Add, modify and delete users.');
  12. }
  13. }
  14. /**
  15. * Implementation of hook_drush_command().
  16. */
  17. function user_drush_command() {
  18. $items['user-information'] = array(
  19. 'callback' => 'drush_user_information',
  20. 'description' => 'Print information about the specified user(s).',
  21. 'aliases' => array('uinf'),
  22. 'examples' => array(
  23. 'drush user-information 2,3,someguy,somegal,billgates@microsoft.com' =>
  24. 'Display information about any users with uids, names, or mail addresses matching the strings between commas.',
  25. ),
  26. 'arguments' => array(
  27. 'users' => 'A comma delimited list of uids, user names, or email addresses.',
  28. ),
  29. 'required-arguments' => TRUE,
  30. 'options' => array(
  31. 'full' => 'show extended information about the user',
  32. 'short' => 'show basic information about the user (this is the default)',
  33. ),
  34. );
  35. $items['user-block'] = array(
  36. 'callback' => 'drush_user_block',
  37. 'description' => 'Block the specified user(s).',
  38. 'aliases' => array('ublk'),
  39. 'arguments' => array(
  40. 'users' => 'A comma delimited list of uids, user names, or email addresses.',
  41. ),
  42. 'required-arguments' => TRUE,
  43. 'examples' => array(
  44. 'drush user-block 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
  45. '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',
  46. ),
  47. 'options' => array(
  48. 'uid' => 'A comma delimited list of uids to block',
  49. 'name' => 'A comma delimited list of user names to block',
  50. 'mail' => 'A comma delimited list of user mail addresses to block',
  51. ),
  52. );
  53. $items['user-unblock'] = array(
  54. 'callback' => 'drush_user_unblock',
  55. 'description' => 'Unblock the specified user(s).',
  56. 'aliases' => array('uublk'),
  57. 'arguments' => array(
  58. 'users' => 'A comma delimited list of uids, user names, or email addresses.',
  59. ),
  60. 'required-arguments' => TRUE,
  61. 'examples' => array(
  62. 'drush user-unblock 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
  63. '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',
  64. ),
  65. 'options' => array(
  66. 'uid' => 'A comma delimited list of uids to unblock',
  67. 'name' => 'A comma delimited list of user names to unblock',
  68. 'mail' => 'A comma delimited list of user mail addresses to unblock',
  69. ),
  70. );
  71. $items['user-add-role'] = array(
  72. 'callback' => 'drush_user_add_role',
  73. 'description' => 'Add a role to the specified user accounts.',
  74. 'aliases' => array('urol'),
  75. 'arguments' => array(
  76. 'role' => 'The name of the role to add',
  77. 'users' => '(optional) A comma delimited list of uids, user names, or email addresses.',
  78. ),
  79. 'required-arguments' => 1,
  80. 'examples' => array(
  81. 'drush user-add-role "power user" 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
  82. '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',
  83. ),
  84. 'options' => array(
  85. 'uid' => 'A comma delimited list of uids',
  86. 'name' => 'A comma delimited list of user names',
  87. 'mail' => 'A comma delimited list of user mail addresses',
  88. ),
  89. );
  90. $items['user-remove-role'] = array(
  91. 'callback' => 'drush_user_remove_role',
  92. 'description' => 'Remove a role from the specified user accounts.',
  93. 'aliases' => array('urrol'),
  94. 'arguments' => array(
  95. 'role' => 'The name of the role to remove',
  96. 'users' => '(optional) A comma delimited list of uids, user names, or email addresses.',
  97. ),
  98. 'required-arguments' => 1,
  99. 'examples' => array(
  100. 'drush user-remove-role "power user" 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
  101. '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',
  102. ),
  103. 'options' => array(
  104. 'uid' => 'A comma delimited list of uids',
  105. 'name' => 'A comma delimited list of user names',
  106. 'mail' => 'A comma delimited list of user mail addresses',
  107. ),
  108. );
  109. $items['user-create'] = array(
  110. 'callback' => 'drush_user_create',
  111. 'description' => 'Create a user account with the specified name.',
  112. 'aliases' => array('ucrt'),
  113. 'arguments' => array(
  114. 'name' => 'The name of the account to add'
  115. ),
  116. 'required-arguments' => TRUE,
  117. 'examples' => array(
  118. 'drush user-create newuser --mail="person@example.com" --password="letmein"' =>
  119. 'Create a new user account with the name newuser, the email address person@example.com, and the password letmein',
  120. ),
  121. 'options' => array(
  122. 'password' => 'The password for the new account',
  123. 'mail' => 'The email address for the new account',
  124. ),
  125. );
  126. $items['user-cancel'] = array(
  127. 'callback' => 'drush_user_cancel',
  128. 'description' => 'Cancel a user account with the specified name.',
  129. 'aliases' => array('ucan'),
  130. 'arguments' => array(
  131. 'name' => 'The name of the account to cancel',
  132. ),
  133. 'required-arguments' => TRUE,
  134. 'examples' => array(
  135. 'drush user-cancel username' =>
  136. 'Cancel the user account with the name username and anonymize all content created by that user.',
  137. ),
  138. );
  139. $items['user-password'] = array(
  140. 'callback' => 'drush_user_password',
  141. 'description' => '(Re)Set the password for the user account with the specified name.',
  142. 'aliases' => array('upwd'),
  143. 'arguments' => array(
  144. 'name' => 'The name of the account to modify.'
  145. ),
  146. 'required-arguments' => TRUE,
  147. 'options' => array(
  148. 'password' => array(
  149. 'description' => 'The new password for the account.',
  150. 'required' => TRUE,
  151. 'example-value' => 'foo',
  152. ),
  153. ),
  154. 'examples' => array(
  155. 'drush user-password someuser --password="correct horse battery staple"' =>
  156. 'Set the password for the username someuser. @see xkcd.com/936',
  157. ),
  158. );
  159. $items['user-login'] = array(
  160. 'callback' => 'drush_user_login',
  161. 'description' => 'Display a one time login link for the given user account (defaults to uid 1).',
  162. 'aliases' => array('uli'),
  163. 'arguments' => array(
  164. '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.',
  165. 'path' => 'Optional path to redirect to after logging in.',
  166. ),
  167. 'options' => array(
  168. 'browser' => 'Optional value denotes which browser to use (defaults to operating system default). Set to 0 to suppress opening a browser.',
  169. 'uid' => 'A uid to log in as.',
  170. 'name' => 'A user name to log in as.',
  171. 'mail' => 'A user mail address to log in as.',
  172. ),
  173. 'examples' => array(
  174. '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.',
  175. '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.',
  176. ),
  177. );
  178. return $items;
  179. }
  180. /**
  181. * Implements hook_drush_help_alter().
  182. */
  183. function user_drush_help_alter(&$command) {
  184. // Drupal 7+ only options.
  185. if ($command['command'] == 'user-cancel' && drush_drupal_major_version() >= 7) {
  186. $command['options']['delete-content'] = 'Delete all content created by the user';
  187. $command['examples']['drush user-cancel --delete-content username'] =
  188. 'Cancel the user account with the name username and delete all content created by that user.';
  189. }
  190. }
  191. /**
  192. * Prints information about the specified user(s).
  193. */
  194. function drush_user_information($users) {
  195. $uids = _drush_user_get_users_from_arguments($users);
  196. foreach($uids as $uid) {
  197. _drush_user_print_info($uid);
  198. }
  199. }
  200. /**
  201. * Block the specified user(s).
  202. */
  203. function drush_user_block($users = '') {
  204. $uids = _drush_user_get_users_from_options_and_arguments($users);
  205. if (!empty($uids)) {
  206. drush_op('user_user_operations_block', $uids);
  207. }
  208. else {
  209. return drush_set_error("Could not find any valid uids!");
  210. }
  211. }
  212. /**
  213. * Unblock the specified user(s).
  214. */
  215. function drush_user_unblock($users = '') {
  216. $uids = _drush_user_get_users_from_options_and_arguments($users);
  217. if (!empty($uids)) {
  218. drush_op('user_user_operations_unblock', $uids);
  219. }
  220. else {
  221. return drush_set_error("Could not find any valid uids!");
  222. }
  223. }
  224. /**
  225. * Add a role to the specified user accounts.
  226. */
  227. function drush_user_add_role($role, $users = '') {
  228. $uids = _drush_user_get_users_from_options_and_arguments($users);
  229. if (drush_drupal_major_version() >= 7) {
  230. $rid_query = db_query("SELECT rid FROM {role} WHERE name = :role", array(':role' => $role));
  231. }
  232. else {
  233. $rid_query = db_query("SELECT rid FROM {role} WHERE name = '%s'", $role);
  234. }
  235. if (!empty($uids)) {
  236. if ($rid = drush_db_result($rid_query)) {
  237. drush_op('user_multiple_role_edit', $uids, 'add_role', $rid);
  238. foreach($uids as $uid) {
  239. drush_log(dt("Added the !role role to uid !uid", array('!role' => $role, '!uid' => $uid)), 'success');
  240. }
  241. }
  242. else {
  243. return drush_set_error(dt("There is no role named: !role", array('!role' => $role)));
  244. }
  245. }
  246. else {
  247. return drush_set_error("Could not find any valid uids!");
  248. }
  249. }
  250. /**
  251. * Remove a role from the specified user accounts.
  252. */
  253. function drush_user_remove_role($role, $users = '') {
  254. $uids = _drush_user_get_users_from_options_and_arguments($users);
  255. if (drush_drupal_major_version() >= 7) {
  256. $rid_query = db_query("SELECT rid FROM {role} WHERE name = :role", array(':role' => $role));
  257. }
  258. else {
  259. $rid_query = db_query("SELECT rid FROM {role} WHERE name = '%s'", $role);
  260. }
  261. if (!empty($uids)) {
  262. if ($rid = drush_db_result($rid_query)) {
  263. drush_op('user_multiple_role_edit', $uids, 'remove_role', $rid);
  264. foreach($uids as $uid) {
  265. drush_log(dt("Removed the !role role from uid !uid", array('!role' => $role, '!uid' => $uid)), 'success');
  266. }
  267. }
  268. else {
  269. return drush_set_error(dt("There is no role named: !role", array('!role' => $role)));
  270. }
  271. }
  272. else {
  273. return drush_set_error("Could not find any valid uids!");
  274. }
  275. }
  276. /**
  277. * Creates a new user account.
  278. */
  279. function drush_user_create($name) {
  280. $mail = drush_get_option('mail');
  281. $pass = drush_get_option('password');
  282. $new_user = array(
  283. 'name' => $name,
  284. 'pass' => $pass,
  285. 'mail' => $mail,
  286. 'access' => '0',
  287. 'status' => 1,
  288. );
  289. if (drush_drupal_major_version() >= 7) {
  290. $result = db_query("SELECT uid FROM {users} WHERE name = :name OR mail = :mail", array(':name' => $name, ':mail' => $new_user['mail']));
  291. }
  292. else {
  293. $result = db_query("SELECT uid FROM {users} WHERE name = '%s' OR mail = '%s'", $name, $new_user['mail']);
  294. }
  295. if (drush_db_result($result) === FALSE) {
  296. if (!drush_get_context('DRUSH_SIMULATE')) {
  297. if (drush_drupal_major_version() >= 8) {
  298. $new_user_object = entity_create('user', $new_user);
  299. $new_user_object->save();
  300. }
  301. else {
  302. $new_user_object = user_save(NULL, $new_user, NULL);
  303. }
  304. if ($new_user_object !== FALSE) {
  305. _drush_user_print_info($new_user_object->uid);
  306. return $new_user_object->uid;
  307. }
  308. else {
  309. drush_set_error("Could not create a new user account with the name " . $name . "!");
  310. }
  311. }
  312. }
  313. else {
  314. drush_set_error("There is already a user account with the name " . $name . " or email address " . $new_user['mail'] . "!");
  315. }
  316. }
  317. /**
  318. * Cancels a user account.
  319. */
  320. function drush_user_cancel($name) {
  321. if (drush_drupal_major_version() >= 7) {
  322. $result = db_query("SELECT uid FROM {users} WHERE name = :name", array(':name' => $name));
  323. }
  324. else {
  325. $result = db_query("SELECT uid FROM {users} WHERE name = '%s'", $name);
  326. }
  327. $uid = drush_db_result($result);
  328. if ($uid !== FALSE) {
  329. drush_print("Cancelling the user account with the following information:");
  330. _drush_user_print_info($uid);
  331. if (drush_get_option('delete-content') && drush_drupal_major_version() >= 7) {
  332. drush_print("All content created by this user will be deleted!");
  333. }
  334. if (drush_confirm('Cancel user account?: ')) {
  335. if (drush_drupal_major_version() >= 7) {
  336. if (drush_get_option('delete-content')) {
  337. user_cancel(array(), $uid, 'user_cancel_delete');
  338. }
  339. else {
  340. user_cancel(array(), $uid, 'user_cancel_reassign');
  341. }
  342. // I got the following technique here: http://drupal.org/node/638712
  343. $batch =& batch_get();
  344. $batch['progressive'] = FALSE;
  345. batch_process();
  346. }
  347. else {
  348. user_delete(array(), $uid);
  349. }
  350. }
  351. }
  352. else {
  353. drush_set_error("Could not find a user account with the name " . $name . "!");
  354. }
  355. }
  356. /**
  357. * Sets the password for the account with the given username
  358. */
  359. function drush_user_password($name) {
  360. if (drush_drupal_major_version() >= 7) {
  361. $user = user_load_by_name($name);
  362. }
  363. else {
  364. $user = user_load(array('name' => $name));
  365. }
  366. if ($user !== FALSE) {
  367. if (!drush_get_context('DRUSH_SIMULATE')) {
  368. $pass = drush_get_option('password');
  369. // If no password has been provided, prompt for one.
  370. if (empty($pass)) $pass = drush_prompt(dt('Password'), NULL, TRUE, TRUE);
  371. if (drush_drupal_major_version() >= 8) {
  372. $user->pass = $pass;
  373. $user_object = $user->save();
  374. }
  375. else {
  376. $user_object = user_save($user, array('pass' => $pass));
  377. }
  378. if ($user_object === FALSE) {
  379. drush_set_error("Could not change the password for the user account with the name " . $name . "!");
  380. }
  381. }
  382. }
  383. else {
  384. drush_set_error("The user account with the name " . $name . " could not be loaded!");
  385. }
  386. }
  387. /**
  388. * Displays a one time login link for the given user.
  389. */
  390. function drush_user_login($user = NULL, $path = NULL) {
  391. $user_object = $uid = FALSE;
  392. $args = func_get_args();
  393. if (drush_get_option('uid', FALSE) || drush_get_option('name', FALSE) || drush_get_option('mail', FALSE)) {
  394. // One of the user options was passed, so we prefer that to the user
  395. // argument.
  396. $user = NULL;
  397. // If we only have a single argument and one of the user options is passed,
  398. // then we assume the argument is the path to open.
  399. if (count($args) == 1) {
  400. $path = $args[0];
  401. }
  402. }
  403. else if (empty($user)) {
  404. // No user option or argument was passed, so we default to uid 1.
  405. $uid = 1;
  406. }
  407. // Try and load a user from provided options and arguments.
  408. if ($uid || $uid = reset(_drush_user_get_users_from_options_and_arguments($user))) {
  409. $user_object = user_load($uid);
  410. }
  411. if ($user_object !== FALSE && $user_object->status) {
  412. $options = array();
  413. if ($path) {
  414. $options['query']['destination'] = $path;
  415. }
  416. $link = url(user_pass_reset_url($user_object) . '/login', $options);
  417. drush_start_browser($link);
  418. drush_print($link);
  419. return $link;
  420. }
  421. else {
  422. drush_set_error("The user account could not be loaded or is blocked!");
  423. }
  424. }
  425. /**
  426. * Print information about a given uid
  427. */
  428. function _drush_user_print_info($uid) {
  429. if (drush_drupal_major_version() >= 7) {
  430. $userinfo = user_load($uid);
  431. }
  432. else {
  433. $userinfo = user_load(array('uid' => $uid));
  434. }
  435. if (drush_get_option('full')) {
  436. $userinfo = (array)$userinfo;
  437. $userinfo_pipe = array();
  438. unset($userinfo['data']);
  439. unset($userinfo['block']);
  440. unset($userinfo['form_build_id']);
  441. foreach($userinfo as $key => $val) {
  442. if (is_array($val)) {
  443. drush_print($key . ': ');
  444. drush_print_r($val);
  445. $userinfo_pipe[] = '"' . implode(",", $val) . '"';
  446. }
  447. else {
  448. if ($key === 'created' OR $key === 'access' OR $key === 'login') {
  449. drush_print($key . ': ' . format_date($val));
  450. $userinfo_pipe[] = $val;
  451. }
  452. else {
  453. drush_print($key . ': ' . $val);
  454. $userinfo_pipe[] = $val;
  455. }
  456. }
  457. }
  458. drush_print_pipe(implode(",", $userinfo_pipe));
  459. drush_print_pipe("\n");
  460. }
  461. else {
  462. $userinfo_short = array(
  463. 'User ID' => $userinfo->uid,
  464. 'User name' => $userinfo->name,
  465. 'User mail' => $userinfo->mail,
  466. );
  467. $userinfo_short['User roles'] = implode(', ', $userinfo->roles);
  468. $userinfo->status ? $userinfo_short['User status'] = 'active' : $userinfo_short['User status'] = 'blocked';
  469. drush_print_table(drush_key_value_to_array_table($userinfo_short));
  470. drush_print_pipe("$userinfo->name,$userinfo->uid,$userinfo->mail,$userinfo->status,\"" . implode(',', $userinfo->roles) . "\"\n");
  471. }
  472. }
  473. /**
  474. * Given a comma-separated list of users, return uids
  475. * for users that match either by uid or email address.
  476. */
  477. function _drush_user_get_users_from_arguments($users) {
  478. $uids = array();
  479. if ($users !== '') {
  480. $users = explode(',', $users);
  481. foreach($users as $user) {
  482. $uid = _drush_user_get_uid($user);
  483. if ($uid !== FALSE) {
  484. $uids[] = $uid;
  485. }
  486. }
  487. }
  488. return $uids;
  489. }
  490. /**
  491. * Return the list of matching uids given
  492. */
  493. function _drush_user_get_users_from_options_and_arguments($users) {
  494. $uids = drush_get_option_list('uids');
  495. foreach (array('uid', 'name', 'mail' ) as $user_attr) {
  496. if ($arg = drush_get_option($user_attr)) {
  497. foreach(explode(',', $arg) as $search) {
  498. $uid_query = FALSE;
  499. switch ($user_attr) {
  500. case 'uid':
  501. if (drush_drupal_major_version() >= 7) {
  502. $uid_query = db_query("SELECT uid FROM {users} WHERE uid = :uid", array(':uid' => $search));
  503. }
  504. else {
  505. $uid_query = db_query("SELECT uid FROM {users} WHERE uid = %d", $search);
  506. }
  507. break;
  508. case 'name':
  509. if (drush_drupal_major_version() >= 7) {
  510. $uid_query = db_query("SELECT uid FROM {users} WHERE name = :name", array(':name' => $search));
  511. }
  512. else {
  513. $uid_query = db_query("SELECT uid FROM {users} WHERE name = '%s'", $search);
  514. }
  515. break;
  516. case 'mail':
  517. if (drush_drupal_major_version() >= 7) {
  518. $uid_query = db_query("SELECT uid FROM {users} WHERE mail = :mail", array(':mail' => $search));
  519. }
  520. else {
  521. $uid_query = db_query("SELECT uid FROM {users} WHERE mail = '%s'", $search);
  522. }
  523. break;
  524. }
  525. if ($uid_query !== FALSE) {
  526. if ($uid = drush_db_result($uid_query)) {
  527. $uids[] = $uid;
  528. }
  529. else {
  530. drush_set_error("Could not find a uid for $user_attr = $search");
  531. }
  532. }
  533. }
  534. }
  535. }
  536. return array_merge($uids, _drush_user_get_users_from_arguments($users));
  537. }
  538. /**
  539. * Get uid(s) from a uid, user name, or email address.
  540. * Returns a uid, or FALSE if none found.
  541. */
  542. function _drush_user_get_uid($search) {
  543. // We use a DB query while looking for the uid to keep things speedy.
  544. $uids = array();
  545. if (is_numeric($search)) {
  546. if (drush_drupal_major_version() >= 7) {
  547. $uid_query = db_query("SELECT uid, name FROM {users} WHERE uid = :uid OR name = :name", array(':uid' => $search, ':name' => $search));
  548. }
  549. else {
  550. $uid_query = db_query("SELECT uid, name FROM {users} WHERE uid = %d OR name = '%d'", $search, $search);
  551. }
  552. }
  553. else {
  554. if (drush_drupal_major_version() >= 7) {
  555. $uid_query = db_query("SELECT uid, name FROM {users} WHERE mail = :mail OR name = :name", array(':mail' => $search, ':name' => $search));
  556. }
  557. else {
  558. $uid_query = db_query("SELECT uid, name FROM {users} WHERE mail = '%s' OR name = '%s'", $search, $search);
  559. }
  560. }
  561. while ($uid = drush_db_fetch_object($uid_query)) {
  562. $uids[$uid->uid] = $uid->name;
  563. }
  564. switch (count($uids)) {
  565. case 0:
  566. return drush_set_error("Could not find a uid for the search term '" . $search . "'!");
  567. break;
  568. case 1:
  569. return array_pop(array_keys($uids));
  570. break;
  571. default:
  572. drush_print('More than one user account was found for the search string "' . $search . '".');
  573. return(drush_choice($uids, 'Please choose a name:', '!value (uid=!key)'));
  574. }
  575. }