groupblock.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * Block a user from a group action class.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Action
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  11. * @link http://status.net/
  12. *
  13. * StatusNet - the distributed open-source microblogging tool
  14. * Copyright (C) 2008, 2009, StatusNet, Inc.
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. if (!defined('STATUSNET') && !defined('LACONICA')) {
  30. exit(1);
  31. }
  32. /**
  33. * Block a user from a group
  34. *
  35. * @category Action
  36. * @package StatusNet
  37. * @author Evan Prodromou <evan@status.net>
  38. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  39. * @link http://status.net/
  40. */
  41. class GroupblockAction extends RedirectingAction
  42. {
  43. var $profile = null;
  44. var $group = null;
  45. /**
  46. * Take arguments for running
  47. *
  48. * @param array $args $_REQUEST args
  49. *
  50. * @return boolean success flag
  51. */
  52. function prepare(array $args = array())
  53. {
  54. parent::prepare($args);
  55. if (!common_logged_in()) {
  56. // TRANS: Error message displayed when trying to perform an action that requires a logged in user.
  57. $this->clientError(_('Not logged in.'));
  58. }
  59. $token = $this->trimmed('token');
  60. if (empty($token) || $token != common_session_token()) {
  61. // TRANS: Client error displayed when the session token does not match or is not given.
  62. $this->clientError(_('There was a problem with your session token. Try again, please.'));
  63. }
  64. $id = $this->trimmed('blockto');
  65. if (empty($id)) {
  66. // TRANS: Client error displayed trying to block a user from a group while not specifying a to be blocked user profile.
  67. $this->clientError(_('No profile specified.'));
  68. }
  69. $this->profile = Profile::getKV('id', $id);
  70. if (empty($this->profile)) {
  71. // TRANS: Client error displayed trying to block a user from a group while specifying a non-existing profile.
  72. $this->clientError(_('No profile with that ID.'));
  73. }
  74. $group_id = $this->trimmed('blockgroup');
  75. if (empty($group_id)) {
  76. // TRANS: Client error displayed trying to block a user from a group while not specifying a group to block a profile from.
  77. $this->clientError(_('No group specified.'));
  78. }
  79. $this->group = User_group::getKV('id', $group_id);
  80. if (empty($this->group)) {
  81. // TRANS: Client error displayed trying to block a user from a group while specifying a non-existing group.
  82. $this->clientError(_('No such group.'));
  83. }
  84. $user = common_current_user();
  85. if (!$user->isAdmin($this->group)) {
  86. // TRANS: Client error displayed trying to block a user from a group while not being an admin user.
  87. $this->clientError(_('Only an admin can block group members.'), 401);
  88. }
  89. if (Group_block::isBlocked($this->group, $this->profile)) {
  90. // TRANS: Client error displayed trying to block a user from a group while user is already blocked from the given group.
  91. $this->clientError(_('User is already blocked from group.'));
  92. }
  93. // XXX: could have proactive blocks, but we don't have UI for it.
  94. if (!$this->profile->isMember($this->group)) {
  95. // TRANS: Client error displayed trying to block a user from a group while user is not a member of given group.
  96. $this->clientError(_('User is not a member of group.'));
  97. }
  98. return true;
  99. }
  100. /**
  101. * Handle request
  102. *
  103. * Shows a page with list of favorite notices
  104. *
  105. * @param array $args $_REQUEST args; handled in prepare()
  106. *
  107. * @return void
  108. */
  109. function handle()
  110. {
  111. parent::handle();
  112. if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  113. if ($this->arg('no')) {
  114. $this->returnToPrevious();
  115. } elseif ($this->arg('yes')) {
  116. $this->blockProfile();
  117. } elseif ($this->arg('blockto')) {
  118. $this->showPage();
  119. }
  120. }
  121. }
  122. function showContent() {
  123. $this->areYouSureForm();
  124. }
  125. function title() {
  126. // TRANS: Title for block user from group page.
  127. return _('Block user from group');
  128. }
  129. function showNoticeForm() {
  130. // nop
  131. }
  132. /**
  133. * Confirm with user.
  134. *
  135. * Shows a confirmation form.
  136. *
  137. * @return void
  138. */
  139. function areYouSureForm()
  140. {
  141. $id = $this->profile->id;
  142. $this->elementStart('form', array('id' => 'block-' . $id,
  143. 'method' => 'post',
  144. 'class' => 'form_settings form_entity_block',
  145. 'action' => common_local_url('groupblock')));
  146. $this->elementStart('fieldset');
  147. $this->hidden('token', common_session_token());
  148. // TRANS: Fieldset legend for block user from group form.
  149. $this->element('legend', _('Block user'));
  150. $this->element('p', null,
  151. // TRANS: Explanatory text for block user from group form before setting the block.
  152. // TRANS: %1$s is that to be blocked user, %2$s is the group the user will be blocked from.
  153. sprintf(_('Are you sure you want to block user "%1$s" from the group "%2$s"? '.
  154. 'They will be removed from the group, unable to post, and '.
  155. 'unable to subscribe to the group in the future.'),
  156. $this->profile->getBestName(),
  157. $this->group->getBestName()));
  158. $this->hidden('blockto-' . $this->profile->id,
  159. $this->profile->id,
  160. 'blockto');
  161. $this->hidden('blockgroup-' . $this->group->id,
  162. $this->group->id,
  163. 'blockgroup');
  164. foreach ($this->args as $k => $v) {
  165. if (substr($k, 0, 9) == 'returnto-') {
  166. $this->hidden($k, $v);
  167. }
  168. }
  169. $this->submit('form_action-no',
  170. // TRANS: Button label on the form to block a user from a group.
  171. _m('BUTTON','No'),
  172. 'submit form_action-primary',
  173. 'no',
  174. // TRANS: Submit button title for 'No' when blocking a user from a group.
  175. _('Do not block this user from this group.'));
  176. $this->submit('form_action-yes',
  177. // TRANS: Button label on the form to block a user from a group.
  178. _m('BUTTON','Yes'),
  179. 'submit form_action-secondary',
  180. 'yes',
  181. // TRANS: Submit button title for 'Yes' when blocking a user from a group.
  182. _('Block this user from this group.'));
  183. $this->elementEnd('fieldset');
  184. $this->elementEnd('form');
  185. }
  186. /**
  187. * Actually block a user.
  188. *
  189. * @return void
  190. */
  191. function blockProfile()
  192. {
  193. $block = Group_block::blockProfile($this->group, $this->profile,
  194. common_current_user());
  195. if (empty($block)) {
  196. // TRANS: Server error displayed when trying to block a user from a group fails because of an application error.
  197. $this->serverError(_("Database error blocking user from group."));
  198. }
  199. $this->returnToPrevious();
  200. }
  201. /**
  202. * If we reached this form without returnto arguments, default to
  203. * the top of the group's member list.
  204. *
  205. * @return string URL
  206. */
  207. function defaultReturnTo()
  208. {
  209. return common_local_url('groupmembers',
  210. array('nickname' => $this->group->nickname));
  211. }
  212. function showScripts()
  213. {
  214. parent::showScripts();
  215. $this->autofocus('form_action-yes');
  216. }
  217. }