GroupsController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. public function getGroups(string $search = '', ?int $limit = null, int $offset = 0): DataResponse {
  88. $groups = $this->groupManager->search($search, $limit, $offset);
  89. $groups = array_map(function ($group) {
  90. /** @var IGroup $group */
  91. return $group->getGID();
  92. }, $groups);
  93. return new DataResponse(['groups' => $groups]);
  94. }
  95. /**
  96. * @NoAdminRequired
  97. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Sharing)
  98. *
  99. * Get a list of groups details
  100. *
  101. * @param string $search Text to search for
  102. * @param ?int $limit Limit the amount of groups returned
  103. * @param int $offset Offset for searching for groups
  104. * @return DataResponse<Http::STATUS_OK, array{groups: ProvisioningApiGroupDetails[]}, array{}>
  105. */
  106. public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse {
  107. $groups = $this->groupManager->search($search, $limit, $offset);
  108. $groups = array_map(function ($group) {
  109. /** @var IGroup $group */
  110. return [
  111. 'id' => $group->getGID(),
  112. 'displayname' => $group->getDisplayName(),
  113. 'usercount' => $group->count(),
  114. 'disabled' => $group->countDisabled(),
  115. 'canAdd' => $group->canAddUser(),
  116. 'canRemove' => $group->canRemoveUser(),
  117. ];
  118. }, $groups);
  119. return new DataResponse(['groups' => $groups]);
  120. }
  121. /**
  122. * @NoAdminRequired
  123. *
  124. * Get a list of users in the specified group
  125. *
  126. * @param string $groupId ID of the group
  127. * @return DataResponse<Http::STATUS_OK, array{users: string[]}, array{}>
  128. * @throws OCSException
  129. *
  130. * @deprecated 14 Use getGroupUsers
  131. */
  132. public function getGroup(string $groupId): DataResponse {
  133. return $this->getGroupUsers($groupId);
  134. }
  135. /**
  136. * @NoAdminRequired
  137. *
  138. * Get a list of users in the specified group
  139. *
  140. * @param string $groupId ID of the group
  141. * @return DataResponse<Http::STATUS_OK, array{users: string[]}, array{}>
  142. * @throws OCSException
  143. * @throws OCSNotFoundException Group not found
  144. * @throws OCSForbiddenException Missing permissions to get users in the group
  145. *
  146. * 200: User IDs returned
  147. */
  148. public function getGroupUsers(string $groupId): DataResponse {
  149. $groupId = urldecode($groupId);
  150. $user = $this->userSession->getUser();
  151. $isSubadminOfGroup = false;
  152. // Check the group exists
  153. $group = $this->groupManager->get($groupId);
  154. if ($group !== null) {
  155. $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group);
  156. } else {
  157. throw new OCSNotFoundException('The requested group could not be found');
  158. }
  159. // Check subadmin has access to this group
  160. if ($this->groupManager->isAdmin($user->getUID())
  161. || $isSubadminOfGroup) {
  162. $users = $this->groupManager->get($groupId)->getUsers();
  163. $users = array_map(function ($user) {
  164. /** @var IUser $user */
  165. return $user->getUID();
  166. }, $users);
  167. /** @var string[] $users */
  168. $users = array_values($users);
  169. return new DataResponse(['users' => $users]);
  170. }
  171. throw new OCSForbiddenException();
  172. }
  173. /**
  174. * @NoAdminRequired
  175. *
  176. * Get a list of users details in the specified group
  177. *
  178. * @param string $groupId ID of the group
  179. * @param string $search Text to search for
  180. * @param int|null $limit Limit the amount of groups returned
  181. * @param int $offset Offset for searching for groups
  182. *
  183. * @return DataResponse<Http::STATUS_OK, array{users: array<string, ProvisioningApiUserDetails|array{id: string}>}, array{}>
  184. * @throws OCSException
  185. */
  186. public function getGroupUsersDetails(string $groupId, string $search = '', int $limit = null, int $offset = 0): DataResponse {
  187. $groupId = urldecode($groupId);
  188. $currentUser = $this->userSession->getUser();
  189. // Check the group exists
  190. $group = $this->groupManager->get($groupId);
  191. if ($group !== null) {
  192. $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($currentUser, $group);
  193. } else {
  194. throw new OCSException('The requested group could not be found', OCSController::RESPOND_NOT_FOUND);
  195. }
  196. // Check subadmin has access to this group
  197. if ($this->groupManager->isAdmin($currentUser->getUID()) || $isSubadminOfGroup) {
  198. $users = $group->searchUsers($search, $limit, $offset);
  199. // Extract required number
  200. $usersDetails = [];
  201. foreach ($users as $user) {
  202. try {
  203. /** @var IUser $user */
  204. $userId = (string)$user->getUID();
  205. $userData = $this->getUserData($userId);
  206. // Do not insert empty entry
  207. if ($userData !== null) {
  208. $usersDetails[$userId] = $userData;
  209. } else {
  210. // Logged user does not have permissions to see this user
  211. // only showing its id
  212. $usersDetails[$userId] = ['id' => $userId];
  213. }
  214. } catch (OCSNotFoundException $e) {
  215. // continue if a users ceased to exist.
  216. }
  217. }
  218. return new DataResponse(['users' => $usersDetails]);
  219. }
  220. throw new OCSException('The requested group could not be found', OCSController::RESPOND_NOT_FOUND);
  221. }
  222. /**
  223. * @PasswordConfirmationRequired
  224. *
  225. * Create a new group
  226. *
  227. * @param string $groupid ID of the group
  228. * @param string $displayname Display name of the group
  229. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  230. * @throws OCSException
  231. */
  232. public function addGroup(string $groupid, string $displayname = ''): DataResponse {
  233. // Validate name
  234. if (empty($groupid)) {
  235. $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']);
  236. throw new OCSException('Invalid group name', 101);
  237. }
  238. // Check if it exists
  239. if ($this->groupManager->groupExists($groupid)) {
  240. throw new OCSException('group exists', 102);
  241. }
  242. $group = $this->groupManager->createGroup($groupid);
  243. if ($group === null) {
  244. throw new OCSException('Not supported by backend', 103);
  245. }
  246. if ($displayname !== '') {
  247. $group->setDisplayName($displayname);
  248. }
  249. return new DataResponse();
  250. }
  251. /**
  252. * @PasswordConfirmationRequired
  253. *
  254. * Update a group
  255. *
  256. * @param string $groupId ID of the group
  257. * @param string $key Key to update, only 'displayname'
  258. * @param string $value New value for the key
  259. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  260. * @throws OCSException
  261. */
  262. public function updateGroup(string $groupId, string $key, string $value): DataResponse {
  263. $groupId = urldecode($groupId);
  264. if ($key === 'displayname') {
  265. $group = $this->groupManager->get($groupId);
  266. if ($group === null) {
  267. throw new OCSException('Group does not exist', OCSController::RESPOND_NOT_FOUND);
  268. }
  269. if ($group->setDisplayName($value)) {
  270. return new DataResponse();
  271. }
  272. throw new OCSException('Not supported by backend', 101);
  273. } else {
  274. throw new OCSException('', OCSController::RESPOND_UNKNOWN_ERROR);
  275. }
  276. }
  277. /**
  278. * @PasswordConfirmationRequired
  279. *
  280. * Delete a group
  281. *
  282. * @param string $groupId ID of the group
  283. * @return DataResponse<Http::STATUS_OK, array<empty>, array{}>
  284. * @throws OCSException
  285. */
  286. public function deleteGroup(string $groupId): DataResponse {
  287. $groupId = urldecode($groupId);
  288. // Check it exists
  289. if (!$this->groupManager->groupExists($groupId)) {
  290. throw new OCSException('', 101);
  291. } elseif ($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()) {
  292. // Cannot delete admin group
  293. throw new OCSException('', 102);
  294. }
  295. return new DataResponse();
  296. }
  297. /**
  298. * Get the list of user IDs that are a subadmin of the group
  299. *
  300. * @param string $groupId ID of the group
  301. * @return DataResponse<Http::STATUS_OK, string[], array{}>
  302. * @throws OCSException
  303. */
  304. public function getSubAdminsOfGroup(string $groupId): DataResponse {
  305. // Check group exists
  306. $targetGroup = $this->groupManager->get($groupId);
  307. if ($targetGroup === null) {
  308. throw new OCSException('Group does not exist', 101);
  309. }
  310. /** @var IUser[] $subadmins */
  311. $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
  312. // New class returns IUser[] so convert back
  313. /** @var string[] $uids */
  314. $uids = [];
  315. foreach ($subadmins as $user) {
  316. $uids[] = $user->getUID();
  317. }
  318. return new DataResponse($uids);
  319. }
  320. }