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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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\AppFramework\OCSController;
  39. use OCP\IConfig;
  40. use OCP\IGroup;
  41. use OCP\IGroupManager;
  42. use OCP\IRequest;
  43. use OCP\IUser;
  44. use OCP\IUserManager;
  45. use OCP\IUserSession;
  46. use OCP\L10N\IFactory;
  47. use Psr\Log\LoggerInterface;
  48. class GroupsController extends AUserData {
  49. /** @var LoggerInterface */
  50. private $logger;
  51. public function __construct(string $appName,
  52. IRequest $request,
  53. IUserManager $userManager,
  54. IConfig $config,
  55. IGroupManager $groupManager,
  56. IUserSession $userSession,
  57. AccountManager $accountManager,
  58. IFactory $l10nFactory,
  59. LoggerInterface $logger) {
  60. parent::__construct($appName,
  61. $request,
  62. $userManager,
  63. $config,
  64. $groupManager,
  65. $userSession,
  66. $accountManager,
  67. $l10nFactory
  68. );
  69. $this->logger = $logger;
  70. }
  71. /**
  72. * returns a list of groups
  73. *
  74. * @NoAdminRequired
  75. *
  76. * @param string $search
  77. * @param int $limit
  78. * @param int $offset
  79. * @return DataResponse
  80. */
  81. public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse {
  82. $groups = $this->groupManager->search($search, $limit, $offset);
  83. $groups = array_map(function ($group) {
  84. /** @var IGroup $group */
  85. return $group->getGID();
  86. }, $groups);
  87. return new DataResponse(['groups' => $groups]);
  88. }
  89. /**
  90. * returns a list of groups details with ids and displaynames
  91. *
  92. * @NoAdminRequired
  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('User does not have access to specified group', OCSController::RESPOND_UNAUTHORISED);
  206. }
  207. /**
  208. * creates a new group
  209. *
  210. * @PasswordConfirmationRequired
  211. *
  212. * @param string $groupid
  213. * @return DataResponse
  214. * @throws OCSException
  215. */
  216. public function addGroup(string $groupid): DataResponse {
  217. // Validate name
  218. if (empty($groupid)) {
  219. $this->logger->error('Group name not supplied', ['app' => 'provisioning_api']);
  220. throw new OCSException('Invalid group name', 101);
  221. }
  222. // Check if it exists
  223. if ($this->groupManager->groupExists($groupid)) {
  224. throw new OCSException('group exists', 102);
  225. }
  226. $this->groupManager->createGroup($groupid);
  227. return new DataResponse();
  228. }
  229. /**
  230. * @PasswordConfirmationRequired
  231. *
  232. * @param string $groupId
  233. * @param string $key
  234. * @param string $value
  235. * @return DataResponse
  236. * @throws OCSException
  237. */
  238. public function updateGroup(string $groupId, string $key, string $value): DataResponse {
  239. $groupId = urldecode($groupId);
  240. if ($key === 'displayname') {
  241. $group = $this->groupManager->get($groupId);
  242. if ($group->setDisplayName($value)) {
  243. return new DataResponse();
  244. }
  245. throw new OCSException('Not supported by backend', 101);
  246. } else {
  247. throw new OCSException('', OCSController::RESPOND_UNAUTHORISED);
  248. }
  249. }
  250. /**
  251. * @PasswordConfirmationRequired
  252. *
  253. * @param string $groupId
  254. * @return DataResponse
  255. * @throws OCSException
  256. */
  257. public function deleteGroup(string $groupId): DataResponse {
  258. $groupId = urldecode($groupId);
  259. // Check it exists
  260. if (!$this->groupManager->groupExists($groupId)) {
  261. throw new OCSException('', 101);
  262. } elseif ($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()) {
  263. // Cannot delete admin group
  264. throw new OCSException('', 102);
  265. }
  266. return new DataResponse();
  267. }
  268. /**
  269. * @param string $groupId
  270. * @return DataResponse
  271. * @throws OCSException
  272. */
  273. public function getSubAdminsOfGroup(string $groupId): DataResponse {
  274. // Check group exists
  275. $targetGroup = $this->groupManager->get($groupId);
  276. if ($targetGroup === null) {
  277. throw new OCSException('Group does not exist', 101);
  278. }
  279. /** @var IUser[] $subadmins */
  280. $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
  281. // New class returns IUser[] so convert back
  282. $uids = [];
  283. foreach ($subadmins as $user) {
  284. $uids[] = $user->getUID();
  285. }
  286. return new DataResponse($uids);
  287. }
  288. }