123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820 |
- <?php
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author michag86 <micha_g@arcor.de>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- * @author Tom Needham <tom@owncloud.com>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace OCA\Provisioning_API\Controller;
- use OC\Accounts\AccountManager;
- use \OC_Helper;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\AppFramework\Http\TemplateResponse;
- use OCP\AppFramework\OCS\OCSException;
- use OCP\AppFramework\OCS\OCSForbiddenException;
- use OCP\AppFramework\OCSController;
- use OCP\Files\NotFoundException;
- use OCP\IConfig;
- use OCP\IGroup;
- use OCP\IGroupManager;
- use OCP\ILogger;
- use OCP\IRequest;
- use OCP\IURLGenerator;
- use OCP\IUserManager;
- use OCP\IUserSession;
- use OCP\L10N\IFactory;
- use OCP\Mail\IMailer;
- class UsersController extends OCSController {
- /** @var IUserManager */
- private $userManager;
- /** @var IConfig */
- private $config;
- /** @var IGroupManager|\OC\Group\Manager */ // FIXME Requires a method that is not on the interface
- private $groupManager;
- /** @var IUserSession */
- private $userSession;
- /** @var AccountManager */
- private $accountManager;
- /** @var ILogger */
- private $logger;
- /** @var string */
- private $fromMailAddress;
- /** @var IURLGenerator */
- private $urlGenerator;
- /** @var IMailer */
- private $mailer;
- /** @var \OC_Defaults */
- private $defaults;
- /** @var IFactory */
- private $l10nFactory;
- /**
- * @param string $appName
- * @param IRequest $request
- * @param IUserManager $userManager
- * @param IConfig $config
- * @param IGroupManager $groupManager
- * @param IUserSession $userSession
- * @param AccountManager $accountManager
- * @param ILogger $logger
- * @param string $fromMailAddress
- * @param IURLGenerator $urlGenerator
- * @param IMailer $mailer
- * @param \OC_Defaults $defaults
- * @param IFactory $l10nFactory
- */
- public function __construct($appName,
- IRequest $request,
- IUserManager $userManager,
- IConfig $config,
- IGroupManager $groupManager,
- IUserSession $userSession,
- AccountManager $accountManager,
- ILogger $logger,
- $fromMailAddress,
- IURLGenerator $urlGenerator,
- IMailer $mailer,
- \OC_Defaults $defaults,
- IFactory $l10nFactory) {
- parent::__construct($appName, $request);
- $this->userManager = $userManager;
- $this->config = $config;
- $this->groupManager = $groupManager;
- $this->userSession = $userSession;
- $this->accountManager = $accountManager;
- $this->logger = $logger;
- $this->fromMailAddress = $fromMailAddress;
- $this->urlGenerator = $urlGenerator;
- $this->mailer = $mailer;
- $this->defaults = $defaults;
- $this->l10nFactory = $l10nFactory;
- }
- /**
- * @NoAdminRequired
- *
- * returns a list of users
- *
- * @param string $search
- * @param int $limit
- * @param int $offset
- * @return DataResponse
- */
- public function getUsers($search = '', $limit = null, $offset = null) {
- $user = $this->userSession->getUser();
- $users = [];
- // Admin? Or SubAdmin?
- $uid = $user->getUID();
- $subAdminManager = $this->groupManager->getSubAdmin();
- if($this->groupManager->isAdmin($uid)){
- $users = $this->userManager->search($search, $limit, $offset);
- } else if ($subAdminManager->isSubAdmin($user)) {
- $subAdminOfGroups = $subAdminManager->getSubAdminsGroups($user);
- foreach ($subAdminOfGroups as $key => $group) {
- $subAdminOfGroups[$key] = $group->getGID();
- }
- if($offset === null) {
- $offset = 0;
- }
- $users = [];
- foreach ($subAdminOfGroups as $group) {
- $users = array_merge($users, $this->groupManager->displayNamesInGroup($group, $search));
- }
- $users = array_slice($users, $offset, $limit);
- }
- $users = array_keys($users);
- return new DataResponse([
- 'users' => $users
- ]);
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userid
- * @param string $password
- * @param array $groups
- * @return DataResponse
- * @throws OCSException
- */
- public function addUser($userid, $password, $groups = null) {
- $user = $this->userSession->getUser();
- $isAdmin = $this->groupManager->isAdmin($user->getUID());
- $subAdminManager = $this->groupManager->getSubAdmin();
- if($this->userManager->userExists($userid)) {
- $this->logger->error('Failed addUser attempt: User already exists.', ['app' => 'ocs_api']);
- throw new OCSException('User already exists', 102);
- }
- if(is_array($groups)) {
- foreach ($groups as $group) {
- if(!$this->groupManager->groupExists($group)) {
- throw new OCSException('group '.$group.' does not exist', 104);
- }
- if(!$isAdmin && !$subAdminManager->isSubAdminofGroup($user, $this->groupManager->get($group))) {
- throw new OCSException('insufficient privileges for group '. $group, 105);
- }
- }
- } else {
- if(!$isAdmin) {
- throw new OCSException('no group specified (required for subadmins)', 106);
- }
- }
- try {
- $newUser = $this->userManager->createUser($userid, $password);
- $this->logger->info('Successful addUser call with userid: '.$userid, ['app' => 'ocs_api']);
- if (is_array($groups)) {
- foreach ($groups as $group) {
- $this->groupManager->get($group)->addUser($newUser);
- $this->logger->info('Added userid '.$userid.' to group '.$group, ['app' => 'ocs_api']);
- }
- }
- return new DataResponse();
- } catch (\Exception $e) {
- $this->logger->error('Failed addUser attempt with exception: '.$e->getMessage(), ['app' => 'ocs_api']);
- throw new OCSException('Bad request', 101);
- }
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * gets user info
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- */
- public function getUser($userId) {
- $data = $this->getUserData($userId);
- return new DataResponse($data);
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * gets user info from the currently logged in user
- *
- * @return DataResponse
- * @throws OCSException
- */
- public function getCurrentUser() {
- $user = $this->userSession->getUser();
- if ($user) {
- $data = $this->getUserData($user->getUID());
- // rename "displayname" to "display-name" only for this call to keep
- // the API stable.
- $data['display-name'] = $data['displayname'];
- unset($data['displayname']);
- return new DataResponse($data);
- }
- throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
- }
- /**
- * creates a array with all user data
- *
- * @param $userId
- * @return array
- * @throws OCSException
- */
- protected function getUserData($userId) {
- $currentLoggedInUser = $this->userSession->getUser();
- $data = [];
- // Check if the target user exists
- $targetUserObject = $this->userManager->get($userId);
- if($targetUserObject === null) {
- throw new OCSException('The requested user could not be found', \OCP\API::RESPOND_NOT_FOUND);
- }
- // Admin? Or SubAdmin?
- if($this->groupManager->isAdmin($currentLoggedInUser->getUID())
- || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
- $data['enabled'] = $this->config->getUserValue($userId, 'core', 'enabled', 'true');
- } else {
- // Check they are looking up themselves
- if($currentLoggedInUser->getUID() !== $userId) {
- throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
- }
- }
- $userAccount = $this->accountManager->getUser($targetUserObject);
- $groups = $this->groupManager->getUserGroups($targetUserObject);
- $gids = [];
- foreach ($groups as $group) {
- $gids[] = $group->getDisplayName();
- }
- // Find the data
- $data['id'] = $targetUserObject->getUID();
- $data['quota'] = $this->fillStorageInfo($userId);
- $data['email'] = $targetUserObject->getEMailAddress();
- $data['displayname'] = $targetUserObject->getDisplayName();
- $data['phone'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_PHONE]['value'];
- $data['address'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_ADDRESS]['value'];
- $data['webpage'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_WEBSITE]['value'];
- $data['twitter'] = $userAccount[\OC\Accounts\AccountManager::PROPERTY_TWITTER]['value'];
- $data['groups'] = $gids;
- return $data;
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- * @PasswordConfirmationRequired
- *
- * edit users
- *
- * @param string $userId
- * @param string $key
- * @param string $value
- * @return DataResponse
- * @throws OCSException
- * @throws OCSForbiddenException
- */
- public function editUser($userId, $key, $value) {
- $currentLoggedInUser = $this->userSession->getUser();
- $targetUser = $this->userManager->get($userId);
- if($targetUser === null) {
- throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
- }
- $permittedFields = [];
- if($userId === $currentLoggedInUser->getUID()) {
- // Editing self (display, email)
- $permittedFields[] = 'display';
- $permittedFields[] = 'email';
- $permittedFields[] = 'password';
- // If admin they can edit their own quota
- if($this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
- $permittedFields[] = 'quota';
- }
- } else {
- // Check if admin / subadmin
- $subAdminManager = $this->groupManager->getSubAdmin();
- if($subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
- || $this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
- // They have permissions over the user
- $permittedFields[] = 'display';
- $permittedFields[] = 'quota';
- $permittedFields[] = 'password';
- $permittedFields[] = 'email';
- } else {
- // No rights
- throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
- }
- }
- // Check if permitted to edit this field
- if(!in_array($key, $permittedFields)) {
- throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
- }
- // Process the edit
- switch($key) {
- case 'display':
- $targetUser->setDisplayName($value);
- break;
- case 'quota':
- $quota = $value;
- if($quota !== 'none' && $quota !== 'default') {
- if (is_numeric($quota)) {
- $quota = (float) $quota;
- } else {
- $quota = \OCP\Util::computerFileSize($quota);
- }
- if ($quota === false) {
- throw new OCSException('Invalid quota value '.$value, 103);
- }
- if($quota === 0) {
- $quota = 'default';
- }else if($quota === -1) {
- $quota = 'none';
- } else {
- $quota = \OCP\Util::humanFileSize($quota);
- }
- }
- $targetUser->setQuota($quota);
- break;
- case 'password':
- $targetUser->setPassword($value);
- break;
- case 'email':
- if(filter_var($value, FILTER_VALIDATE_EMAIL)) {
- $targetUser->setEMailAddress($value);
- } else {
- throw new OCSException('', 102);
- }
- break;
- default:
- throw new OCSException('', 103);
- }
- return new DataResponse();
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- * @throws OCSForbiddenException
- */
- public function deleteUser($userId) {
- $currentLoggedInUser = $this->userSession->getUser();
- $targetUser = $this->userManager->get($userId);
- if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
- throw new OCSException('', 101);
- }
- // If not permitted
- $subAdminManager = $this->groupManager->getSubAdmin();
- if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
- throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
- }
- // Go ahead with the delete
- if($targetUser->delete()) {
- return new DataResponse();
- } else {
- throw new OCSException('', 101);
- }
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- * @throws OCSForbiddenException
- */
- public function disableUser($userId) {
- return $this->setEnabled($userId, false);
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- * @throws OCSForbiddenException
- */
- public function enableUser($userId) {
- return $this->setEnabled($userId, true);
- }
- /**
- * @param string $userId
- * @param bool $value
- * @return DataResponse
- * @throws OCSException
- * @throws OCSForbiddenException
- */
- private function setEnabled($userId, $value) {
- $currentLoggedInUser = $this->userSession->getUser();
- $targetUser = $this->userManager->get($userId);
- if($targetUser === null || $targetUser->getUID() === $currentLoggedInUser->getUID()) {
- throw new OCSException('', 101);
- }
- // If not permitted
- $subAdminManager = $this->groupManager->getSubAdmin();
- if(!$this->groupManager->isAdmin($currentLoggedInUser->getUID()) && !$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)) {
- throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
- }
- // enable/disable the user now
- $targetUser->setEnabled($value);
- return new DataResponse();
- }
- /**
- * @NoAdminRequired
- * @NoSubAdminRequired
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- */
- public function getUsersGroups($userId) {
- $loggedInUser = $this->userSession->getUser();
- $targetUser = $this->userManager->get($userId);
- if($targetUser === null) {
- throw new OCSException('', \OCP\API::RESPOND_NOT_FOUND);
- }
- if($targetUser->getUID() === $loggedInUser->getUID() || $this->groupManager->isAdmin($loggedInUser->getUID())) {
- // Self lookup or admin lookup
- return new DataResponse([
- 'groups' => $this->groupManager->getUserGroupIds($targetUser)
- ]);
- } else {
- $subAdminManager = $this->groupManager->getSubAdmin();
- // Looking up someone else
- if($subAdminManager->isUserAccessible($loggedInUser, $targetUser)) {
- // Return the group that the method caller is subadmin of for the user in question
- /** @var IGroup[] $getSubAdminsGroups */
- $getSubAdminsGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
- foreach ($getSubAdminsGroups as $key => $group) {
- $getSubAdminsGroups[$key] = $group->getGID();
- }
- $groups = array_intersect(
- $getSubAdminsGroups,
- $this->groupManager->getUserGroupIds($targetUser)
- );
- return new DataResponse(['groups' => $groups]);
- } else {
- // Not permitted
- throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
- }
- }
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userId
- * @param string $groupid
- * @return DataResponse
- * @throws OCSException
- */
- public function addToGroup($userId, $groupid = '') {
- if($groupid === '') {
- throw new OCSException('', 101);
- }
- $group = $this->groupManager->get($groupid);
- $targetUser = $this->userManager->get($userId);
- if($group === null) {
- throw new OCSException('', 102);
- }
- if($targetUser === null) {
- throw new OCSException('', 103);
- }
- // If they're not an admin, check they are a subadmin of the group in question
- $loggedInUser = $this->userSession->getUser();
- $subAdminManager = $this->groupManager->getSubAdmin();
- if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
- throw new OCSException('', 104);
- }
- // Add user to group
- $group->addUser($targetUser);
- return new DataResponse();
- }
- /**
- * @PasswordConfirmationRequired
- * @NoAdminRequired
- *
- * @param string $userId
- * @param string $groupid
- * @return DataResponse
- * @throws OCSException
- */
- public function removeFromGroup($userId, $groupid) {
- $loggedInUser = $this->userSession->getUser();
- if($groupid === null) {
- throw new OCSException('', 101);
- }
- $group = $this->groupManager->get($groupid);
- if($group === null) {
- throw new OCSException('', 102);
- }
- $targetUser = $this->userManager->get($userId);
- if($targetUser === null) {
- throw new OCSException('', 103);
- }
- // If they're not an admin, check they are a subadmin of the group in question
- $subAdminManager = $this->groupManager->getSubAdmin();
- if (!$this->groupManager->isAdmin($loggedInUser->getUID()) && !$subAdminManager->isSubAdminOfGroup($loggedInUser, $group)) {
- throw new OCSException('', 104);
- }
- // Check they aren't removing themselves from 'admin' or their 'subadmin; group
- if ($userId === $loggedInUser->getUID()) {
- if ($this->groupManager->isAdmin($loggedInUser->getUID())) {
- if ($group->getGID() === 'admin') {
- throw new OCSException('Cannot remove yourself from the admin group', 105);
- }
- } else {
- // Not an admin, so the user must be a subadmin of this group, but that is not allowed.
- throw new OCSException('Cannot remove yourself from this group as you are a SubAdmin', 105);
- }
- } else if (!$this->groupManager->isAdmin($loggedInUser->getUID())) {
- /** @var IGroup[] $subAdminGroups */
- $subAdminGroups = $subAdminManager->getSubAdminsGroups($loggedInUser);
- $subAdminGroups = array_map(function (IGroup $subAdminGroup) {
- return $subAdminGroup->getGID();
- }, $subAdminGroups);
- $userGroups = $this->groupManager->getUserGroupIds($targetUser);
- $userSubAdminGroups = array_intersect($subAdminGroups, $userGroups);
- if (count($userSubAdminGroups) <= 1) {
- // Subadmin must not be able to remove a user from all their subadmin groups.
- throw new OCSException('Cannot remove user from this group as this is the only remaining group you are a SubAdmin of', 105);
- }
- }
- // Remove user from group
- $group->removeUser($targetUser);
- return new DataResponse();
- }
- /**
- * Creates a subadmin
- *
- * @PasswordConfirmationRequired
- *
- * @param string $userId
- * @param string $groupid
- * @return DataResponse
- * @throws OCSException
- */
- public function addSubAdmin($userId, $groupid) {
- $group = $this->groupManager->get($groupid);
- $user = $this->userManager->get($userId);
- // Check if the user exists
- if($user === null) {
- throw new OCSException('User does not exist', 101);
- }
- // Check if group exists
- if($group === null) {
- throw new OCSException('Group:'.$groupid.' does not exist', 102);
- }
- // Check if trying to make subadmin of admin group
- if(strtolower($groupid) === 'admin') {
- throw new OCSException('Cannot create subadmins for admin group', 103);
- }
- $subAdminManager = $this->groupManager->getSubAdmin();
- // We cannot be subadmin twice
- if ($subAdminManager->isSubAdminofGroup($user, $group)) {
- return new DataResponse();
- }
- // Go
- if($subAdminManager->createSubAdmin($user, $group)) {
- return new DataResponse();
- } else {
- throw new OCSException('Unknown error occurred', 103);
- }
- }
- /**
- * Removes a subadmin from a group
- *
- * @PasswordConfirmationRequired
- *
- * @param string $userId
- * @param string $groupid
- * @return DataResponse
- * @throws OCSException
- */
- public function removeSubAdmin($userId, $groupid) {
- $group = $this->groupManager->get($groupid);
- $user = $this->userManager->get($userId);
- $subAdminManager = $this->groupManager->getSubAdmin();
- // Check if the user exists
- if($user === null) {
- throw new OCSException('User does not exist', 101);
- }
- // Check if the group exists
- if($group === null) {
- throw new OCSException('Group does not exist', 101);
- }
- // Check if they are a subadmin of this said group
- if(!$subAdminManager->isSubAdminofGroup($user, $group)) {
- throw new OCSException('User is not a subadmin of this group', 102);
- }
- // Go
- if($subAdminManager->deleteSubAdmin($user, $group)) {
- return new DataResponse();
- } else {
- throw new OCSException('Unknown error occurred', 103);
- }
- }
- /**
- * Get the groups a user is a subadmin of
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- */
- public function getUserSubAdminGroups($userId) {
- $user = $this->userManager->get($userId);
- // Check if the user exists
- if($user === null) {
- throw new OCSException('User does not exist', 101);
- }
- // Get the subadmin groups
- $groups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
- foreach ($groups as $key => $group) {
- $groups[$key] = $group->getGID();
- }
- if(!$groups) {
- throw new OCSException('Unknown error occurred', 102);
- } else {
- return new DataResponse($groups);
- }
- }
- /**
- * @param string $userId
- * @return array
- * @throws \OCP\Files\NotFoundException
- */
- protected function fillStorageInfo($userId) {
- try {
- \OC_Util::tearDownFS();
- \OC_Util::setupFS($userId);
- $storage = OC_Helper::getStorageInfo('/');
- $data = [
- 'free' => $storage['free'],
- 'used' => $storage['used'],
- 'total' => $storage['total'],
- 'relative' => $storage['relative'],
- 'quota' => $storage['quota'],
- ];
- } catch (NotFoundException $ex) {
- $data = [];
- }
- return $data;
- }
- /**
- * @NoAdminRequired
- * @PasswordConfirmationRequired
- *
- * resend welcome message
- *
- * @param string $userId
- * @return DataResponse
- * @throws OCSException
- */
- public function resendWelcomeMessage($userId) {
- $currentLoggedInUser = $this->userSession->getUser();
- $targetUser = $this->userManager->get($userId);
- if($targetUser === null) {
- throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
- }
- // Check if admin / subadmin
- $subAdminManager = $this->groupManager->getSubAdmin();
- if(!$subAdminManager->isUserAccessible($currentLoggedInUser, $targetUser)
- && !$this->groupManager->isAdmin($currentLoggedInUser->getUID())) {
- // No rights
- throw new OCSException('', \OCP\API::RESPOND_UNAUTHORISED);
- }
- $email = $targetUser->getEMailAddress();
- if ($email === '' || $email === null) {
- throw new OCSException('Email address not available', 101);
- }
- $username = $targetUser->getUID();
- $lang = $this->config->getUserValue($username, 'core', 'lang', 'en');
- if (!$this->l10nFactory->languageExists('settings', $lang)) {
- $lang = 'en';
- }
- $l10n = $this->l10nFactory->get('settings', $lang);
- // data for the mail template
- $mailData = [
- 'username' => $username,
- 'url' => $this->urlGenerator->getAbsoluteURL('/')
- ];
- // FIXME: set users language in email
- $mail = new TemplateResponse('settings', 'email.new_user', $mailData, 'blank');
- $mailContent = $mail->render();
- // FIXME: set users language in email
- $mail = new TemplateResponse('settings', 'email.new_user_plain_text', $mailData, 'blank');
- $plainTextMailContent = $mail->render();
- $subject = $l10n->t('Your %s account was created', [$this->defaults->getName()]);
- try {
- $message = $this->mailer->createMessage();
- $message->setTo([$email => $username]);
- $message->setSubject($subject);
- $message->setHtmlBody($mailContent);
- $message->setPlainBody($plainTextMailContent);
- $message->setFrom([$this->fromMailAddress => $this->defaults->getName()]);
- $this->mailer->send($message);
- } catch(\Exception $e) {
- $this->logger->error("Can't send new user mail to $email: " . $e->getMessage(), array('app' => 'settings'));
- throw new OCSException('Sending email failed', 102);
- }
- return new DataResponse();
- }
- }
|