diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 96bf75a37539c26557bb82a72b046240ac03cfd9..dd71cf834bf26cb080846e3f201b70b17d35ee71 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -4,13 +4,13 @@ declare(strict_types=1); namespace OCA\EcloudAccounts\Controller; +use \Psr\Log\LoggerInterface; use Exception; use OCA\EcloudAccounts\Db\MailUsageMapper; use OCA\EcloudAccounts\Service\UserService; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http\DataResponse; use OCP\IConfig; -use OCP\ILogger; use OCP\IRequest; class UserController extends ApiController { @@ -23,7 +23,7 @@ class UserController extends ApiController { private $config; - public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper) { + public function __construct($appName, IRequest $request, LoggerInterface $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper) { parent::__construct($appName, $request); $this->userService = $userService; $this->mailUsageMapper = $mailUsageMapper; @@ -67,36 +67,62 @@ class UserController extends ApiController { */ public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', bool $tosAccepted = false): DataResponse { $response = new DataResponse(); - + $this->logger->info('API CALLED. UID: ' . $uid); + if (!$this->checkAppCredentials($token)) { + $this->logger->error('checkAppCredentials Failed token:'.$token); + $response->setData(['error' => 'checkAppCredentials Failed token:'.$token]); $response->setStatus(401); return $response; } - if (!$this->userService->userExists($uid)) { + $exists = false; + if ($this->userService->userExists($uid)) { + $exists = true; + } + // To check for old accounts + $legacyDomain = $this->config->getSystemValue('legacy_domain'); + $legacyDomainSuffix = !empty($legacyDomain) ? '@' . $legacyDomain : ''; + if (!$exists && stristr($uid, $legacyDomainSuffix) === false) { + $exists = $this->userService->userExists($uid . $legacyDomainSuffix); + } + if (!$exists) { + $this->logger->error('userExists Failed'); + $response->setData(['error' => 'userExists Failed UID: '.$uid]); $response->setStatus(404); return $response; } - + $user = $this->userService->getUser($uid); - + if (is_null($user)) { $response->setStatus(404); + $response->setData(['error' => 'User not found']); + $this->logger->error('User not found'); return $response; } - + + $this->logger->info('Setting Email address: ' . $email); $user->setEMailAddress($email); + + $this->logger->info('Setting quota: ' . $quota); $user->setQuota($quota); + + $this->logger->info('Sending welcome email...'); $this->userService->sendWelcomeEmail($uid, $email); + $this->config->setUserValue($uid, 'terms_of_service', 'tosAccepted', intval($tosAccepted)); + $recoveryEmailUpdated = $this->userService->setRecoveryEmail($uid, $recoveryEmail); if (!$recoveryEmailUpdated) { return $this->getErrorResponse($response, 'error_setting_recovery', 400); } + $hmeAliasAdded = $this->userService->addHMEAliasInConfig($uid, $hmeAlias); if (!$hmeAliasAdded) { return $this->getErrorResponse($response, 'error_adding_hme_alias', 400); } + return $response; }