makeadmin.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Make another user an admin of a group
  18. *
  19. * @category Action
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2008, 2009 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Make another user an admin of a group
  28. *
  29. * @copyright 2008, 2009 StatusNet, Inc.
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class MakeadminAction extends RedirectingAction
  33. {
  34. public $profile = null;
  35. public $group = null;
  36. /**
  37. * Take arguments for running
  38. *
  39. * @param array $args $_REQUEST args
  40. *
  41. * @return boolean success flag
  42. */
  43. public function prepare(array $args = [])
  44. {
  45. parent::prepare($args);
  46. if (!common_logged_in()) {
  47. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  48. $this->clientError(_('Not logged in.'));
  49. }
  50. $token = $this->trimmed('token');
  51. if (empty($token) || $token != common_session_token()) {
  52. // TRANS: Client error displayed when the session token does not match or is not given.
  53. $this->clientError(_('There was a problem with your session token. Try again, please.'));
  54. }
  55. $id = $this->trimmed('profileid');
  56. if (empty($id)) {
  57. // TRANS: Client error displayed when not providing a profile ID on the Make Admin page.
  58. $this->clientError(_('No profile specified.'));
  59. }
  60. $this->profile = Profile::getKV('id', $id);
  61. if (empty($this->profile)) {
  62. // TRANS: Client error displayed when specifying an invalid profile ID on the Make Admin page.
  63. $this->clientError(_('No profile with that ID.'));
  64. }
  65. $group_id = $this->trimmed('groupid');
  66. if (empty($group_id)) {
  67. // TRANS: Client error displayed when not providing a group ID on the Make Admin page.
  68. $this->clientError(_('No group specified.'));
  69. }
  70. $this->group = User_group::getKV('id', $group_id);
  71. if (empty($this->group)) {
  72. // TRANS: Client error displayed when providing an invalid group ID on the Make Admin page.
  73. $this->clientError(_('No such group.'));
  74. }
  75. $user = common_current_user();
  76. if (!$user->isAdmin($this->group) &&
  77. !$user->hasRight(Right::MAKEGROUPADMIN)) {
  78. // TRANS: Client error displayed when trying to make another user admin on the Make Admin page while not an admin.
  79. $this->clientError(_('Only an admin can make another user an admin.'), 401);
  80. }
  81. if ($this->profile->isAdmin($this->group)) {
  82. // TRANS: Client error displayed when trying to make another user admin on the Make Admin page who already is admin.
  83. // TRANS: %1$s is the user that is already admin, %2$s is the group user is already admin for.
  84. $this->clientError(
  85. sprintf(
  86. _('%1$s is already an admin for group "%2$s".'),
  87. $this->profile->getBestName(),
  88. $this->group->getBestName()
  89. ),
  90. 401
  91. );
  92. }
  93. return true;
  94. }
  95. /**
  96. * Handle request
  97. *
  98. * @param array $args $_REQUEST args; handled in prepare()
  99. *
  100. * @return void
  101. */
  102. public function handle()
  103. {
  104. parent::handle();
  105. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  106. $this->makeAdmin();
  107. }
  108. }
  109. /**
  110. * Make user an admin
  111. *
  112. * @return void
  113. */
  114. public function makeAdmin()
  115. {
  116. $member = Group_member::pkeyGet(array('group_id' => $this->group->id,
  117. 'profile_id' => $this->profile->id));
  118. if (empty($member)) {
  119. // TRANS: Server error displayed when trying to make another user admin on the Make Admin page fails
  120. // TRANS: because the group membership record could not be gotten.
  121. // TRANS: %1$s is the to be admin user, %2$s is the group user should be admin for.
  122. $this->serverError(
  123. _('Can\'t get membership record for %1$s in group %2$s.'),
  124. $this->profile->getBestName(),
  125. $this->group->getBestName()
  126. );
  127. }
  128. $orig = clone($member);
  129. $member->is_admin = true;
  130. $result = $member->update($orig);
  131. if (!$result) {
  132. common_log_db_error($member, 'UPDATE', __FILE__);
  133. // TRANS: Server error displayed when trying to make another user admin on the Make Admin page fails
  134. // TRANS: because the group adminship record coud not be saved properly.
  135. // TRANS: %1$s is the to be admin user, %2$s is the group user is already admin for.
  136. $this->serverError(
  137. _('Can\'t make %1$s an admin for group %2$s.'),
  138. $this->profile->getBestName(),
  139. $this->group->getBestName()
  140. );
  141. }
  142. $this->returnToPrevious();
  143. }
  144. /**
  145. * If we reached this form without returnto arguments, default to
  146. * the top of the group's member list.
  147. *
  148. * @return string URL
  149. */
  150. public function defaultReturnTo()
  151. {
  152. return common_local_url(
  153. 'groupmembers',
  154. ['nickname' => $this->group->nickname]
  155. );
  156. }
  157. }