1
0

GroupsController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Tom Needham <tom@owncloud.com>
  11. * @author John Molakvoæ <skjnldsv@protonmail.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Provisioning_API\Controller;
  29. use OC\Accounts\AccountManager;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\OCS\OCSException;
  32. use OCP\AppFramework\OCS\OCSNotFoundException;
  33. use OCP\AppFramework\OCS\OCSForbiddenException;
  34. use OCP\IConfig;
  35. use OCP\IGroup;
  36. use OCP\IGroupManager;
  37. use OCP\ILogger;
  38. use OCP\IRequest;
  39. use OCP\IUserManager;
  40. use OCP\IUserSession;
  41. use OCP\IUser;
  42. class GroupsController extends AUserData {
  43. /** @var ILogger */
  44. private $logger;
  45. /**
  46. * @param string $appName
  47. * @param IRequest $request
  48. * @param IUserManager $userManager
  49. * @param IConfig $config
  50. * @param IGroupManager $groupManager
  51. * @param IUserSession $userSession
  52. * @param AccountManager $accountManager
  53. * @param ILogger $logger
  54. * @param UsersController $userController
  55. */
  56. public function __construct(string $appName,
  57. IRequest $request,
  58. IUserManager $userManager,
  59. IConfig $config,
  60. IGroupManager $groupManager,
  61. IUserSession $userSession,
  62. AccountManager $accountManager,
  63. ILogger $logger) {
  64. parent::__construct($appName,
  65. $request,
  66. $userManager,
  67. $config,
  68. $groupManager,
  69. $userSession,
  70. $accountManager);
  71. $this->logger = $logger;
  72. }
  73. /**
  74. * returns a list of groups
  75. *
  76. * @NoAdminRequired
  77. *
  78. * @param string $search
  79. * @param int $limit
  80. * @param int $offset
  81. * @return DataResponse
  82. */
  83. public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse {
  84. $groups = $this->groupManager->search($search, $limit, $offset);
  85. $groups = array_map(function($group) {
  86. /** @var IGroup $group */
  87. return $group->getGID();
  88. }, $groups);
  89. return new DataResponse(['groups' => $groups]);
  90. }
  91. /**
  92. * returns a list of groups details with ids and displaynames
  93. *
  94. * @NoAdminRequired
  95. *
  96. * @param string $search
  97. * @param int $limit
  98. * @param int $offset
  99. * @return DataResponse
  100. */
  101. public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse {
  102. $groups = $this->groupManager->search($search, $limit, $offset);
  103. $groups = array_map(function($group) {
  104. /** @var IGroup $group */
  105. return [
  106. 'id' => $group->getGID(),
  107. 'displayname' => $group->getDisplayName(),
  108. 'usercount' => $group->count(),
  109. 'disabled' => $group->countDisabled(),
  110. 'canAdd' => $group->canAddUser(),
  111. 'canRemove' => $group->canRemoveUser(),
  112. ];
  113. }, $groups);
  114. return new DataResponse(['groups' => $groups]);
  115. }
  116. /**
  117. * @NoAdminRequired
  118. *
  119. * @param string $groupId
  120. * @return DataResponse
  121. * @throws OCSException
  122. *
  123. * @deprecated 14 Use getGroupUsers
  124. */
  125. public function getGroup(string $groupId): DataResponse {
  126. return $this->getGroupUsers($groupId);
  127. }
  128. /**
  129. * returns an array of users in the specified group
  130. *
  131. * @NoAdminRequired
  132. *
  133. * @param string $groupId
  134. * @return DataResponse
  135. * @throws OCSException
  136. */
  137. public function getGroupUsers(string $groupId): DataResponse {
  138. $user = $this->userSession->getUser();
  139. $isSubadminOfGroup = false;
  140. // Check the group exists
  141. $group = $this->groupManager->get($groupId);
  142. if ($group !== null) {
  143. $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group);
  144. } else {
  145. throw new OCSNotFoundException('The requested group could not be found');
  146. }
  147. // Check subadmin has access to this group
  148. if($this->groupManager->isAdmin($user->getUID())
  149. || $isSubadminOfGroup) {
  150. $users = $this->groupManager->get($groupId)->getUsers();
  151. $users = array_map(function($user) {
  152. /** @var IUser $user */
  153. return $user->getUID();
  154. }, $users);
  155. $users = array_values($users);
  156. return new DataResponse(['users' => $users]);
  157. }
  158. throw new OCSForbiddenException();
  159. }
  160. /**
  161. * returns an array of users details in the specified group
  162. *
  163. * @NoAdminRequired
  164. *
  165. * @param string $groupId
  166. * @param string $search
  167. * @param int $limit
  168. * @param int $offset
  169. * @return DataResponse
  170. * @throws OCSException
  171. */
  172. public function getGroupUsersDetails(string $groupId, string $search = '', int $limit = null, int $offset = 0): DataResponse {
  173. $currentUser = $this->userSession->getUser();
  174. // Check the group exists
  175. $group = $this->groupManager->get($groupId);
  176. if ($group !== null) {
  177. $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($currentUser, $group);
  178. } else {
  179. throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND);
  180. }
  181. // Check subadmin has access to this group
  182. if($this->groupManager->isAdmin($currentUser->getUID()) || $isSubadminOfGroup) {
  183. $users = $group->searchUsers($search, $limit, $offset);
  184. // Extract required number
  185. $usersDetails = [];
  186. foreach ($users as $user) {
  187. /** @var IUser $user */
  188. $userId = (string) $user->getUID();
  189. $userData = $this->getUserData($userId);
  190. // Do not insert empty entry
  191. if(!empty($userData)) {
  192. $usersDetails[$userId] = $userData;
  193. } else {
  194. // Logged user does not have permissions to see this user
  195. // only showing its id
  196. $usersDetails[$userId] = ['id' => $userId];
  197. }
  198. }
  199. return new DataResponse(['users' => $usersDetails]);
  200. }
  201. throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED);
  202. }
  203. /**
  204. * creates a new group
  205. *
  206. * @PasswordConfirmationRequired
  207. *
  208. * @param string $groupid
  209. * @return DataResponse
  210. * @throws OCSException
  211. */
  212. public function addGroup(string $groupid): DataResponse {
  213. // Validate name
  214. if(empty($groupid)) {
  215. $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']);
  216. throw new OCSException('Invalid group name', 101);
  217. }
  218. // Check if it exists
  219. if($this->groupManager->groupExists($groupid)){
  220. throw new OCSException('group exists', 102);
  221. }
  222. $this->groupManager->createGroup($groupid);
  223. return new DataResponse();
  224. }
  225. /**
  226. * @PasswordConfirmationRequired
  227. *
  228. * @param string $groupId
  229. * @return DataResponse
  230. * @throws OCSException
  231. */
  232. public function deleteGroup(string $groupId): DataResponse {
  233. // Check it exists
  234. if(!$this->groupManager->groupExists($groupId)){
  235. throw new OCSException('', 101);
  236. } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){
  237. // Cannot delete admin group
  238. throw new OCSException('', 102);
  239. }
  240. return new DataResponse();
  241. }
  242. /**
  243. * @param string $groupId
  244. * @return DataResponse
  245. * @throws OCSException
  246. */
  247. public function getSubAdminsOfGroup(string $groupId): DataResponse {
  248. // Check group exists
  249. $targetGroup = $this->groupManager->get($groupId);
  250. if($targetGroup === null) {
  251. throw new OCSException('Group does not exist', 101);
  252. }
  253. /** @var IUser[] $subadmins */
  254. $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
  255. // New class returns IUser[] so convert back
  256. $uids = [];
  257. foreach ($subadmins as $user) {
  258. $uids[] = $user->getUID();
  259. }
  260. return new DataResponse($uids);
  261. }
  262. }