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