UsersController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author michag86 <micha_g@arcor.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Tom Needham <tom@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Provisioning_API\Controller;
  30. use OC\Accounts\AccountManager;
  31. use \OC_Helper;
  32. use OCP\AppFramework\Http\DataResponse;
  33. use OCP\AppFramework\Http\TemplateResponse;
  34. use OCP\AppFramework\OCS\OCSException;
  35. use OCP\AppFramework\OCS\OCSForbiddenException;
  36. use OCP\AppFramework\OCSController;
  37. use OCP\Files\NotFoundException;
  38. use OCP\IConfig;
  39. use OCP\IGroup;
  40. use OCP\IGroupManager;
  41. use OCP\ILogger;
  42. use OCP\IRequest;
  43. use OCP\IURLGenerator;
  44. use OCP\IUserManager;
  45. use OCP\IUserSession;
  46. use OCP\L10N\IFactory;
  47. use OCP\Mail\IMailer;
  48. class UsersController extends OCSController {
  49. /** @var IUserManager */
  50. private $userManager;
  51. /** @var IConfig */
  52. private $config;
  53. /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
  54. private $groupManager;
  55. /** @var IUserSession */
  56. private $userSession;
  57. /** @var AccountManager */
  58. private $accountManager;
  59. /** @var ILogger */
  60. private $logger;
  61. /** @var string */
  62. private $fromMailAddress;
  63. /** @var IURLGenerator */
  64. private $urlGenerator;
  65. /** @var IMailer */
  66. private $mailer;
  67. /** @var \OC_Defaults */
  68. private $defaults;
  69. /** @var IFactory */
  70. private $l10nFactory;
  71. /**
  72. * @param string $appName
  73. * @param IRequest $request
  74. * @param IUserManager $userManager
  75. * @param IConfig $config
  76. * @param IGroupManager $groupManager
  77. * @param IUserSession $userSession
  78. * @param AccountManager $accountManager
  79. * @param ILogger $logger
  80. * @param string $fromMailAddress
  81. * @param IURLGenerator $urlGenerator
  82. * @param IMailer $mailer
  83. * @param \OC_Defaults $defaults
  84. * @param IFactory $l10nFactory
  85. */
  86. public function __construct($appName,
  87. IRequest $request,
  88. IUserManager $userManager,
  89. IConfig $config,
  90. IGroupManager $groupManager,
  91. IUserSession $userSession,
  92. AccountManager $accountManager,
  93. ILogger $logger,
  94. $fromMailAddress,
  95. IURLGenerator $urlGenerator,
  96. IMailer $mailer,
  97. \OC_Defaults $defaults,
  98. IFactory $l10nFactory) {
  99. parent::__construct($appName, $request);
  100. $this->userManager = $userManager;
  101. $this->config = $config;
  102. $this->groupManager = $groupManager;
  103. $this->userSession = $userSession;
  104. $this->accountManager = $accountManager;
  105. $this->logger = $logger;
  106. $this->fromMailAddress = $fromMailAddress;
  107. $this->urlGenerator = $urlGenerator;
  108. $this->mailer = $mailer;
  109. $this->defaults = $defaults;
  110. $this->l10nFactory = $l10nFactory;
  111. }
  112. /**
  113. * @NoAdminRequired
  114. *
  115. * returns a list of users
  116. *
  117. * @param string $search
  118. * @param int $limit
  119. * @param int $offset
  120. * @return DataResponse
  121. */
  122. public function getUsers($search = '', $limit = null, $offset = null) {
  123. $user = $this->userSession->getUser();
  124. $users = [];
  125. // Admin? Or SubAdmin?
  126. $uid = $user->getUID();
  127. $subAdminManager = $this->groupManager->getSubAdmin();
  128. if($this->groupManager->isAdmin($uid)){
  129. $users = $this->userManager->search($search, $limit, $offset);
  130. } else if ($subAdminManager->isSubAdmin($user)) {
  131. $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
  132. foreach ($subAdminOfGroups as $key => $group) {
  133. $subAdminOfGroups[$key] = $group->getGID();
  134. }
  135. if($offset === null) {
  136. $offset = 0;
  137. }
  138. $users = [];
  139. foreach ($subAdminOfGroups as $group) {
  140. $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search));
  141. }
  142. $users = array_slice($users, $offset, $limit);
  143. }
  144. $users = array_keys($users);
  145. return new DataResponse([
  146. 'users' => $users
  147. ]);
  148. }
  149. /**
  150. * @PasswordConfirmationRequired
  151. * @NoAdminRequired
  152. *
  153. * @param string $userid
  154. * @param string $password
  155. * @param array $groups
  156. * @return DataResponse
  157. * @throws OCSException
  158. */
  159. public function addUser($userid, $password, $groups = null) {
  160. $user = $this->userSession->getUser();
  161. $isAdmin = $this->groupManager->isAdmin($user->getUID());
  162. $subAdminManager = $this->groupManager->getSubAdmin();
  163. if($this->userManager->userExists($userid)) {
  164. $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
  165. throw new OCSException('User already exists', 102);
  166. }
  167. if(is_array($groups)) {
  168. foreach ($groups as $group) {
  169. if(!$this->groupManager->groupExists($group)) {
  170. throw new OCSException('group '.$group.' does not exist', 104);
  171. }
  172. if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) {
  173. throw new OCSException('insufficient privileges for group '. $group, 105);
  174. }
  175. }
  176. } else {
  177. if(!$isAdmin) {
  178. throw new OCSException('no group specified (required for subadmins)', 106);
  179. }
  180. }
  181. try {
  182. $newUser = $this->userManager->createUser($userid, $password);
  183. $this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']);
  184. if (is_array($groups)) {
  185. foreach ($groups as $group) {
  186. $this->groupManager->get($group)->addUser($newUser);
  187. $this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']);
  188. }
  189. }
  190. return new DataResponse();
  191. } catch (\Exception $e) {
  192. $this->logger->error('Failed addUser attempt with exception: '.$e->getMessage(), ['app' => 'ocs_api']);
  193. throw new OCSException('Bad request', 101);
  194. }
  195. }
  196. /**
  197. * @NoAdminRequired
  198. * @NoSubAdminRequired
  199. *
  200. * gets user info
  201. *
  202. * @param string $userId
  203. * @return DataResponse
  204. * @throws OCSException
  205. */
  206. public function getUser($userId) {
  207. $data = $this->getUserData($userId);
  208. return new DataResponse($data);
  209. }
  210. /**
  211. * @NoAdminRequired
  212. * @NoSubAdminRequired
  213. *
  214. * gets user info from the currently logged in user
  215. *
  216. * @return DataResponse
  217. * @throws OCSException
  218. */
  219. public function getCurrentUser() {
  220. $user = $this->userSession->getUser();
  221. if ($user) {
  222. $data = $this->getUserData($user->getUID());
  223. // rename "displayname" to "display-name" only for this call to keep
  224. // the API stable.
  225. $data['display-name'] = $data['displayname'];
  226. unset($data['displayname']);
  227. return new DataResponse($data);
  228. }
  229. throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
  230. }
  231. /**
  232. * creates a array with all user data
  233. *
  234. * @param $userId
  235. * @return array
  236. * @throws OCSException
  237. */
  238. protected function getUserData($userId) {
  239. $currentLoggedInUser = $this->userSession->getUser();
  240. $data = [];
  241. // Check if the target user exists
  242. $targetUserObject = $this->userManager->get($userId);
  243. if($targetUserObject === null) {
  244. throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND);
  245. }
  246. // Admin? Or SubAdmin?
  247. if($this->groupManager->isAdmin($currentLoggedInUser->getUID())
  248. || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
  249. $data['enabled'] = $this->config->getUserValue($userId, 'core', 'enabled', 'true');
  250. } else {
  251. // Check they are looking up themselves
  252. if($currentLoggedInUser->getUID() !== $userId) {
  253. throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
  254. }
  255. }
  256. $userAccount = $this->accountManager->getUser($targetUserObject);
  257. $groups = $this->groupManager->getUserGroups($targetUserObject);
  258. $gids = [];
  259. foreach ($groups as $group) {
  260. $gids[] = $group->getDisplayName();
  261. }
  262. // Find the data
  263. $data['id'] = $targetUserObject->getUID();
  264. $data['quota'] = $this->fillStorageInfo($userId);
  265. $data['email'] = $targetUserObject->getEMailAddress();
  266. $data['displayname'] = $targetUserObject->getDisplayName();
  267. $data['phone'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_PHONE]['value'];
  268. $data['address'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_ADDRESS]['value'];
  269. $data['webpage'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_WEBSITE]['value'];
  270. $data['twitter'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_TWITTER]['value'];
  271. $data['groups'] = $gids;
  272. return $data;
  273. }
  274. /**
  275. * @NoAdminRequired
  276. * @NoSubAdminRequired
  277. * @PasswordConfirmationRequired
  278. *
  279. * edit users
  280. *
  281. * @param string $userId
  282. * @param string $key
  283. * @param string $value
  284. * @return DataResponse
  285. * @throws OCSException
  286. * @throws OCSForbiddenException
  287. */
  288. public function editUser($userId, $key, $value) {
  289. $currentLoggedInUser = $this->userSession->getUser();
  290. $targetUser = $this->userManager->get($userId);
  291. if($targetUser === null) {
  292. throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
  293. }
  294. $permittedFields = [];
  295. if($userId === $currentLoggedInUser->getUID()) {
  296. // Editing self (display, email)
  297. $permittedFields[] = 'display';
  298. $permittedFields[] = 'email';
  299. $permittedFields[] = 'password';
  300. // If admin they can edit their own quota
  301. if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
  302. $permittedFields[] = 'quota';
  303. }
  304. } else {
  305. // Check if admin / subadmin
  306. $subAdminManager = $this->groupManager->getSubAdmin();
  307. if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
  308. || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
  309. // They have permissions over the user
  310. $permittedFields[] = 'display';
  311. $permittedFields[] = 'quota';
  312. $permittedFields[] = 'password';
  313. $permittedFields[] = 'email';
  314. } else {
  315. // No rights
  316. throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
  317. }
  318. }
  319. // Check if permitted to edit this field
  320. if(!in_array($key, $permittedFields)) {
  321. throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
  322. }
  323. // Process the edit
  324. switch($key) {
  325. case 'display':
  326. $targetUser->setDisplayName($value);
  327. break;
  328. case 'quota':
  329. $quota = $value;
  330. if($quota !== 'none' && $quota !== 'default') {
  331. if (is_numeric($quota)) {
  332. $quota = (float) $quota;
  333. } else {
  334. $quota = \OCP\Util::computerFileSize($quota);
  335. }
  336. if ($quota === false) {
  337. throw new OCSException('Invalid quota value '.$value, 103);
  338. }
  339. if($quota === 0) {
  340. $quota = 'default';
  341. }else if($quota === -1) {
  342. $quota = 'none';
  343. } else {
  344. $quota = \OCP\Util::humanFileSize($quota);
  345. }
  346. }
  347. $targetUser->setQuota($quota);
  348. break;
  349. case 'password':
  350. $targetUser->setPassword($value);
  351. break;
  352. case 'email':
  353. if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
  354. $targetUser->setEMailAddress($value);
  355. } else {
  356. throw new OCSException('', 102);
  357. }
  358. break;
  359. default:
  360. throw new OCSException('', 103);
  361. }
  362. return new DataResponse();
  363. }
  364. /**
  365. * @PasswordConfirmationRequired
  366. * @NoAdminRequired
  367. *
  368. * @param string $userId
  369. * @return DataResponse
  370. * @throws OCSException
  371. * @throws OCSForbiddenException
  372. */
  373. public function deleteUser($userId) {
  374. $currentLoggedInUser = $this->userSession->getUser();
  375. $targetUser = $this->userManager->get($userId);
  376. if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
  377. throw new OCSException('', 101);
  378. }
  379. // If not permitted
  380. $subAdminManager = $this->groupManager->getSubAdmin();
  381. if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
  382. throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
  383. }
  384. // Go ahead with the delete
  385. if($targetUser->delete()) {
  386. return new DataResponse();
  387. } else {
  388. throw new OCSException('', 101);
  389. }
  390. }
  391. /**
  392. * @PasswordConfirmationRequired
  393. * @NoAdminRequired
  394. *
  395. * @param string $userId
  396. * @return DataResponse
  397. * @throws OCSException
  398. * @throws OCSForbiddenException
  399. */
  400. public function disableUser($userId) {
  401. return $this->setEnabled($userId, false);
  402. }
  403. /**
  404. * @PasswordConfirmationRequired
  405. * @NoAdminRequired
  406. *
  407. * @param string $userId
  408. * @return DataResponse
  409. * @throws OCSException
  410. * @throws OCSForbiddenException
  411. */
  412. public function enableUser($userId) {
  413. return $this->setEnabled($userId, true);
  414. }
  415. /**
  416. * @param string $userId
  417. * @param bool $value
  418. * @return DataResponse
  419. * @throws OCSException
  420. * @throws OCSForbiddenException
  421. */
  422. private function setEnabled($userId, $value) {
  423. $currentLoggedInUser = $this->userSession->getUser();
  424. $targetUser = $this->userManager->get($userId);
  425. if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
  426. throw new OCSException('', 101);
  427. }
  428. // If not permitted
  429. $subAdminManager = $this->groupManager->getSubAdmin();
  430. if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
  431. throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
  432. }
  433. // enable/disable the user now
  434. $targetUser->setEnabled($value);
  435. return new DataResponse();
  436. }
  437. /**
  438. * @NoAdminRequired
  439. * @NoSubAdminRequired
  440. *
  441. * @param string $userId
  442. * @return DataResponse
  443. * @throws OCSException
  444. */
  445. public function getUsersGroups($userId) {
  446. $loggedInUser = $this->userSession->getUser();
  447. $targetUser = $this->userManager->get($userId);
  448. if($targetUser === null) {
  449. throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
  450. }
  451. if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
  452. // Self lookup or admin lookup
  453. return new DataResponse([
  454. 'groups' => $this->groupManager->getUserGroupIds($targetUser)
  455. ]);
  456. } else {
  457. $subAdminManager = $this->groupManager->getSubAdmin();
  458. // Looking up someone else
  459. if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
  460. // Return the group that the method caller is subadmin of for the user in question
  461. /** @var IGroup[] $getSubAdminsGroups */
  462. $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
  463. foreach ($getSubAdminsGroups as $key => $group) {
  464. $getSubAdminsGroups[$key] = $group->getGID();
  465. }
  466. $groups = array_intersect(
  467. $getSubAdminsGroups,
  468. $this->groupManager->getUserGroupIds($targetUser)
  469. );
  470. return new DataResponse(['groups' => $groups]);
  471. } else {
  472. // Not permitted
  473. throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
  474. }
  475. }
  476. }
  477. /**
  478. * @PasswordConfirmationRequired
  479. * @NoAdminRequired
  480. *
  481. * @param string $userId
  482. * @param string $groupid
  483. * @return DataResponse
  484. * @throws OCSException
  485. */
  486. public function addToGroup($userId, $groupid = '') {
  487. if($groupid === '') {
  488. throw new OCSException('', 101);
  489. }
  490. $group = $this->groupManager->get($groupid);
  491. $targetUser = $this->userManager->get($userId);
  492. if($group === null) {
  493. throw new OCSException('', 102);
  494. }
  495. if($targetUser === null) {
  496. throw new OCSException('', 103);
  497. }
  498. // If they're not an admin, check they are a subadmin of the group in question
  499. $loggedInUser = $this->userSession->getUser();
  500. $subAdminManager = $this->groupManager->getSubAdmin();
  501. if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
  502. throw new OCSException('', 104);
  503. }
  504. // Add user to group
  505. $group->addUser($targetUser);
  506. return new DataResponse();
  507. }
  508. /**
  509. * @PasswordConfirmationRequired
  510. * @NoAdminRequired
  511. *
  512. * @param string $userId
  513. * @param string $groupid
  514. * @return DataResponse
  515. * @throws OCSException
  516. */
  517. public function removeFromGroup($userId, $groupid) {
  518. $loggedInUser = $this->userSession->getUser();
  519. if($groupid === null) {
  520. throw new OCSException('', 101);
  521. }
  522. $group = $this->groupManager->get($groupid);
  523. if($group === null) {
  524. throw new OCSException('', 102);
  525. }
  526. $targetUser = $this->userManager->get($userId);
  527. if($targetUser === null) {
  528. throw new OCSException('', 103);
  529. }
  530. // If they're not an admin, check they are a subadmin of the group in question
  531. $subAdminManager = $this->groupManager->getSubAdmin();
  532. if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
  533. throw new OCSException('', 104);
  534. }
  535. // Check they aren't removing themselves from 'admin' or their 'subadmin; group
  536. if ($userId === $loggedInUser->getUID()) {
  537. if ($this->groupManager->isAdmin($loggedInUser->getUID())) {
  538. if ($group->getGID() === 'admin') {
  539. throw new OCSException('Cannot remove yourself from the admin group', 105);
  540. }
  541. } else {
  542. // Not an admin, so the user must be a subadmin of this group, but that is not allowed.
  543. throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
  544. }
  545. } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
  546. /** @var IGroup[] $subAdminGroups */
  547. $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
  548. $subAdminGroups = array_map(function (IGroup $subAdminGroup) {
  549. return $subAdminGroup->getGID();
  550. }, $subAdminGroups);
  551. $userGroups = $this->groupManager->getUserGroupIds($targetUser);
  552. $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);
  553. if (count($userSubAdminGroups) <= 1) {
  554. // Subadmin must not be able to remove a user from all their subadmin groups.
  555. throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105);
  556. }
  557. }
  558. // Remove user from group
  559. $group->removeUser($targetUser);
  560. return new DataResponse();
  561. }
  562. /**
  563. * Creates a subadmin
  564. *
  565. * @PasswordConfirmationRequired
  566. *
  567. * @param string $userId
  568. * @param string $groupid
  569. * @return DataResponse
  570. * @throws OCSException
  571. */
  572. public function addSubAdmin($userId, $groupid) {
  573. $group = $this->groupManager->get($groupid);
  574. $user = $this->userManager->get($userId);
  575. // Check if the user exists
  576. if($user === null) {
  577. throw new OCSException('User does not exist', 101);
  578. }
  579. // Check if group exists
  580. if($group === null) {
  581. throw new OCSException('Group:'.$groupid.' does not exist', 102);
  582. }
  583. // Check if trying to make subadmin of admin group
  584. if(strtolower($groupid) === 'admin') {
  585. throw new OCSException('Cannot create subadmins for admin group', 103);
  586. }
  587. $subAdminManager = $this->groupManager->getSubAdmin();
  588. // We cannot be subadmin twice
  589. if ($subAdminManager->isSubAdminofGroup($user, $group)) {
  590. return new DataResponse();
  591. }
  592. // Go
  593. if($subAdminManager->createSubAdmin($user, $group)) {
  594. return new DataResponse();
  595. } else {
  596. throw new OCSException('Unknown error occurred', 103);
  597. }
  598. }
  599. /**
  600. * Removes a subadmin from a group
  601. *
  602. * @PasswordConfirmationRequired
  603. *
  604. * @param string $userId
  605. * @param string $groupid
  606. * @return DataResponse
  607. * @throws OCSException
  608. */
  609. public function removeSubAdmin($userId, $groupid) {
  610. $group = $this->groupManager->get($groupid);
  611. $user = $this->userManager->get($userId);
  612. $subAdminManager = $this->groupManager->getSubAdmin();
  613. // Check if the user exists
  614. if($user === null) {
  615. throw new OCSException('User does not exist', 101);
  616. }
  617. // Check if the group exists
  618. if($group === null) {
  619. throw new OCSException('Group does not exist', 101);
  620. }
  621. // Check if they are a subadmin of this said group
  622. if(!$subAdminManager->isSubAdminofGroup($user, $group)) {
  623. throw new OCSException('User is not a subadmin of this group', 102);
  624. }
  625. // Go
  626. if($subAdminManager->deleteSubAdmin($user, $group)) {
  627. return new DataResponse();
  628. } else {
  629. throw new OCSException('Unknown error occurred', 103);
  630. }
  631. }
  632. /**
  633. * Get the groups a user is a subadmin of
  634. *
  635. * @param string $userId
  636. * @return DataResponse
  637. * @throws OCSException
  638. */
  639. public function getUserSubAdminGroups($userId) {
  640. $user = $this->userManager->get($userId);
  641. // Check if the user exists
  642. if($user === null) {
  643. throw new OCSException('User does not exist', 101);
  644. }
  645. // Get the subadmin groups
  646. $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
  647. foreach ($groups as $key => $group) {
  648. $groups[$key] = $group->getGID();
  649. }
  650. if(!$groups) {
  651. throw new OCSException('Unknown error occurred', 102);
  652. } else {
  653. return new DataResponse($groups);
  654. }
  655. }
  656. /**
  657. * @param string $userId
  658. * @return array
  659. * @throws \OCP\Files\NotFoundException
  660. */
  661. protected function fillStorageInfo($userId) {
  662. try {
  663. \OC_Util::tearDownFS();
  664. \OC_Util::setupFS($userId);
  665. $storage = OC_Helper::getStorageInfo('/');
  666. $data = [
  667. 'free' => $storage['free'],
  668. 'used' => $storage['used'],
  669. 'total' => $storage['total'],
  670. 'relative' => $storage['relative'],
  671. 'quota' => $storage['quota'],
  672. ];
  673. } catch (NotFoundException $ex) {
  674. $data = [];
  675. }
  676. return $data;
  677. }
  678. /**
  679. * @NoAdminRequired
  680. * @PasswordConfirmationRequired
  681. *
  682. * resend welcome message
  683. *
  684. * @param string $userId
  685. * @return DataResponse
  686. * @throws OCSException
  687. */
  688. public function resendWelcomeMessage($userId) {
  689. $currentLoggedInUser = $this->userSession->getUser();
  690. $targetUser = $this->userManager->get($userId);
  691. if($targetUser === null) {
  692. throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
  693. }
  694. // Check if admin / subadmin
  695. $subAdminManager = $this->groupManager->getSubAdmin();
  696. if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
  697. && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
  698. // No rights
  699. throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
  700. }
  701. $email = $targetUser->getEMailAddress();
  702. if ($email === '' || $email === null) {
  703. throw new OCSException('Email address not available', 101);
  704. }
  705. $username = $targetUser->getUID();
  706. $lang = $this->config->getUserValue($username, 'core', 'lang', 'en');
  707. if (!$this->l10nFactory->languageExists('settings', $lang)) {
  708. $lang = 'en';
  709. }
  710. $l10n = $this->l10nFactory->get('settings', $lang);
  711. // data for the mail template
  712. $mailData = [
  713. 'username' => $username,
  714. 'url' => $this->urlGenerator->getAbsoluteURL('/')
  715. ];
  716. // FIXME: set users language in email
  717. $mail = new TemplateResponse('settings', 'email.new_user', $mailData, 'blank');
  718. $mailContent = $mail->render();
  719. // FIXME: set users language in email
  720. $mail = new TemplateResponse('settings', 'email.new_user_plain_text', $mailData, 'blank');
  721. $plainTextMailContent = $mail->render();
  722. $subject = $l10n->t('Your %s account was created', [$this->defaults->getName()]);
  723. try {
  724. $message = $this->mailer->createMessage();
  725. $message->setTo([$email => $username]);
  726. $message->setSubject($subject);
  727. $message->setHtmlBody($mailContent);
  728. $message->setPlainBody($plainTextMailContent);
  729. $message->setFrom([$this->fromMailAddress => $this->defaults->getName()]);
  730. $this->mailer->send($message);
  731. } catch(\Exception $e) {
  732. $this->logger->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings'));
  733. throw new OCSException('Sending email failed', 102);
  734. }
  735. return new DataResponse();
  736. }
  737. }