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.

GroupsController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ <skjnldsv@protonmail.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Tom Needham <tom@owncloud.com>
  15. * @author Kate Döen <kate.doeen@nextcloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OCA\Provisioning_API\Controller;
  33. use OCA\Provisioning_API\ResponseDefinitions;
  34. use OCP\Accounts\IAccountManager;
  35. use OCP\AppFramework\Http;
  36. use OCP\AppFramework\Http\DataResponse;
  37. use OCP\AppFramework\OCS\OCSException;
  38. use OCP\AppFramework\OCS\OCSForbiddenException;
  39. use OCP\AppFramework\OCS\OCSNotFoundException;
  40. use OCP\AppFramework\OCSController;
  41. use OCP\IConfig;
  42. use OCP\IGroup;
  43. use OCP\IGroupManager;
  44. use OCP\IRequest;
  45. use OCP\IUser;
  46. use OCP\IUserManager;
  47. use OCP\IUserSession;
  48. use OCP\L10N\IFactory;
  49. use Psr\Log\LoggerInterface;
  50. /**
  51. * @psalm-import-type ProvisioningApiGroupDetails from ResponseDefinitions
  52. * @psalm-import-type ProvisioningApiUserDetails from ResponseDefinitions
  53. */
  54. class GroupsController extends AUserData {
  55. /** @var LoggerInterface */
  56. private $logger;
  57. public function __construct(string $appName,
  58. IRequest $request,
  59. IUserManager $userManager,
  60. IConfig $config,
  61. IGroupManager $groupManager,
  62. IUserSession $userSession,
  63. IAccountManager $accountManager,
  64. IFactory $l10nFactory,
  65. LoggerInterface $logger) {
  66. parent::__construct($appName,
  67. $request,
  68. $userManager,
  69. $config,
  70. $groupManager,
  71. $userSession,
  72. $accountManager,
  73. $l10nFactory
  74. );
  75. $this->logger = $logger;
  76. }
  77. /**
  78. * @NoAdminRequired
  79. *
  80. * Get a list of groups
  81. *
  82. * @param string $search Text to search for
  83. * @param ?int $limit Limit the amount of groups returned
  84. * @param int $offset Offset for searching for groups
  85. * @return DataResponse<Http::STATUS_OK, array{groups: string[]}, array{}>
  86. *
  87. * 200: Groups returned
  88. */
  89. public function getGroups(string $search = '', ?int $limit = null, int $offset = 0): DataResponse {
  90. $groups = $this->groupManager->search($search, $limit, $offset);
  91. $groups = array_map(function ($group) {
  92. /** @var IGroup $group */
  93. return $group->getGID();
  94. }, $groups);
  95. return new DataResponse(['groups' => $groups]);
  96. }
  97. /**
  98. * @NoAdminRequired
  99. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Sharing)
  100. *
  101. * Get a list of groups details
  102. *
  103. * @param string $search Text to search for
  104. * @param ?int $limit Limit the amount of groups returned
  105. * @param int $offset Offset for searching for groups
  106. * @return DataResponse<Http::STATUS_OK, array{groups: ProvisioningApiGroupDetails[]}, array{}>
  107. *
  108. * 200: Groups details returned
  109. */
  110. public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse {
  111. $groups = $this->groupManager->search($search, $limit, $offset);
  112. $groups = array_map(function ($group) {
  113. /** @var IGroup $group */
  114. return [
  115. 'id' => $group->getGID(),
  116. 'displayname' => $group->getDisplayName(),
  117. 'usercount' => $group->count(),
  118. 'disabled' => $group->countDisabled(),
  119. 'canAdd' => $group->canAddUser(),
  120. 'canRemove' => $group->canRemoveUser(),
  121. ];
  122. }, $groups);
  123. return new DataResponse(['groups' => $groups]);
  124. }
  125. /**
  126. * @NoAdminRequired
  127. *
  128. * Get a list of users in the specified group
  129. *
  130. * @param string $groupId ID of the group
  131. * @return DataResponse<Http::STATUS_OK, array{users: string[]}, array{}>
  132. * @throws OCSException
  133. *
  134. * @deprecated 14 Use getGroupUsers
  135. *
  136. * 200: Group users returned
  137. */
  138. public function getGroup(string $groupId): DataResponse {
  139. return $this->getGroupUsers($groupId);
  140. }
  141. /**
  142. * @NoAdminRequired
  143. *
  144. * Get a list of users in the specified group
  145. *
  146. * @param string $groupId ID of the group
  147. * @return DataResponse<Http::STATUS_OK, array{users: string[]}, array{}>
  148. * @throws OCSException
  149. * @throws OCSNotFoundException Group not found
  150. * @throws OCSForbiddenException Missing permissions to get users in the group
  151. *
  152. * 200: User IDs returned
  153. */
  154. public function getGroupUsers(string $groupId): DataResponse {
  155. $groupId = urldecode($groupId);
  156. $user = $this->userSession->getUser();
  157. $isSubadminOfGroup = false;
  158. // Check the group exists
  159. $group = $this->groupManager->get($groupId);
  160. if ($group !== null) {
  161. $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group);
  162. } else {
  163. throw new OCSNotFoundException('The requested group could not be found');
  164. }
  165. // Check subadmin has access to this group
  166. if ($this->groupManager->isAdmin($user->getUID())
  167. || $isSubadminOfGroup) {
  168. $users = $this->groupManager->get($groupId)->getUsers();
  169. $users = array_map(function ($user) {
  170. /** @var IUser $user */
  171. return $user->getUID();
  172. }, $users);
  173. /** @var string[] $users */
  174. $users = array_values($users);
  175. return new DataResponse(['users' => $users]);
  176. }
  177. throw new OCSForbiddenException();
  178. }
  179. /**
  180. * @NoAdminRequired
  181. *
  182. * Get a list of users details in the specified group
  183. *
  184. * @param string $groupId ID of the group
  185. * @param string $search Text to search for
  186. * @param int|null $limit Limit the amount of groups returned
  187. * @param int $offset Offset for searching for groups
  188. *
  189. * @return DataResponse<Http::STATUS_OK, array{users: array<string, ProvisioningApiUserDetails|array{id: string}>}, array{}>
  190. * @throws OCSException
  191. *
  192. * 200: Group users details returned
  193. */
  194. public function getGroupUsersDetails(string $groupId, string $search = '', int $limit = null, int $offset = 0): DataResponse {
  195. $groupId = urldecode($groupId);
  196. $currentUser = $this->userSession->getUser();
  197. // Check the group exists
  198. $group = $this->groupManager->get($groupId);
  199. if ($group !== null) {
  200. $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($currentUser, $group);
  201. } else {
  202. throw new OCSException('The requested group could not be found', OCSController::RESPOND_NOT_FOUND);
  203. }
  204. // Check subadmin has access to this group
  205. if ($this->groupManager->isAdmin($currentUser->getUID()) || $isSubadminOfGroup) {
  206. $users = $group->searchUsers($search, $limit, $offset);
  207. // Extract required number
  208. $usersDetails = [];
  209. foreach ($users as $user) {
  210. try {
  211. /** @var IUser $user */
  212. $userId = (string)$user->getUID();
  213. $userData = $this->getUserData($userId);
  214. // Do not insert empty entry
  215. if ($userData !== null) {
  216. $usersDetails[$userId] = $userData;
  217. } else {
  218. // Logged user does not have permissions to see this user
  219. // only showing its id
  220. $usersDetails[$userId] = ['id' => $userId];
  221. }
  222. } catch (OCSNotFoundException $e) {
  223. // continue if a users ceased to exist.
  224. }
  225. }
  226. return new DataResponse(['users' => $usersDetails]);
  227. }
  228. throw new OCSException('The requested group could not be found', OCSController::RESPOND_NOT_FOUND);
  229. }
  230. /**
  231. * @PasswordConfirmationRequired
  232. *
  233. * Create a new group
  234. *
  235. * @param string $groupid ID of the group
  236. * @param string $displayname Display name of the group
  237. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  238. * @throws OCSException
  239. *
  240. * 200: Group created successfully
  241. */
  242. public function addGroup(string $groupid, string $displayname = ''): DataResponse {
  243. // Validate name
  244. if (empty($groupid)) {
  245. $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']);
  246. throw new OCSException('Invalid group name', 101);
  247. }
  248. // Check if it exists
  249. if ($this->groupManager->groupExists($groupid)) {
  250. throw new OCSException('group exists', 102);
  251. }
  252. $group = $this->groupManager->createGroup($groupid);
  253. if ($group === null) {
  254. throw new OCSException('Not supported by backend', 103);
  255. }
  256. if ($displayname !== '') {
  257. $group->setDisplayName($displayname);
  258. }
  259. return new DataResponse();
  260. }
  261. /**
  262. * @PasswordConfirmationRequired
  263. *
  264. * Update a group
  265. *
  266. * @param string $groupId ID of the group
  267. * @param string $key Key to update, only 'displayname'
  268. * @param string $value New value for the key
  269. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  270. * @throws OCSException
  271. *
  272. * 200: Group updated successfully
  273. */
  274. public function updateGroup(string $groupId, string $key, string $value): DataResponse {
  275. $groupId = urldecode($groupId);
  276. if ($key === 'displayname') {
  277. $group = $this->groupManager->get($groupId);
  278. if ($group === null) {
  279. throw new OCSException('Group does not exist', OCSController::RESPOND_NOT_FOUND);
  280. }
  281. if ($group->setDisplayName($value)) {
  282. return new DataResponse();
  283. }
  284. throw new OCSException('Not supported by backend', 101);
  285. } else {
  286. throw new OCSException('', OCSController::RESPOND_UNKNOWN_ERROR);
  287. }
  288. }
  289. /**
  290. * @PasswordConfirmationRequired
  291. *
  292. * Delete a group
  293. *
  294. * @param string $groupId ID of the group
  295. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  296. * @throws OCSException
  297. *
  298. * 200: Group deleted successfully
  299. */
  300. public function deleteGroup(string $groupId): DataResponse {
  301. $groupId = urldecode($groupId);
  302. // Check it exists
  303. if (!$this->groupManager->groupExists($groupId)) {
  304. throw new OCSException('', 101);
  305. } elseif ($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()) {
  306. // Cannot delete admin group
  307. throw new OCSException('', 102);
  308. }
  309. return new DataResponse();
  310. }
  311. /**
  312. * Get the list of user IDs that are a subadmin of the group
  313. *
  314. * @param string $groupId ID of the group
  315. * @return DataResponse<Http::STATUS_OK, string[], array{}>
  316. * @throws OCSException
  317. *
  318. * 200: Sub admins returned
  319. */
  320. public function getSubAdminsOfGroup(string $groupId): DataResponse {
  321. // Check group exists
  322. $targetGroup = $this->groupManager->get($groupId);
  323. if ($targetGroup === null) {
  324. throw new OCSException('Group does not exist', 101);
  325. }
  326. /** @var IUser[] $subadmins */
  327. $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
  328. // New class returns IUser[] so convert back
  329. /** @var string[] $uids */
  330. $uids = [];
  331. foreach ($subadmins as $user) {
  332. $uids[] = $user->getUID();
  333. }
  334. return new DataResponse($uids);
  335. }
  336. }