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_usersingle_get_class Get a version specific UserSingle class.
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_create_validate
drush_user_get_class Get a version specific User class.
drush_user_information Command callback. 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().

File

commands/user/user.drush.inc
View source
  1. <?php
  2. use Drush\Log\LogLevel;
  3. use Drush\User\UserList;
  4. use Drush\User\UserListException;
  5. /**
  6. * @file
  7. * Drush User Management commands
  8. */
  9. function user_drush_help($section) {
  10. switch ($section) {
  11. case 'meta:user:title':
  12. return dt('User commands');
  13. case 'meta:user:summary':
  14. return dt('Add, modify and delete users.');
  15. }
  16. }
  17. /**
  18. * Implementation of hook_drush_command().
  19. */
  20. function user_drush_command() {
  21. $options_common = array(
  22. 'uid' => array(
  23. 'description' => 'A comma delimited list of uids of users to operate on.',
  24. 'example-value' => '3,5',
  25. 'value' => 'required',
  26. ),
  27. 'name' => array(
  28. 'description' => 'A comma delimited list of user names of users to operate on.',
  29. 'example-value' => 'foo',
  30. 'value' => 'required',
  31. ),
  32. 'mail' => array(
  33. 'description' => 'A comma delimited list of user mail addresses of users to operate on.',
  34. 'example-value' => 'me@example.com',
  35. 'value' => 'required',
  36. )
  37. );
  38. $items['user-information'] = array(
  39. 'description' => 'Print information about the specified user(s).',
  40. 'aliases' => array('uinf'),
  41. 'examples' => array(
  42. 'drush user-information 2,3,someguy,somegal,billgates@microsoft.com' =>
  43. 'Display information about the listed users.',
  44. ),
  45. 'arguments' => array(
  46. 'users' => 'A comma delimited list of uids, user names, or email addresses.',
  47. ),
  48. 'required-arguments' => TRUE,
  49. 'outputformat' => array(
  50. 'default' => 'key-value-list',
  51. 'pipe-format' => 'csv',
  52. 'field-labels' => array(
  53. 'uid' => 'User ID',
  54. 'name' => 'User name',
  55. 'pass' => 'Password',
  56. 'mail' => 'User mail',
  57. 'theme' => 'User theme',
  58. 'signature' => 'Signature',
  59. 'signature_format' => 'Signature format',
  60. 'user_created' => 'User created',
  61. 'created' => 'Created',
  62. 'user_access' => 'User last access',
  63. 'access' => 'Last access',
  64. 'user_login' => 'User last login',
  65. 'login' => 'Last login',
  66. 'user_status' => 'User status',
  67. 'status' => 'Status',
  68. 'timezone' => 'Time zone',
  69. 'picture' => 'User picture',
  70. 'init' => 'Initial user mail',
  71. 'roles' => 'User roles',
  72. 'group_audience' => 'Group Audience',
  73. 'langcode' => 'Language code',
  74. 'uuid' => 'Uuid',
  75. ),
  76. 'format-cell' => 'csv',
  77. 'fields-default' => array('uid', 'name', 'mail', 'roles', 'user_status'),
  78. 'fields-pipe' => array('name', 'uid', 'mail', 'status', 'roles'),
  79. 'fields-full' => array('uid', 'name', 'pass', 'mail', 'theme', 'signature', 'user_created', 'user_access', 'user_login', 'user_status', 'timezone', 'roles', 'group_audience', 'langcode', 'uuid'),
  80. 'output-data-type' => 'format-table',
  81. ),
  82. );
  83. $items['user-block'] = array(
  84. 'description' => 'Block the specified user(s).',
  85. 'aliases' => array('ublk'),
  86. 'arguments' => array(
  87. 'users' => 'A comma delimited list of uids, user names, or email addresses.',
  88. ),
  89. 'examples' => array(
  90. 'drush user-block 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
  91. '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',
  92. ),
  93. 'options' => $options_common,
  94. );
  95. $items['user-unblock'] = array(
  96. 'description' => 'Unblock the specified user(s).',
  97. 'aliases' => array('uublk'),
  98. 'arguments' => array(
  99. 'users' => 'A comma delimited list of uids, user names, or email addresses.',
  100. ),
  101. 'examples' => array(
  102. 'drush user-unblock 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
  103. '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',
  104. ),
  105. 'options' => $options_common,
  106. );
  107. $items['user-add-role'] = array(
  108. 'description' => 'Add a role to the specified user accounts.',
  109. 'aliases' => array('urol'),
  110. 'arguments' => array(
  111. 'role' => 'The name of the role to add',
  112. 'users' => '(optional) A comma delimited list of uids, user names, or email addresses.',
  113. ),
  114. 'required-arguments' => 1,
  115. 'examples' => array(
  116. 'drush user-add-role "power user" 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
  117. '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',
  118. ),
  119. 'options' => $options_common,
  120. );
  121. $items['user-remove-role'] = array(
  122. 'description' => 'Remove a role from the specified user accounts.',
  123. 'aliases' => array('urrol'),
  124. 'arguments' => array(
  125. 'role' => 'The name of the role to remove',
  126. 'users' => '(optional) A comma delimited list of uids, user names, or email addresses.',
  127. ),
  128. 'required-arguments' => 1,
  129. 'examples' => array(
  130. 'drush user-remove-role "power user" 5,user3 --uid=2,3 --name=someguy,somegal --mail=billgates@microsoft.com' =>
  131. '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',
  132. ),
  133. 'options' => $options_common,
  134. );
  135. $items['user-create'] = array(
  136. 'description' => 'Create a user account with the specified name.',
  137. 'aliases' => array('ucrt'),
  138. 'arguments' => array(
  139. 'name' => 'The name of the account to add'
  140. ),
  141. 'required-arguments' => TRUE,
  142. 'examples' => array(
  143. 'drush user-create newuser --mail="person@example.com" --password="letmein"' =>
  144. 'Create a new user account with the name newuser, the email address person@example.com, and the password letmein',
  145. ),
  146. 'options' => array(
  147. 'password' => 'The password for the new account',
  148. 'mail' => 'The email address for the new account',
  149. ),
  150. 'outputformat' => $items['user-information']['outputformat'],
  151. );
  152. $items['user-cancel'] = array(
  153. 'description' => 'Cancel a user account with the specified name.',
  154. 'aliases' => array('ucan'),
  155. 'arguments' => array(
  156. 'name' => 'The name of the account to cancel',
  157. ),
  158. // The `_user_cancel` method still references global $user.
  159. // @todo remove once https://www.drupal.org/node/2163205 is in.
  160. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN,
  161. 'required-arguments' => TRUE,
  162. 'examples' => array(
  163. 'drush user-cancel username' =>
  164. 'Cancel the user account with the name username and anonymize all content created by that user.',
  165. ),
  166. );
  167. $items['user-password'] = array(
  168. 'description' => '(Re)Set the password for the user account with the specified name.',
  169. 'aliases' => array('upwd'),
  170. 'arguments' => array(
  171. 'name' => 'The name of the account to modify.'
  172. ),
  173. 'required-arguments' => TRUE,
  174. 'options' => array(
  175. 'password' => array(
  176. 'description' => 'The new password for the account.',
  177. 'required' => TRUE,
  178. 'example-value' => 'foo',
  179. ),
  180. ),
  181. 'examples' => array(
  182. 'drush user-password someuser --password="correct horse battery staple"' =>
  183. 'Set the password for the username someuser. @see xkcd.com/936',
  184. ),
  185. );
  186. $items['user-login'] = array(
  187. 'description' => 'Display a one time login link for the given user account (defaults to uid 1).',
  188. 'aliases' => array('uli'),
  189. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  190. 'handle-remote-commands' => TRUE,
  191. 'arguments' => array(
  192. '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.',
  193. 'path' => 'Optional path to redirect to after logging in.',
  194. ),
  195. 'options' => array(
  196. 'browser' => 'Optional value denotes which browser to use (defaults to operating system default). Use --no-browser to suppress opening a browser.',
  197. 'uid' => 'A uid to log in as.',
  198. 'redirect-port' => 'A custom port for redirecting to (e.g. when running within a Vagrant environment)',
  199. 'name' => 'A user name to log in as.',
  200. 'mail' => 'A user mail address to log in as.',
  201. ),
  202. 'examples' => array(
  203. '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.',
  204. '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.',
  205. ),
  206. );
  207. return $items;
  208. }
  209. /**
  210. * Implements hook_drush_help_alter().
  211. */
  212. function user_drush_help_alter(&$command) {
  213. // Drupal 7+ only options.
  214. if ($command['command'] == 'user-cancel' && drush_drupal_major_version() >= 7) {
  215. $command['options']['delete-content'] = 'Delete all content created by the user';
  216. $command['examples']['drush user-cancel --delete-content username'] =
  217. 'Cancel the user account with the name username and delete all content created by that user.';
  218. }
  219. }
  220. /**
  221. * Get a version specific UserSingle class.
  222. *
  223. * @param $account
  224. * @return \Drush\User\UserSingleBase
  225. *
  226. * @see drush_get_class().
  227. */
  228. function drush_usersingle_get_class($account) {
  229. return drush_get_class('Drush\User\UserSingle', array($account));
  230. }
  231. /**
  232. * Get a version specific User class.
  233. *
  234. * @return \Drush\User\UserVersion
  235. *
  236. * @see drush_get_class().
  237. */
  238. function drush_user_get_class() {
  239. return drush_get_class('Drush\User\User');
  240. }
  241. /**
  242. * Command callback. Prints information about the specified user(s).
  243. */
  244. function drush_user_information($users) {
  245. $userlist = new UserList($users);
  246. $info = $userlist->each('info');
  247. return $info;
  248. }
  249. /**
  250. * Block the specified user(s).
  251. */
  252. function drush_user_block($users = '') {
  253. $userlist = new UserList($users);
  254. $userlist->each('block');
  255. drush_log(dt('Blocked user(s): !users', array('!users' => $userlist->names())), LogLevel::SUCCESS);
  256. }
  257. /**
  258. * Unblock the specified user(s).
  259. */
  260. function drush_user_unblock($users = '') {
  261. $userlist = new UserList($users);
  262. $userlist->each('unblock');
  263. drush_log(dt('Unblocked user(s): !users', array('!users' => $userlist->names())), LogLevel::SUCCESS);
  264. }
  265. /**
  266. * Add a role to the specified user accounts.
  267. */
  268. function drush_user_add_role($role, $users = '') {
  269. // If role is not found, an exception gets thrown and handled by command invoke.
  270. $role_object = drush_role_get_class($role);
  271. $userlist = new UserList($users);
  272. $userlist->each('addRole', array($role_object->rid));
  273. drush_log(dt('Added role !role role to !users', array('!role' => $role, '!users' => $userlist->names())),LogLevel::SUCCESS);
  274. }
  275. /**
  276. * Remove a role from the specified user accounts.
  277. */
  278. function drush_user_remove_role($role, $users = '') {
  279. // If role is not found, an exception gets thrown and handled by command invoke.
  280. $role_object = drush_role_get_class($role);
  281. $userlist = new UserList($users);
  282. $userlist->each('removeRole', array($role_object->rid));
  283. drush_log(dt('Removed !role role from !users', array('!role' => $role, '!users' => $userlist->names())),LogLevel::SUCCESS);
  284. }
  285. /**
  286. * Creates a new user account.
  287. */
  288. function drush_user_create($name) {
  289. $userversion = drush_user_get_class();
  290. $mail = drush_get_option('mail');
  291. $pass = drush_get_option('password');
  292. $new_user = array(
  293. 'name' => $name,
  294. 'pass' => $pass,
  295. 'mail' => $mail,
  296. 'access' => '0',
  297. 'status' => 1,
  298. );
  299. if (!drush_get_context('DRUSH_SIMULATE')) {
  300. if ($account = $userversion->create($new_user)) {
  301. return array($account->id() => $account->info());
  302. }
  303. else {
  304. return drush_set_error("Could not create a new user account with the name " . $name . ".");
  305. }
  306. }
  307. }
  308. function drush_user_create_validate($name) {
  309. $userversion = drush_user_get_class();
  310. if ($mail = drush_get_option('mail')) {
  311. if ($userversion->load_by_mail($mail)) {
  312. return drush_set_error(dt('There is already a user account with the email !mail', array('!mail' => $mail)));
  313. }
  314. }
  315. if ($userversion->load_by_name($name)) {
  316. return drush_set_error(dt('There is already a user account with the name !name', array('!name' => $name)));
  317. }
  318. }
  319. /**
  320. * Cancels a user account.
  321. */
  322. function drush_user_cancel($inputs) {
  323. $userlist = new UserList($inputs);
  324. foreach ($userlist->accounts as $account) {
  325. if (drush_get_option('delete-content') && drush_drupal_major_version() >= 7) {
  326. drush_print(dt('All content created by !name will be deleted.', array('!name' => $account->getUsername())));
  327. }
  328. if (drush_confirm('Cancel user account?: ')) {
  329. $account->cancel();
  330. }
  331. }
  332. drush_log(dt('Cancelled user(s): !users', array('!users' => $userlist->names())),LogLevel::SUCCESS);
  333. }
  334. /**
  335. * Sets the password for the account with the given username
  336. */
  337. function drush_user_password($inputs) {
  338. $userlist = new UserList($inputs);
  339. if (!drush_get_context('DRUSH_SIMULATE')) {
  340. $pass = drush_get_option('password');
  341. // If no password has been provided, prompt for one.
  342. if (empty($pass)) {
  343. $pass = drush_prompt(dt('Password'), NULL, TRUE, TRUE);
  344. }
  345. foreach ($userlist->accounts as $account) {
  346. $userlist->each('password', array($pass));
  347. }
  348. drush_log(dt('Changed password for !users', array('!users' => $userlist->names())), LogLevel::SUCCESS);
  349. }
  350. }
  351. /**
  352. * Displays a one time login link for the given user.
  353. */
  354. function drush_user_login($inputs = '', $path = NULL) {
  355. $args = func_get_args();
  356. // Redispatch if called against a remote-host so a browser is started on the
  357. // the *local* machine.
  358. $alias = drush_get_context('DRUSH_TARGET_SITE_ALIAS');
  359. if (drush_sitealias_is_remote_site($alias)) {
  360. $return = drush_invoke_process($alias, 'user-login', $args, drush_redispatch_get_options(), array('integrate' => FALSE));
  361. if ($return['error_status']) {
  362. return drush_set_error('Unable to execute user login.');
  363. }
  364. else {
  365. // Prior versions of Drupal returned a string so cast to an array if needed.
  366. $links = is_string($return['object']) ? array($return['object']) : $return['object'];
  367. }
  368. }
  369. else {
  370. if (!drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  371. // Fail gracefully if unable to bootstrap Drupal.
  372. // drush_bootstrap() has already logged an error.
  373. return FALSE;
  374. }
  375. if (drush_get_option('uid', FALSE) || drush_get_option('name', FALSE) || drush_get_option('mail', FALSE)) {
  376. // If we only have a single argument and one of the user options is passed,
  377. // then we assume the argument is the path to open.
  378. if (count($args) == 1) {
  379. $path = $args[0];
  380. }
  381. }
  382. // Try to load a user from provided options and arguments.
  383. try {
  384. $userlist = new UserList($inputs);
  385. }
  386. catch (UserListException $e) {
  387. // No user option or argument was passed, so we default to uid 1.
  388. $userlist = new UserList(1);
  389. }
  390. $links = $userlist->each('passResetUrl', array($path));
  391. }
  392. $port = drush_get_option('redirect-port', FALSE);
  393. // There is almost always only one link so pick the first one for display and browser.
  394. // The full array is sent on backend calls.
  395. $first = current($links);
  396. drush_start_browser($first, FALSE, $port);
  397. drush_backend_set_result($links);
  398. return $first;
  399. }