Group.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Group\Controller;
  20. use App\Core\ActorLocalRoles;
  21. use App\Core\Cache;
  22. use App\Core\Controller;
  23. use App\Core\DB\DB;
  24. use App\Core\Form;
  25. use function App\Core\I18n\_m;
  26. use App\Core\Log;
  27. use App\Entity as E;
  28. use App\Util\Common;
  29. use App\Util\Exception\ClientException;
  30. use App\Util\Exception\DuplicateFoundException;
  31. use App\Util\Exception\NicknameEmptyException;
  32. use App\Util\Exception\NicknameException;
  33. use App\Util\Exception\NicknameInvalidException;
  34. use App\Util\Exception\NicknameNotAllowedException;
  35. use App\Util\Exception\NicknameTakenException;
  36. use App\Util\Exception\NicknameTooLongException;
  37. use App\Util\Exception\NotFoundException;
  38. use App\Util\Exception\RedirectException;
  39. use App\Util\Exception\ServerException;
  40. use App\Util\Form\ActorForms;
  41. use App\Util\Nickname;
  42. use Component\Group\Entity\GroupMember;
  43. use Component\Group\Entity\LocalGroup;
  44. use Component\Subscription\Entity\ActorSubscription;
  45. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  46. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  47. use Symfony\Component\Form\Extension\Core\Type\TextType;
  48. use Symfony\Component\Form\FormInterface;
  49. use Symfony\Component\HttpFoundation\Request;
  50. class Group extends Controller
  51. {
  52. /**
  53. * Page that allows an actor to create a new group
  54. *
  55. * @throws RedirectException
  56. * @throws ServerException
  57. */
  58. public function groupCreate(Request $request): array
  59. {
  60. if (\is_null($actor = Common::actor())) {
  61. throw new RedirectException('security_login');
  62. }
  63. $create_form = self::getGroupCreateForm($request, $actor);
  64. return [
  65. '_template' => 'group/create.html.twig',
  66. 'create_form' => $create_form->createView(),
  67. ];
  68. }
  69. /**
  70. * Settings page for the group with the provided nickname, checks if the current actor can administrate given group
  71. *
  72. * @throws ClientException
  73. * @throws DuplicateFoundException
  74. * @throws NicknameEmptyException
  75. * @throws NicknameException
  76. * @throws NicknameInvalidException
  77. * @throws NicknameNotAllowedException
  78. * @throws NicknameTakenException
  79. * @throws NicknameTooLongException
  80. * @throws NotFoundException
  81. * @throws ServerException
  82. */
  83. public function groupSettings(Request $request, int $id): array
  84. {
  85. $local_group = DB::findOneBy(LocalGroup::class, ['actor_id' => $id]);
  86. $group_actor = $local_group->getActor();
  87. $actor = Common::actor();
  88. if (!\is_null($group_actor) && $actor->canModerate($group_actor)) {
  89. return [
  90. '_template' => 'group/settings.html.twig',
  91. 'group' => $group_actor,
  92. 'personal_info_form' => ActorForms::personalInfo(request: $request, scope: $actor, target: $group_actor)->createView(),
  93. 'open_details_query' => $this->string('open'),
  94. ];
  95. } else {
  96. throw new ClientException(_m('You do not have permission to edit settings for the group "{group}"', ['{group}' => $id]), code: 404);
  97. }
  98. }
  99. /**
  100. * Create a new Group FormInterface getter
  101. *
  102. * @throws RedirectException
  103. * @throws ServerException
  104. */
  105. public static function getGroupCreateForm(Request $request, E\Actor $actor): FormInterface
  106. {
  107. $create_form = Form::create([
  108. ['group_nickname', TextType::class, ['label' => _m('Group nickname')]],
  109. ['group_type', ChoiceType::class, ['label' => _m('Type:'), 'multiple' => false, 'expanded' => false, 'choices' => [
  110. _m('Group') => 'group',
  111. _m('Organisation') => 'organisation',
  112. ]]],
  113. ['group_scope', ChoiceType::class, ['label' => _m('Is this a private group:'), 'multiple' => false, 'expanded' => false, 'choices' => [
  114. _m('No') => 'public',
  115. _m('Yes') => 'private',
  116. ]]],
  117. ['group_create', SubmitType::class, ['label' => _m('Create this group!')]],
  118. ]);
  119. $create_form->handleRequest($request);
  120. if ($create_form->isSubmitted() && $create_form->isValid()) {
  121. $data = $create_form->getData();
  122. $nickname = Nickname::normalize(
  123. nickname: $data['group_nickname'],
  124. check_already_used: true,
  125. which: Nickname::CHECK_LOCAL_GROUP,
  126. check_is_allowed: true,
  127. );
  128. $roles = ActorLocalRoles::VISITOR; // Can send direct messages to other actors
  129. if ($data['group_scope'] === 'private') {
  130. $roles |= ActorLocalRoles::PRIVATE_GROUP;
  131. }
  132. Log::info(
  133. _m(
  134. 'Actor id:{actor_id} nick:{actor_nick} created the ' . ($roles & ActorLocalRoles::PRIVATE_GROUP ? 'private' : 'public') . ' group {nickname}',
  135. ['{actor_id}' => $actor->getId(), 'actor_nick' => $actor->getNickname(), 'nickname' => $nickname],
  136. ),
  137. );
  138. DB::persist($group = E\Actor::create([
  139. 'nickname' => $nickname,
  140. 'type' => E\Actor::GROUP,
  141. 'is_local' => true,
  142. 'roles' => $roles,
  143. ]));
  144. DB::persist(LocalGroup::create([
  145. 'actor_id' => $group->getId(),
  146. 'type' => $data['group_type'],
  147. 'nickname' => $nickname,
  148. ]));
  149. DB::persist(ActorSubscription::create([
  150. 'subscriber_id' => $group->getId(),
  151. 'subscribed_id' => $group->getId(),
  152. ]));
  153. DB::persist(GroupMember::create([
  154. 'group_id' => $group->getId(),
  155. 'actor_id' => $actor->getId(),
  156. // Group Owner
  157. 'roles' => ActorLocalRoles::OPERATOR | ActorLocalRoles::MODERATOR | ActorLocalRoles::PARTICIPANT | ActorLocalRoles::VISITOR,
  158. ]));
  159. DB::flush();
  160. Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribers']);
  161. Cache::delete(E\Actor::cacheKeys($actor->getId())['subscribed']);
  162. throw new RedirectException();
  163. }
  164. return $create_form;
  165. }
  166. }