UserSingleBase.php

  1. 8.0.x lib/Drush/User/UserSingleBase.php
  2. 7.x lib/Drush/User/UserSingleBase.php
  3. master lib/Drush/User/UserSingleBase.php

Namespace

Drush\User

Classes

Namesort descending Description
UserSingleBase

File

lib/Drush/User/UserSingleBase.php
View source
  1. <?php
  2. namespace Drush\User;
  3. abstract class UserSingleBase {
  4. // A Drupal user entity.
  5. public $account;
  6. public function __construct($account) {
  7. $this->account = $account;
  8. }
  9. /**
  10. * A flatter and simpler array presentation of a Drupal $user object.
  11. *
  12. * @return array
  13. */
  14. public function info() {
  15. return array(
  16. 'uid' => $this->account->id(),
  17. 'name' => $this->account->getUsername(),
  18. 'password' => $this->account->getPassword(),
  19. 'mail' => $this->account->getEmail(),
  20. 'user_created' => $this->account->getCreatedTime(),
  21. 'created' => format_date($this->account->getCreatedTime()),
  22. 'user_access' => $this->account->getLastAccessedTime(),
  23. 'access' => format_date($this->account->getLastAccessedTime()),
  24. 'user_login' => $this->account->getLastLoginTime(),
  25. 'login' => format_date($this->account->getLastLoginTime()),
  26. 'user_status' => $this->account->get('status')->value,
  27. 'status' => $this->account->isActive() ? 'active' : 'blocked',
  28. 'timezone' => $this->account->getTimeZone(),
  29. 'roles' => $this->account->getRoles(),
  30. 'langcode' => $this->account->getPreferredLangcode(),
  31. 'uuid' => $this->account->uuid->value,
  32. );
  33. }
  34. /**
  35. * Block a user from login.
  36. */
  37. public function block() {
  38. $this->account->block();
  39. $this->account->save();
  40. }
  41. /**
  42. * Unblock a user from login.
  43. */
  44. public function unblock() {
  45. $this->account->get('status')->value = 1;
  46. $this->account->save();
  47. }
  48. /**
  49. * Add a role to the current user.
  50. *
  51. * @param $rid
  52. * A role ID.
  53. */
  54. public function addRole($rid) {
  55. $this->account->addRole($rid);
  56. $this->account->save();
  57. }
  58. /**
  59. * Remove a role from the current user.
  60. *
  61. * @param $rid
  62. * A role ID.
  63. */
  64. public function removeRole($rid) {
  65. $this->account->removeRole($rid);
  66. $this->account->save();
  67. }
  68. /**
  69. * Block a user and remove or reassign their content.
  70. */
  71. public function cancel() {
  72. if (drush_get_option('delete-content')) {
  73. user_cancel(array(), $this->id(), 'user_cancel_delete');
  74. }
  75. else {
  76. user_cancel(array(), $this->id(), 'user_cancel_reassign');
  77. }
  78. // I got the following technique here: http://drupal.org/node/638712
  79. $batch =& batch_get();
  80. $batch['progressive'] = FALSE;
  81. batch_process();
  82. }
  83. /**
  84. * Change a user's password.
  85. *
  86. * @param $password
  87. */
  88. public function password($password) {
  89. $this->account->setPassword($password);
  90. $this->account->save();
  91. }
  92. /**
  93. * Build a one time login link.
  94. *
  95. * @param string $path
  96. * @return string
  97. */
  98. public function passResetUrl($path = '') {
  99. $url = user_pass_reset_url($this->account);
  100. if ($path) {
  101. $url .= '?destination=' . $path;
  102. }
  103. return $url;
  104. }
  105. /**
  106. * Get a user's name.
  107. * @return string
  108. */
  109. public function getUsername() {
  110. return $this->account->getUsername();
  111. }
  112. /**
  113. * Return an id from a Drupal user account.
  114. * @return int
  115. */
  116. public function id() {
  117. return $this->account->id();
  118. }
  119. }