apiaccountdelete.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php declare(strict_types=1);
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Delete account
  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 API
  23. * @package GNUsocial
  24. * @author SENOO, Ken <develop@senooken.jp>
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link http://www.gnu.org/software/social/
  27. */
  28. if (!defined('GNUSOCIAL')) { exit(1); }
  29. /**
  30. * Delete a account with API.
  31. *
  32. * Refer to DeleteuserAction.
  33. */
  34. class ApiAccountDeleteAction extends ApiAuthAction
  35. {
  36. var $user = null;
  37. /**
  38. * Take arguments for running
  39. *
  40. * @param array $args $_REQUEST args
  41. *
  42. * @return boolean success flag
  43. */
  44. protected function prepare(array $args=array())
  45. {
  46. if (!parent::prepare($args)) {
  47. return false;
  48. }
  49. assert($this->scoped instanceof Profile);
  50. $profile = $this->getTargetProfile($this->arg('id'));
  51. if (empty($profile)) {
  52. // TRANS: Client error displayed when trying delete who's profile could not be found.
  53. throw new ClientException(_('Could not delete user: user not found.'), 403);
  54. }
  55. $this->user = $profile->getUser();
  56. if ($this->user->id === $this->scoped->id) {
  57. throw new ClientException('Could not delete self for mistake.', 403);
  58. }
  59. if (!$this->scoped->hasRight(Right::DELETEUSER)) {
  60. // TRANS: Client error displayed when trying to delete a user without having the right to delete users.
  61. throw new AuthorizationException(_('You cannot delete users.'));
  62. }
  63. // Only administrators can delete other privileged users (such as others who have the right to silence).
  64. if ($this->scoped->isPrivileged() && !$this->scoped->hasRole(Profile_role::ADMINISTRATOR)) {
  65. // TRANS: Client error displayed when trying to delete a user that has been granted moderation privileges
  66. throw new AuthorizationException(_('You cannot delete other privileged users.'));
  67. }
  68. return true;
  69. }
  70. /**
  71. * Handle the request
  72. *
  73. * @param array $args $_REQUEST data (unused)
  74. *
  75. * @return void
  76. */
  77. protected function handle()
  78. {
  79. parent::handle();
  80. if ($_SERVER['REQUEST_METHOD'] == 'DELETE') {
  81. $this->handleDelete();
  82. }
  83. }
  84. /**
  85. * Actually delete a user.
  86. *
  87. * @return void
  88. */
  89. function handleDelete()
  90. {
  91. if (Event::handle('StartDeleteUser', array($this, $this->user))) {
  92. // Mark the account as deleted and shove low-level deletion tasks
  93. // to background queues. Removing a lot of posts can take a while...
  94. if (!$this->user->hasRole(Profile_role::DELETED)) {
  95. $this->user->grantRole(Profile_role::DELETED);
  96. }
  97. $qm = QueueManager::get();
  98. $qm->enqueue($this->user, 'deluser');
  99. Event::handle('EndDeleteUser', array($this, $this->user));
  100. }
  101. }
  102. }