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.

152 lines
3.5 KiB

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