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 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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) <skjnldsv@protonmail.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Tom Needham <tom@owncloud.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 OC\Accounts\AccountManager;
  34. use OCP\AppFramework\Http\DataResponse;
  35. use OCP\AppFramework\OCS\OCSException;
  36. use OCP\AppFramework\OCS\OCSForbiddenException;
  37. use OCP\AppFramework\OCS\OCSNotFoundException;
  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. AccountManager $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. *
  93. * @param string $search
  94. * @param int $limit
  95. * @param int $offset
  96. * @return DataResponse
  97. */
  98. public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse {
  99. $groups = $this->groupManager->search($search, $limit, $offset);
  100. $groups = array_map(function ($group) {
  101. /** @var IGroup $group */
  102. return [
  103. 'id' => $group->getGID(),
  104. 'displayname' => $group->getDisplayName(),
  105. 'usercount' => $group->count(),
  106. 'disabled' => $group->countDisabled(),
  107. 'canAdd' => $group->canAddUser(),
  108. 'canRemove' => $group->canRemoveUser(),
  109. ];
  110. }, $groups);
  111. return new DataResponse(['groups' => $groups]);
  112. }
  113. /**
  114. * @NoAdminRequired
  115. *
  116. * @param string $groupId
  117. * @return DataResponse
  118. * @throws OCSException
  119. *
  120. * @deprecated 14 Use getGroupUsers
  121. */
  122. public function getGroup(string $groupId): DataResponse {
  123. return $this->getGroupUsers($groupId);
  124. }
  125. /**
  126. * returns an array of users in the specified group
  127. *
  128. * @NoAdminRequired
  129. *
  130. * @param string $groupId
  131. * @return DataResponse
  132. * @throws OCSException
  133. */
  134. public function getGroupUsers(string $groupId): DataResponse {
  135. $groupId = urldecode($groupId);
  136. $user = $this->userSession->getUser();
  137. $isSubadminOfGroup = false;
  138. // Check the group exists
  139. $group = $this->groupManager->get($groupId);
  140. if ($group !== null) {
  141. $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group);
  142. } else {
  143. throw new OCSNotFoundException('The requested group could not be found');
  144. }
  145. // Check subadmin has access to this group
  146. if ($this->groupManager->isAdmin($user->getUID())
  147. || $isSubadminOfGroup) {
  148. $users = $this->groupManager->get($groupId)->getUsers();
  149. $users = array_map(function ($user) {
  150. /** @var IUser $user */
  151. return $user->getUID();
  152. }, $users);
  153. $users = array_values($users);
  154. return new DataResponse(['users' => $users]);
  155. }
  156. throw new OCSForbiddenException();
  157. }
  158. /**
  159. * returns an array of users details in the specified group
  160. *
  161. * @NoAdminRequired
  162. *
  163. * @param string $groupId
  164. * @param string $search
  165. * @param int $limit
  166. * @param int $offset
  167. * @return DataResponse
  168. * @throws OCSException
  169. */
  170. public function getGroupUsersDetails(string $groupId, string $search = '', int $limit = null, int $offset = 0): DataResponse {
  171. $groupId = urldecode($groupId);
  172. $currentUser = $this->userSession->getUser();
  173. // Check the group exists
  174. $group = $this->groupManager->get($groupId);
  175. if ($group !== null) {
  176. $isSubadminOfGroup = $this->groupManager->getSubAdmin()->isSubAdminOfGroup($currentUser, $group);
  177. } else {
  178. throw new OCSException('The requested group could not be found', \OCP\API::RESPOND_NOT_FOUND);
  179. }
  180. // Check subadmin has access to this group
  181. if ($this->groupManager->isAdmin($currentUser->getUID()) || $isSubadminOfGroup) {
  182. $users = $group->searchUsers($search, $limit, $offset);
  183. // Extract required number
  184. $usersDetails = [];
  185. foreach ($users as $user) {
  186. try {
  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. } catch (OCSNotFoundException $e) {
  199. // continue if a users ceased to exist.
  200. }
  201. }
  202. return new DataResponse(['users' => $usersDetails]);
  203. }
  204. throw new OCSException('User does not have access to specified group', \OCP\API::RESPOND_UNAUTHORISED);
  205. }
  206. /**
  207. * creates a new group
  208. *
  209. * @PasswordConfirmationRequired
  210. *
  211. * @param string $groupid
  212. * @return DataResponse
  213. * @throws OCSException
  214. */
  215. public function addGroup(string $groupid): DataResponse {
  216. // Validate name
  217. if (empty($groupid)) {
  218. $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']);
  219. throw new OCSException('Invalid group name', 101);
  220. }
  221. // Check if it exists
  222. if ($this->groupManager->groupExists($groupid)) {
  223. throw new OCSException('group exists', 102);
  224. }
  225. $this->groupManager->createGroup($groupid);
  226. return new DataResponse();
  227. }
  228. /**
  229. * @PasswordConfirmationRequired
  230. *
  231. * @param string $groupId
  232. * @param string $key
  233. * @param string $value
  234. * @return DataResponse
  235. * @throws OCSException
  236. */
  237. public function updateGroup(string $groupId, string $key, string $value): DataResponse {
  238. $groupId = urldecode($groupId);
  239. if ($key === 'displayname') {
  240. $group = $this->groupManager->get($groupId);
  241. if ($group->setDisplayName($value)) {
  242. return new DataResponse();
  243. }
  244. throw new OCSException('Not supported by backend', 101);
  245. } else {
  246. throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
  247. }
  248. }
  249. /**
  250. * @PasswordConfirmationRequired
  251. *
  252. * @param string $groupId
  253. * @return DataResponse
  254. * @throws OCSException
  255. */
  256. public function deleteGroup(string $groupId): DataResponse {
  257. $groupId = urldecode($groupId);
  258. // Check it exists
  259. if (!$this->groupManager->groupExists($groupId)) {
  260. throw new OCSException('', 101);
  261. } elseif ($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()) {
  262. // Cannot delete admin group
  263. throw new OCSException('', 102);
  264. }
  265. return new DataResponse();
  266. }
  267. /**
  268. * @param string $groupId
  269. * @return DataResponse
  270. * @throws OCSException
  271. */
  272. public function getSubAdminsOfGroup(string $groupId): DataResponse {
  273. // Check group exists
  274. $targetGroup = $this->groupManager->get($groupId);
  275. if ($targetGroup === null) {
  276. throw new OCSException('Group does not exist', 101);
  277. }
  278. /** @var IUser[] $subadmins */
  279. $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
  280. // New class returns IUser[] so convert back
  281. $uids = [];
  282. foreach ($subadmins as $user) {
  283. $uids[] = $user->getUID();
  284. }
  285. return new DataResponse($uids);
  286. }
  287. }