GroupsController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\Provisioning_API\Controller;
  9. use OCA\Provisioning_API\ResponseDefinitions;
  10. use OCA\Settings\Settings\Admin\Sharing;
  11. use OCA\Settings\Settings\Admin\Users;
  12. use OCP\Accounts\IAccountManager;
  13. use OCP\AppFramework\Http;
  14. use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
  15. use OCP\AppFramework\Http\Attribute\NoAdminRequired;
  16. use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
  17. use OCP\AppFramework\Http\DataResponse;
  18. use OCP\AppFramework\OCS\OCSException;
  19. use OCP\AppFramework\OCS\OCSForbiddenException;
  20. use OCP\AppFramework\OCS\OCSNotFoundException;
  21. use OCP\AppFramework\OCSController;
  22. use OCP\IConfig;
  23. use OCP\IGroup;
  24. use OCP\IGroupManager;
  25. use OCP\IRequest;
  26. use OCP\IUser;
  27. use OCP\IUserManager;
  28. use OCP\IUserSession;
  29. use OCP\L10N\IFactory;
  30. use Psr\Log\LoggerInterface;
  31. /**
  32. * @psalm-import-type Provisioning_APIGroupDetails from ResponseDefinitions
  33. * @psalm-import-type Provisioning_APIUserDetails from ResponseDefinitions
  34. */
  35. class GroupsController extends AUserData {
  36. /** @var LoggerInterface */
  37. private $logger;
  38. public function __construct(string $appName,
  39. IRequest $request,
  40. IUserManager $userManager,
  41. IConfig $config,
  42. IGroupManager $groupManager,
  43. IUserSession $userSession,
  44. IAccountManager $accountManager,
  45. IFactory $l10nFactory,
  46. LoggerInterface $logger) {
  47. parent::__construct($appName,
  48. $request,
  49. $userManager,
  50. $config,
  51. $groupManager,
  52. $userSession,
  53. $accountManager,
  54. $l10nFactory
  55. );
  56. $this->logger = $logger;
  57. }
  58. /**
  59. * Get a list of groups
  60. *
  61. * @param string $search Text to search for
  62. * @param ?int $limit Limit the amount of groups returned
  63. * @param int $offset Offset for searching for groups
  64. * @return DataResponse<Http::STATUS_OK, array{groups: string[]}, array{}>
  65. *
  66. * 200: Groups returned
  67. */
  68. #[NoAdminRequired]
  69. public function getGroups(string $search = '', ?int $limit = null, int $offset = 0): DataResponse {
  70. $groups = $this->groupManager->search($search, $limit, $offset);
  71. $groups = array_map(function ($group) {
  72. /** @var IGroup $group */
  73. return $group->getGID();
  74. }, $groups);
  75. return new DataResponse(['groups' => $groups]);
  76. }
  77. /**
  78. * Get a list of groups details
  79. *
  80. * @param string $search Text to search for
  81. * @param ?int $limit Limit the amount of groups returned
  82. * @param int $offset Offset for searching for groups
  83. * @return DataResponse<Http::STATUS_OK, array{groups: Provisioning_APIGroupDetails[]}, array{}>
  84. *
  85. * 200: Groups details returned
  86. */
  87. #[NoAdminRequired]
  88. #[AuthorizedAdminSetting(settings: Sharing::class)]
  89. public function getGroupsDetails(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 [
  94. 'id' => $group->getGID(),
  95. 'displayname' => $group->getDisplayName(),
  96. 'usercount' => $group->count(),
  97. 'disabled' => $group->countDisabled(),
  98. 'canAdd' => $group->canAddUser(),
  99. 'canRemove' => $group->canRemoveUser(),
  100. ];
  101. }, $groups);
  102. return new DataResponse(['groups' => $groups]);
  103. }
  104. /**
  105. * Get a list of users in the specified group
  106. *
  107. * @param string $groupId ID of the group
  108. * @return DataResponse<Http::STATUS_OK, array{users: string[]}, array{}>
  109. * @throws OCSException
  110. *
  111. * @deprecated 14 Use getGroupUsers
  112. *
  113. * 200: Group users returned
  114. */
  115. #[NoAdminRequired]
  116. public function getGroup(string $groupId): DataResponse {
  117. return $this->getGroupUsers($groupId);
  118. }
  119. /**
  120. * Get a list of users in the specified group
  121. *
  122. * @param string $groupId ID of the group
  123. * @return DataResponse<Http::STATUS_OK, array{users: string[]}, array{}>
  124. * @throws OCSException
  125. * @throws OCSNotFoundException Group not found
  126. * @throws OCSForbiddenException Missing permissions to get users in the group
  127. *
  128. * 200: User IDs returned
  129. */
  130. #[NoAdminRequired]
  131. public function getGroupUsers(string $groupId): DataResponse {
  132. $groupId = urldecode($groupId);
  133. $user = $this->userSession->getUser();
  134. $isSubadminOfGroup = false;
  135. // Check the group exists
  136. $group = $this->groupManager->get($groupId);
  137. if ($group !== null) {
  138. $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group);
  139. } else {
  140. throw new OCSNotFoundException('The requested group could not be found');
  141. }
  142. // Check subadmin has access to this group
  143. $isAdmin = $this->groupManager->isAdmin($user->getUID());
  144. $isDelegatedAdmin = $this->groupManager->isDelegatedAdmin($user->getUID());
  145. if ($isAdmin || $isDelegatedAdmin || $isSubadminOfGroup) {
  146. $users = $this->groupManager->get($groupId)->getUsers();
  147. $users = array_map(function ($user) {
  148. /** @var IUser $user */
  149. return $user->getUID();
  150. }, $users);
  151. /** @var string[] $users */
  152. $users = array_values($users);
  153. return new DataResponse(['users' => $users]);
  154. }
  155. throw new OCSForbiddenException();
  156. }
  157. /**
  158. * Get a list of users details in the specified group
  159. *
  160. * @param string $groupId ID of the group
  161. * @param string $search Text to search for
  162. * @param int|null $limit Limit the amount of groups returned
  163. * @param int $offset Offset for searching for groups
  164. *
  165. * @return DataResponse<Http::STATUS_OK, array{users: array<string, Provisioning_APIUserDetails|array{id: string}>}, array{}>
  166. * @throws OCSException
  167. *
  168. * 200: Group users details returned
  169. */
  170. #[NoAdminRequired]
  171. public function getGroupUsersDetails(string $groupId, string $search = '', ?int $limit = null, int $offset = 0): DataResponse {
  172. $groupId = urldecode($groupId);
  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', OCSController::RESPOND_NOT_FOUND);
  180. }
  181. // Check subadmin has access to this group
  182. $isAdmin = $this->groupManager->isAdmin($currentUser->getUID());
  183. $isDelegatedAdmin = $this->groupManager->isDelegatedAdmin($currentUser->getUID());
  184. if ($isAdmin || $isDelegatedAdmin || $isSubadminOfGroup) {
  185. $users = $group->searchUsers($search, $limit, $offset);
  186. // Extract required number
  187. $usersDetails = [];
  188. foreach ($users as $user) {
  189. try {
  190. /** @var IUser $user */
  191. $userId = (string)$user->getUID();
  192. $userData = $this->getUserData($userId);
  193. // Do not insert empty entry
  194. if ($userData !== null) {
  195. $usersDetails[$userId] = $userData;
  196. } else {
  197. // Logged user does not have permissions to see this user
  198. // only showing its id
  199. $usersDetails[$userId] = ['id' => $userId];
  200. }
  201. } catch (OCSNotFoundException $e) {
  202. // continue if a users ceased to exist.
  203. }
  204. }
  205. return new DataResponse(['users' => $usersDetails]);
  206. }
  207. throw new OCSException('The requested group could not be found', OCSController::RESPOND_NOT_FOUND);
  208. }
  209. /**
  210. * Create a new group
  211. *
  212. * @param string $groupid ID of the group
  213. * @param string $displayname Display name of the group
  214. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  215. * @throws OCSException
  216. *
  217. * 200: Group created successfully
  218. */
  219. #[AuthorizedAdminSetting(settings:Users::class)]
  220. #[PasswordConfirmationRequired]
  221. public function addGroup(string $groupid, string $displayname = ''): DataResponse {
  222. // Validate name
  223. if (empty($groupid)) {
  224. $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']);
  225. throw new OCSException('Invalid group name', 101);
  226. }
  227. // Check if it exists
  228. if ($this->groupManager->groupExists($groupid)) {
  229. throw new OCSException('group exists', 102);
  230. }
  231. $group = $this->groupManager->createGroup($groupid);
  232. if ($group === null) {
  233. throw new OCSException('Not supported by backend', 103);
  234. }
  235. if ($displayname !== '') {
  236. $group->setDisplayName($displayname);
  237. }
  238. return new DataResponse();
  239. }
  240. /**
  241. * Update a group
  242. *
  243. * @param string $groupId ID of the group
  244. * @param string $key Key to update, only 'displayname'
  245. * @param string $value New value for the key
  246. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  247. * @throws OCSException
  248. *
  249. * 200: Group updated successfully
  250. */
  251. #[AuthorizedAdminSetting(settings:Users::class)]
  252. #[PasswordConfirmationRequired]
  253. public function updateGroup(string $groupId, string $key, string $value): DataResponse {
  254. $groupId = urldecode($groupId);
  255. if ($key === 'displayname') {
  256. $group = $this->groupManager->get($groupId);
  257. if ($group === null) {
  258. throw new OCSException('Group does not exist', OCSController::RESPOND_NOT_FOUND);
  259. }
  260. if ($group->setDisplayName($value)) {
  261. return new DataResponse();
  262. }
  263. throw new OCSException('Not supported by backend', 101);
  264. } else {
  265. throw new OCSException('', OCSController::RESPOND_UNKNOWN_ERROR);
  266. }
  267. }
  268. /**
  269. * Delete a group
  270. *
  271. * @param string $groupId ID of the group
  272. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  273. * @throws OCSException
  274. *
  275. * 200: Group deleted successfully
  276. */
  277. #[AuthorizedAdminSetting(settings:Users::class)]
  278. #[PasswordConfirmationRequired]
  279. public function deleteGroup(string $groupId): DataResponse {
  280. $groupId = urldecode($groupId);
  281. // Check it exists
  282. if (!$this->groupManager->groupExists($groupId)) {
  283. throw new OCSException('', 101);
  284. } elseif ($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()) {
  285. // Cannot delete admin group
  286. throw new OCSException('', 102);
  287. }
  288. return new DataResponse();
  289. }
  290. /**
  291. * Get the list of user IDs that are a subadmin of the group
  292. *
  293. * @param string $groupId ID of the group
  294. * @return DataResponse<Http::STATUS_OK, string[], array{}>
  295. * @throws OCSException
  296. *
  297. * 200: Sub admins returned
  298. */
  299. #[AuthorizedAdminSetting(settings:Users::class)]
  300. public function getSubAdminsOfGroup(string $groupId): DataResponse {
  301. // Check group exists
  302. $targetGroup = $this->groupManager->get($groupId);
  303. if ($targetGroup === null) {
  304. throw new OCSException('Group does not exist', 101);
  305. }
  306. /** @var IUser[] $subadmins */
  307. $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
  308. // New class returns IUser[] so convert back
  309. /** @var string[] $uids */
  310. $uids = [];
  311. foreach ($subadmins as $user) {
  312. $uids[] = $user->getUID();
  313. }
  314. return new DataResponse($uids);
  315. }
  316. }