/aegir/patches/user.drush.inc.patch

https://github.com/pcolemonts/nginx-for-drupal · Patch · 551 lines · 551 code · 0 blank · 0 comment · 0 complexity · fb85c7ceae3679e44e2e415b069ff606 MD5 · raw file

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