You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
3.7 KiB

10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  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. * @param string $id
  91. * @return DataResponse
  92. */
  93. public function create($id) {
  94. if($this->groupManager->groupExists($id)) {
  95. return new DataResponse(
  96. array(
  97. 'message' => (string)$this->l10n->t('Group already exists.')
  98. ),
  99. Http::STATUS_CONFLICT
  100. );
  101. }
  102. if($this->groupManager->createGroup($id)) {
  103. return new DataResponse(
  104. array(
  105. 'groupname' => $id
  106. ),
  107. Http::STATUS_CREATED
  108. );
  109. }
  110. return new DataResponse(
  111. array(
  112. 'status' => 'error',
  113. 'data' => array(
  114. 'message' => (string)$this->l10n->t('Unable to add group.')
  115. )
  116. ),
  117. Http::STATUS_FORBIDDEN
  118. );
  119. }
  120. /**
  121. * @param string $id
  122. * @return DataResponse
  123. */
  124. public function destroy($id) {
  125. $group = $this->groupManager->get($id);
  126. if ($group) {
  127. if ($group->delete()) {
  128. return new DataResponse(
  129. array(
  130. 'status' => 'success',
  131. 'data' => array(
  132. 'groupname' => $id
  133. )
  134. ),
  135. Http::STATUS_NO_CONTENT
  136. );
  137. }
  138. }
  139. return new DataResponse(
  140. array(
  141. 'status' => 'error',
  142. 'data' => array(
  143. 'message' => (string)$this->l10n->t('Unable to delete group.')
  144. ),
  145. ),
  146. Http::STATUS_FORBIDDEN
  147. );
  148. }
  149. }