GroupsController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\Provisioning_API\Controller;
  32. use OCP\Accounts\IAccountManager;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\AppFramework\OCS\OCSException;
  35. use OCP\AppFramework\OCS\OCSForbiddenException;
  36. use OCP\AppFramework\OCS\OCSNotFoundException;
  37. use OCP\AppFramework\OCSController;
  38. use OCP\IConfig;
  39. use OCP\IGroup;
  40. use OCP\IGroupManager;
  41. use OCP\IRequest;
  42. use OCP\IUser;
  43. use OCP\IUserManager;
  44. use OCP\IUserSession;
  45. use OCP\L10N\IFactory;
  46. use Psr\Log\LoggerInterface;
  47. class GroupsController extends AUserData {
  48. /** @var LoggerInterface */
  49. private $logger;
  50. public function __construct(string $appName,
  51. IRequest $request,
  52. IUserManager $userManager,
  53. IConfig $config,
  54. IGroupManager $groupManager,
  55. IUserSession $userSession,
  56. IAccountManager $accountManager,
  57. IFactory $l10nFactory,
  58. LoggerInterface $logger) {
  59. parent::__construct($appName,
  60. $request,
  61. $userManager,
  62. $config,
  63. $groupManager,
  64. $userSession,
  65. $accountManager,
  66. $l10nFactory
  67. );
  68. $this->logger = $logger;
  69. }
  70. /**
  71. * returns a list of groups
  72. *
  73. * @NoAdminRequired
  74. *
  75. * @param string $search
  76. * @param int $limit
  77. * @param int $offset
  78. * @return DataResponse
  79. */
  80. public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse {
  81. $groups = $this->groupManager->search($search, $limit, $offset);
  82. $groups = array_map(function ($group) {
  83. /** @var IGroup $group */
  84. return $group->getGID();
  85. }, $groups);
  86. return new DataResponse(['groups' => $groups]);
  87. }
  88. /**
  89. * Returns a list of groups details with ids and displaynames
  90. *
  91. * @NoAdminRequired
  92. * @AuthorizedAdminSetting(settings=OCA\Settings\Settings\Admin\Sharing)
  93. *
  94. * @param string $search
  95. * @param int $limit
  96. * @param int $offset
  97. * @return DataResponse
  98. */
  99. public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse {
  100. $groups = $this->groupManager->search($search, $limit, $offset);
  101. $groups = array_map(function ($group) {
  102. /** @var IGroup $group */
  103. return [
  104. 'id' => $group->getGID(),
  105. 'displayname' => $group->getDisplayName(),
  106. 'usercount' => $group->count(),
  107. 'disabled' => $group->countDisabled(),
  108. 'canAdd' => $group->canAddUser(),
  109. 'canRemove' => $group->canRemoveUser(),
  110. ];
  111. }, $groups);
  112. return new DataResponse(['groups' => $groups]);
  113. }
  114. /**
  115. * @NoAdminRequired
  116. *
  117. * @param string $groupId
  118. * @return DataResponse
  119. * @throws OCSException
  120. *
  121. * @deprecated 14 Use getGroupUsers
  122. */
  123. public function getGroup(string $groupId): DataResponse {
  124. return $this->getGroupUsers($groupId);
  125. }
  126. /**
  127. * returns an array of users in the specified group
  128. *
  129. * @NoAdminRequired
  130. *
  131. * @param string $groupId
  132. * @return DataResponse
  133. * @throws OCSException
  134. */
  135. public function getGroupUsers(string $groupId): DataResponse {
  136. $groupId = urldecode($groupId);
  137. $user = $this->userSession->getUser();
  138. $isSubadminOfGroup = false;
  139. // Check the group exists
  140. $group = $this->groupManager->get($groupId);
  141. if ($group !== null) {
  142. $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group);
  143. } else {
  144. throw new OCSNotFoundException('The requested group could not be found');
  145. }
  146. // Check subadmin has access to this group
  147. if ($this->groupManager->isAdmin($user->getUID())
  148. || $isSubadminOfGroup) {
  149. $users = $this->groupManager->get($groupId)->getUsers();
  150. $users = array_map(function ($user) {
  151. /** @var IUser $user */
  152. return $user->getUID();
  153. }, $users);
  154. $users = array_values($users);
  155. return new DataResponse(['users' => $users]);
  156. }
  157. throw new OCSForbiddenException();
  158. }
  159. /**
  160. * returns an array of users details in the specified group
  161. *
  162. * @NoAdminRequired
  163. *
  164. * @param string $groupId
  165. * @param string $search
  166. * @param int $limit
  167. * @param int $offset
  168. * @return DataResponse
  169. * @throws OCSException
  170. */
  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. 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. try {
  188. /** @var IUser $user */
  189. $userId = (string)$user->getUID();
  190. $userData = $this->getUserData($userId);
  191. // Do not insert empty entry
  192. if (!empty($userData)) {
  193. $usersDetails[$userId] = $userData;
  194. } else {
  195. // Logged user does not have permissions to see this user
  196. // only showing its id
  197. $usersDetails[$userId] = ['id' => $userId];
  198. }
  199. } catch (OCSNotFoundException $e) {
  200. // continue if a users ceased to exist.
  201. }
  202. }
  203. return new DataResponse(['users' => $usersDetails]);
  204. }
  205. throw new OCSException('The requested group could not be found', OCSController::RESPOND_NOT_FOUND);
  206. }
  207. /**
  208. * creates a new group
  209. *
  210. * @PasswordConfirmationRequired
  211. *
  212. * @param string $groupid
  213. * @param string $displayname
  214. * @return DataResponse
  215. * @throws OCSException
  216. */
  217. public function addGroup(string $groupid, string $displayname = ''): DataResponse {
  218. // Validate name
  219. if (empty($groupid)) {
  220. $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']);
  221. throw new OCSException('Invalid group name', 101);
  222. }
  223. // Check if it exists
  224. if ($this->groupManager->groupExists($groupid)) {
  225. throw new OCSException('group exists', 102);
  226. }
  227. $group = $this->groupManager->createGroup($groupid);
  228. if ($group === null) {
  229. throw new OCSException('Not supported by backend', 103);
  230. }
  231. if ($displayname !== '') {
  232. $group->setDisplayName($displayname);
  233. }
  234. return new DataResponse();
  235. }
  236. /**
  237. * @PasswordConfirmationRequired
  238. *
  239. * @param string $groupId
  240. * @param string $key
  241. * @param string $value
  242. * @return DataResponse
  243. * @throws OCSException
  244. */
  245. public function updateGroup(string $groupId, string $key, string $value): DataResponse {
  246. $groupId = urldecode($groupId);
  247. if ($key === 'displayname') {
  248. $group = $this->groupManager->get($groupId);
  249. if ($group === null) {
  250. throw new OCSException('Group does not exist', OCSController::RESPOND_NOT_FOUND);
  251. }
  252. if ($group->setDisplayName($value)) {
  253. return new DataResponse();
  254. }
  255. throw new OCSException('Not supported by backend', 101);
  256. } else {
  257. throw new OCSException('', OCSController::RESPOND_UNKNOWN_ERROR);
  258. }
  259. }
  260. /**
  261. * @PasswordConfirmationRequired
  262. *
  263. * @param string $groupId
  264. * @return DataResponse
  265. * @throws OCSException
  266. */
  267. public function deleteGroup(string $groupId): DataResponse {
  268. $groupId = urldecode($groupId);
  269. // Check it exists
  270. if (!$this->groupManager->groupExists($groupId)) {
  271. throw new OCSException('', 101);
  272. } elseif ($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()) {
  273. // Cannot delete admin group
  274. throw new OCSException('', 102);
  275. }
  276. return new DataResponse();
  277. }
  278. /**
  279. * @param string $groupId
  280. * @return DataResponse
  281. * @throws OCSException
  282. */
  283. public function getSubAdminsOfGroup(string $groupId): DataResponse {
  284. // Check group exists
  285. $targetGroup = $this->groupManager->get($groupId);
  286. if ($targetGroup === null) {
  287. throw new OCSException('Group does not exist', 101);
  288. }
  289. /** @var IUser[] $subadmins */
  290. $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
  291. // New class returns IUser[] so convert back
  292. $uids = [];
  293. foreach ($subadmins as $user) {
  294. $uids[] = $user->getUID();
  295. }
  296. return new DataResponse($uids);
  297. }
  298. }