deleteuser.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Action class to delete a user
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Action
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL')) { exit(1); }
  30. /**
  31. * Delete a user
  32. *
  33. * @category Action
  34. * @package StatusNet
  35. * @author Evan Prodromou <evan@status.net>
  36. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  37. * @link http://status.net/
  38. */
  39. class DeleteuserAction extends ProfileFormAction
  40. {
  41. protected $user = null;
  42. public function prepare(array $args = []): bool
  43. {
  44. if (!parent::prepare($args)) {
  45. return false;
  46. }
  47. assert($this->scoped instanceof Profile);
  48. if (!$this->scoped->hasRight(Right::DELETEUSER)) {
  49. // TRANS: Client error displayed when trying to delete a user without having the right to delete users.
  50. throw new AuthorizationException(_('You cannot delete users.'));
  51. }
  52. try {
  53. $this->user = $this->profile->getUser();
  54. } catch (NoSuchUserException $e) {
  55. // TRANS: Client error displayed when trying to delete a non-local user.
  56. throw new ClientException(_('You can only delete local users.'));
  57. }
  58. // Only administrators can delete other privileged users (such as others who have the right to silence).
  59. if ($this->profile->isPrivileged() && !$this->scoped->hasRole(Profile_role::ADMINISTRATOR)) {
  60. // TRANS: Client error displayed when trying to delete a user that has been granted moderation privileges
  61. throw new AuthorizationException(_('You cannot delete other privileged users.'));
  62. }
  63. return true;
  64. }
  65. /**
  66. * Handle request
  67. *
  68. * Shows a page with list of favorite notices
  69. *
  70. * @param array $args $_REQUEST args; handled in prepare()
  71. *
  72. * @return void
  73. */
  74. function handle()
  75. {
  76. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  77. if ($this->arg('no')) {
  78. $this->returnToPrevious();
  79. return;
  80. }
  81. if ($this->arg('yes')) {
  82. $this->handlePost();
  83. $this->returnToPrevious();
  84. return;
  85. }
  86. $this->showPage();
  87. }
  88. }
  89. public function showContent(): void
  90. {
  91. $this->areYouSureForm();
  92. $block = new AccountProfileBlock($this, $this->profile);
  93. $block->show();
  94. }
  95. public function title(): string
  96. {
  97. // TRANS: Title of delete user page.
  98. return _m('TITLE','Delete user');
  99. }
  100. function showNoticeForm(): void
  101. {
  102. // nop
  103. }
  104. /**
  105. * Confirm with user.
  106. *
  107. * Shows a confirmation form.
  108. *
  109. * @return void
  110. */
  111. function areYouSureForm(): void
  112. {
  113. $id = $this->profile->id;
  114. $this->elementStart('form', array('id' => 'deleteuser-' . $id,
  115. 'method' => 'post',
  116. 'class' => 'form_settings form_entity_block',
  117. 'action' => common_local_url('deleteuser')));
  118. $this->elementStart('fieldset');
  119. $this->hidden('token', common_session_token());
  120. // TRANS: Fieldset legend on delete user page.
  121. $this->element('legend', _('Delete user'));
  122. if (Event::handle('StartDeleteUserForm', array($this, $this->user))) {
  123. $this->element('p', null,
  124. // TRANS: Information text to request if a user is certain that the described action has to be performed.
  125. _('Are you sure you want to delete this user? '.
  126. 'This will clear all data about the user from the '.
  127. 'database, without a backup.'));
  128. $this->element('input', array('id' => 'deleteuserto-' . $id,
  129. 'name' => 'profileid',
  130. 'type' => 'hidden',
  131. 'value' => $id));
  132. foreach ($this->args as $k => $v) {
  133. if (substr($k, 0, 9) == 'returnto-') {
  134. $this->hidden($k, $v);
  135. }
  136. }
  137. Event::handle('EndDeleteUserForm', array($this, $this->user));
  138. }
  139. $this->submit('form_action-no',
  140. // TRANS: Button label on the delete user form.
  141. _m('BUTTON','No'),
  142. 'submit form_action-primary',
  143. 'no',
  144. // TRANS: Submit button title for 'No' when deleting a user.
  145. _('Do not delete this user.'));
  146. $this->submit('form_action-yes',
  147. // TRANS: Button label on the delete user form.
  148. _m('BUTTON','Yes'),
  149. 'submit form_action-secondary',
  150. 'yes',
  151. // TRANS: Submit button title for 'Yes' when deleting a user.
  152. _('Delete this user.'));
  153. $this->elementEnd('fieldset');
  154. $this->elementEnd('form');
  155. }
  156. /**
  157. * Actually delete a user.
  158. *
  159. * @return void
  160. */
  161. function handlePost(): void
  162. {
  163. if (Event::handle('StartDeleteUser', array($this, $this->user))) {
  164. // Mark the account as deleted and shove low-level deletion tasks
  165. // to background queues. Removing a lot of posts can take a while...
  166. if (!$this->user->hasRole(Profile_role::DELETED)) {
  167. $this->user->grantRole(Profile_role::DELETED);
  168. }
  169. $qm = QueueManager::get();
  170. $qm->enqueue($this->user, 'deluser');
  171. Event::handle('EndDeleteUser', array($this, $this->user));
  172. }
  173. }
  174. }