deleteuser.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. var $user = null;
  42. function prepare(array $args=array())
  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. } elseif ($this->arg('yes')) {
  80. $this->handlePost();
  81. $this->returnToPrevious();
  82. } else {
  83. $this->showPage();
  84. }
  85. }
  86. }
  87. function showContent() {
  88. $this->areYouSureForm();
  89. $block = new AccountProfileBlock($this, $this->profile);
  90. $block->show();
  91. }
  92. function title() {
  93. // TRANS: Title of delete user page.
  94. return _m('TITLE','Delete user');
  95. }
  96. function showNoticeForm() {
  97. // nop
  98. }
  99. /**
  100. * Confirm with user.
  101. *
  102. * Shows a confirmation form.
  103. *
  104. * @return void
  105. */
  106. function areYouSureForm()
  107. {
  108. $id = $this->profile->id;
  109. $this->elementStart('form', array('id' => 'deleteuser-' . $id,
  110. 'method' => 'post',
  111. 'class' => 'form_settings form_entity_block',
  112. 'action' => common_local_url('deleteuser')));
  113. $this->elementStart('fieldset');
  114. $this->hidden('token', common_session_token());
  115. // TRANS: Fieldset legend on delete user page.
  116. $this->element('legend', _('Delete user'));
  117. if (Event::handle('StartDeleteUserForm', array($this, $this->user))) {
  118. $this->element('p', null,
  119. // TRANS: Information text to request if a user is certain that the described action has to be performed.
  120. _('Are you sure you want to delete this user? '.
  121. 'This will clear all data about the user from the '.
  122. 'database, without a backup.'));
  123. $this->element('input', array('id' => 'deleteuserto-' . $id,
  124. 'name' => 'profileid',
  125. 'type' => 'hidden',
  126. 'value' => $id));
  127. foreach ($this->args as $k => $v) {
  128. if (substr($k, 0, 9) == 'returnto-') {
  129. $this->hidden($k, $v);
  130. }
  131. }
  132. Event::handle('EndDeleteUserForm', array($this, $this->user));
  133. }
  134. $this->submit('form_action-no',
  135. // TRANS: Button label on the delete user form.
  136. _m('BUTTON','No'),
  137. 'submit form_action-primary',
  138. 'no',
  139. // TRANS: Submit button title for 'No' when deleting a user.
  140. _('Do not delete this user.'));
  141. $this->submit('form_action-yes',
  142. // TRANS: Button label on the delete user form.
  143. _m('BUTTON','Yes'),
  144. 'submit form_action-secondary',
  145. 'yes',
  146. // TRANS: Submit button title for 'Yes' when deleting a user.
  147. _('Delete this user.'));
  148. $this->elementEnd('fieldset');
  149. $this->elementEnd('form');
  150. }
  151. /**
  152. * Actually delete a user.
  153. *
  154. * @return void
  155. */
  156. function handlePost()
  157. {
  158. if (Event::handle('StartDeleteUser', array($this, $this->user))) {
  159. // Mark the account as deleted and shove low-level deletion tasks
  160. // to background queues. Removing a lot of posts can take a while...
  161. if (!$this->user->hasRole(Profile_role::DELETED)) {
  162. $this->user->grantRole(Profile_role::DELETED);
  163. }
  164. $qm = QueueManager::get();
  165. $qm->enqueue($this->user, 'deluser');
  166. Event::handle('EndDeleteUser', array($this, $this->user));
  167. }
  168. }
  169. }