GroupsController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Tom Needham <tom@owncloud.com>
  11. * @author John Molakvoæ <skjnldsv@protonmail.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Provisioning_API\Controller;
  29. use OC\Accounts\AccountManager;
  30. use OCP\AppFramework\Http\DataResponse;
  31. use OCP\AppFramework\OCS\OCSException;
  32. use OCP\AppFramework\OCS\OCSNotFoundException;
  33. use OCP\AppFramework\OCS\OCSForbiddenException;
  34. use OCP\IConfig;
  35. use OCP\IGroup;
  36. use OCP\IGroupManager;
  37. use OCP\ILogger;
  38. use OCP\IRequest;
  39. use OCP\IUserManager;
  40. use OCP\IUserSession;
  41. use OCP\IUser;
  42. class GroupsController extends AUserData {
  43. /** @var ILogger */
  44. private $logger;
  45. /**
  46. * @param string $appName
  47. * @param IRequest $request
  48. * @param IUserManager $userManager
  49. * @param IConfig $config
  50. * @param IGroupManager $groupManager
  51. * @param IUserSession $userSession
  52. * @param AccountManager $accountManager
  53. * @param ILogger $logger
  54. * @param UsersController $userController
  55. */
  56. public function __construct(string $appName,
  57. IRequest $request,
  58. IUserManager $userManager,
  59. IConfig $config,
  60. IGroupManager $groupManager,
  61. IUserSession $userSession,
  62. AccountManager $accountManager,
  63. ILogger $logger) {
  64. parent::__construct($appName,
  65. $request,
  66. $userManager,
  67. $config,
  68. $groupManager,
  69. $userSession,
  70. $accountManager);
  71. $this->logger = $logger;
  72. }
  73. /**
  74. * returns a list of groups
  75. *
  76. * @NoAdminRequired
  77. *
  78. * @param string $search
  79. * @param int $limit
  80. * @param int $offset
  81. * @return DataResponse
  82. */
  83. public function getGroups(string $search = '', int $limit = null, int $offset = 0): DataResponse {
  84. $groups = $this->groupManager->search($search, $limit, $offset);
  85. $groups = array_map(function($group) {
  86. /** @var IGroup $group */
  87. return $group->getGID();
  88. }, $groups);
  89. return new DataResponse(['groups' => $groups]);
  90. }
  91. /**
  92. * returns a list of groups details with ids and displaynames
  93. *
  94. * @NoAdminRequired
  95. *
  96. * @param string $search
  97. * @param int $limit
  98. * @param int $offset
  99. * @return DataResponse
  100. */
  101. public function getGroupsDetails(string $search = '', int $limit = null, int $offset = 0): DataResponse {
  102. $groups = $this->groupManager->search($search, $limit, $offset);
  103. $groups = array_map(function($group) {
  104. /** @var IGroup $group */
  105. return [
  106. 'id' => $group->getGID(),
  107. 'displayname' => $group->getDisplayName(),
  108. 'usercount' => $group->count(),
  109. 'disabled' => $group->countDisabled(),
  110. 'canAdd' => $group->canAddUser(),
  111. 'canRemove' => $group->canRemoveUser(),
  112. ];
  113. }, $groups);
  114. return new DataResponse(['groups' => $groups]);
  115. }
  116. /**
  117. * @NoAdminRequired
  118. *
  119. * @param string $groupId
  120. * @return DataResponse
  121. * @throws OCSException
  122. *
  123. * @deprecated 14 Use getGroupUsers
  124. */
  125. public function getGroup(string $groupId): DataResponse {
  126. return $this->getGroupUsers($groupId);
  127. }
  128. /**
  129. * returns an array of users in the specified group
  130. *
  131. * @NoAdminRequired
  132. *
  133. * @param string $groupId
  134. * @return DataResponse
  135. * @throws OCSException
  136. */
  137. public function getGroupUsers(string $groupId): DataResponse {
  138. $user = $this->userSession->getUser();
  139. $isSubadminOfGroup = false;
  140. // Check the group exists
  141. $group = $this->groupManager->get($groupId);
  142. if ($group !== null) {
  143. $isSubadminOfGroup =$this->groupManager->getSubAdmin()->isSubAdminOfGroup($user, $group);
  144. } else {
  145. throw new OCSNotFoundException('The requested group could not be found');
  146. }
  147. // Check subadmin has access to this group
  148. if($this->groupManager->isAdmin($user->getUID())
  149. || $isSubadminOfGroup) {
  150. $users = $this->groupManager->get($groupId)->getUsers();
  151. $users = array_map(function($user) {
  152. /** @var IUser $user */
  153. return $user->getUID();
  154. }, $users);
  155. $users = array_values($users);
  156. return new DataResponse(['users' => $users]);
  157. }
  158. throw new OCSForbiddenException();
  159. }
  160. /**
  161. * returns an array of users details in the specified group
  162. *
  163. * @NoAdminRequired
  164. *
  165. * @param string $groupId
  166. * @param string $search
  167. * @param int $limit
  168. * @param int $offset
  169. * @return DataResponse
  170. * @throws OCSException
  171. */
  172. public function getGroupUsersDetails(string $groupId, string $search = '', int $limit = null, int $offset = 0): DataResponse {
  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', \OCP\API::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', \OCP\API::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. * @return DataResponse
  234. * @throws OCSException
  235. */
  236. public function deleteGroup(string $groupId): DataResponse {
  237. // Check it exists
  238. if(!$this->groupManager->groupExists($groupId)){
  239. throw new OCSException('', 101);
  240. } else if($groupId === 'admin' || !$this->groupManager->get($groupId)->delete()){
  241. // Cannot delete admin group
  242. throw new OCSException('', 102);
  243. }
  244. return new DataResponse();
  245. }
  246. /**
  247. * @param string $groupId
  248. * @return DataResponse
  249. * @throws OCSException
  250. */
  251. public function getSubAdminsOfGroup(string $groupId): DataResponse {
  252. // Check group exists
  253. $targetGroup = $this->groupManager->get($groupId);
  254. if($targetGroup === null) {
  255. throw new OCSException('Group does not exist', 101);
  256. }
  257. /** @var IUser[] $subadmins */
  258. $subadmins = $this->groupManager->getSubAdmin()->getGroupsSubAdmins($targetGroup);
  259. // New class returns IUser[] so convert back
  260. $uids = [];
  261. foreach ($subadmins as $user) {
  262. $uids[] = $user->getUID();
  263. }
  264. return new DataResponse($uids);
  265. }
  266. }