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 ascending Description
_drush_user_print_info Print information about a given uid
_drush_user_info
_drush_user_get_users_from_options_and_arguments Return the list of matching uids given
_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_uid Get uid(s) from a uid, user name, or email address. Returns a uid, or FALSE if none found.
user_drush_help_alter Implements hook_drush_help_alter().
user_drush_help
user_drush_command Implementation of hook_drush_command().
drush_user_unblock Unblock the specified user(s).
drush_user_remove_role Remove a role from the specified user accounts.
drush_user_password Sets the password for the account with the given username
drush_user_login Displays a one time login link for the given user.
drush_user_information Prints information about the specified user(s).
drush_user_create Creates a new user account.
drush_user_cancel Cancels a user account.
drush_user_block Block the specified user(s).
drush_user_add_role Add a role to the specified user accounts.

File

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