GroupsController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OC\Settings\Controller;
  25. use OC\AppFramework\Http;
  26. use OC\Group\MetaData;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http\DataResponse;
  29. use OCP\IGroupManager;
  30. use OCP\IL10N;
  31. use OCP\IRequest;
  32. use OCP\IUserSession;
  33. /**
  34. * @package OC\Settings\Controller
  35. */
  36. class GroupsController extends Controller {
  37. /** @var IGroupManager */
  38. private $groupManager;
  39. /** @var IL10N */
  40. private $l10n;
  41. /** @var IUserSession */
  42. private $userSession;
  43. /** @var bool */
  44. private $isAdmin;
  45. /**
  46. * @param string $appName
  47. * @param IRequest $request
  48. * @param IGroupManager $groupManager
  49. * @param IUserSession $userSession
  50. * @param bool $isAdmin
  51. * @param IL10N $l10n
  52. */
  53. public function __construct($appName,
  54. IRequest $request,
  55. IGroupManager $groupManager,
  56. IUserSession $userSession,
  57. $isAdmin,
  58. IL10N $l10n) {
  59. parent::__construct($appName, $request);
  60. $this->groupManager = $groupManager;
  61. $this->userSession = $userSession;
  62. $this->isAdmin = $isAdmin;
  63. $this->l10n = $l10n;
  64. }
  65. /**
  66. * @NoAdminRequired
  67. *
  68. * @param string $pattern
  69. * @param bool $filterGroups
  70. * @param int $sortGroups
  71. * @return DataResponse
  72. */
  73. public function index($pattern = '', $filterGroups = false, $sortGroups = MetaData::SORT_USERCOUNT) {
  74. $groupPattern = $filterGroups ? $pattern : '';
  75. $groupsInfo = new MetaData(
  76. $this->userSession->getUser()->getUID(),
  77. $this->isAdmin,
  78. $this->groupManager,
  79. $this->userSession
  80. );
  81. $groupsInfo->setSorting($sortGroups);
  82. list($adminGroups, $groups) = $groupsInfo->get($groupPattern, $pattern);
  83. return new DataResponse(
  84. array(
  85. 'data' => array('adminGroups' => $adminGroups, 'groups' => $groups)
  86. )
  87. );
  88. }
  89. /**
  90. * @PasswordConfirmationRequired
  91. * @param string $id
  92. * @return DataResponse
  93. */
  94. public function create($id) {
  95. if($this->groupManager->groupExists($id)) {
  96. return new DataResponse(
  97. array(
  98. 'message' => (string)$this->l10n->t('Group already exists.')
  99. ),
  100. Http::STATUS_CONFLICT
  101. );
  102. }
  103. if($this->groupManager->createGroup($id)) {
  104. return new DataResponse(
  105. array(
  106. 'groupname' => $id
  107. ),
  108. Http::STATUS_CREATED
  109. );
  110. }
  111. return new DataResponse(
  112. array(
  113. 'status' => 'error',
  114. 'data' => array(
  115. 'message' => (string)$this->l10n->t('Unable to add group.')
  116. )
  117. ),
  118. Http::STATUS_FORBIDDEN
  119. );
  120. }
  121. /**
  122. * @PasswordConfirmationRequired
  123. * @param string $id
  124. * @return DataResponse
  125. */
  126. public function destroy($id) {
  127. $group = $this->groupManager->get($id);
  128. if ($group) {
  129. if ($group->delete()) {
  130. return new DataResponse(
  131. array(
  132. 'status' => 'success',
  133. 'data' => array(
  134. 'groupname' => $id
  135. )
  136. ),
  137. Http::STATUS_NO_CONTENT
  138. );
  139. }
  140. }
  141. return new DataResponse(
  142. array(
  143. 'status' => 'error',
  144. 'data' => array(
  145. 'message' => (string)$this->l10n->t('Unable to delete group.')
  146. ),
  147. ),
  148. Http::STATUS_FORBIDDEN
  149. );
  150. }
  151. }