From e8fee956a42cafe7b8004c12656d4b5c1b2b1616 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Thu, 27 Apr 2023 14:47:35 +0530 Subject: [PATCH 01/20] tos service set --- lib/Controller/UserController.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index dd87f13e..06ad0a37 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -11,11 +11,14 @@ use OCP\IConfig; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http\DataResponse; use OCA\EcloudAccounts\Service\UserService; +use OCA\TermsOfService\Service\SignatoryService ; use OCA\EcloudAccounts\Db\MailUsageMapper; class UserController extends ApiController { /** @var UserService */ private $userService; + /** @var SignatoryService */ + private $signatoryService; private $mailUsageMapper; @@ -23,12 +26,13 @@ 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, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper, SignatoryService $signatoryService) { parent::__construct($appName, $request); $this->userService = $userService; $this->mailUsageMapper = $mailUsageMapper; $this->logger = $logger; $this->config = $config; + $this->signatoryService = $signatoryService; } /** @@ -87,7 +91,10 @@ class UserController extends ApiController { $user->setEMailAddress($email); $user->setQuota($quota); - + $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); + if (!$tosSignatoryInserted) { + return $this->getErrorResponse($response, 'error_setting_tos', 400); + } $recoveryEmailUpdated = $this->userService->setRecoveryEmail($uid, $recoveryEmail); if (!$recoveryEmailUpdated) { return $this->getErrorResponse($response, 'error_setting_recovery', 400); -- GitLab From 71af9e9438b787c725800c3507fe79f18efc0dcb Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 28 Apr 2023 10:15:08 +0530 Subject: [PATCH 02/20] check tos enabled --- lib/Controller/UserController.php | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 06ad0a37..79995cda 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -10,6 +10,7 @@ use OCP\ILogger; use OCP\IConfig; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http\DataResponse; +use OCP\App\IAppManager; use OCA\EcloudAccounts\Service\UserService; use OCA\TermsOfService\Service\SignatoryService ; use OCA\EcloudAccounts\Db\MailUsageMapper; @@ -26,9 +27,13 @@ class UserController extends ApiController { private $config; - public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper, SignatoryService $signatoryService) { + /** @var IAppManager */ + private $appManager; + + public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper, SignatoryService $signatoryService, IAppManager $appManager) { parent::__construct($appName, $request); $this->userService = $userService; + $this->appManager = $appManager; $this->mailUsageMapper = $mailUsageMapper; $this->logger = $logger; $this->config = $config; @@ -91,9 +96,11 @@ class UserController extends ApiController { $user->setEMailAddress($email); $user->setQuota($quota); - $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); - if (!$tosSignatoryInserted) { - return $this->getErrorResponse($response, 'error_setting_tos', 400); + if ($this->appManager->isEnabledForUser('terms_of_service')) { + $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); + if (!$tosSignatoryInserted) { + return $this->getErrorResponse($response, 'error_setting_tos', 400); + } } $recoveryEmailUpdated = $this->userService->setRecoveryEmail($uid, $recoveryEmail); if (!$recoveryEmailUpdated) { -- GitLab From 8826d89c17270319cabf2ed24d332e6d7132ca98 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 28 Apr 2023 10:17:23 +0530 Subject: [PATCH 03/20] check tos enabled --- lib/Controller/UserController.php | 270 +++++++++++++++--------------- 1 file changed, 139 insertions(+), 131 deletions(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 79995cda..ad5fe7ad 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -15,141 +15,149 @@ use OCA\EcloudAccounts\Service\UserService; use OCA\TermsOfService\Service\SignatoryService ; use OCA\EcloudAccounts\Db\MailUsageMapper; -class UserController extends ApiController { - /** @var UserService */ - private $userService; - /** @var SignatoryService */ - private $signatoryService; +class UserController extends ApiController +{ + /** @var UserService */ + private $userService; + /** @var SignatoryService */ + private $signatoryService; - private $mailUsageMapper; + private $mailUsageMapper; - private $logger; + private $logger; - private $config; + private $config; - /** @var IAppManager */ + /** @var IAppManager */ private $appManager; - public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper, SignatoryService $signatoryService, IAppManager $appManager) { - parent::__construct($appName, $request); - $this->userService = $userService; - $this->appManager = $appManager; - $this->mailUsageMapper = $mailUsageMapper; - $this->logger = $logger; - $this->config = $config; - $this->signatoryService = $signatoryService; - } - - /** - * @CORS - * @PublicPage - * @NoCSRFRequired - */ - public function userExists(string $token, string $uid): DataResponse { - $response = new DataResponse(); - if (!$this->checkAppCredentials($token)) { - $response->setStatus(401); - return $response; - } - - $exists = false; - - if ($this->userService->userExists($uid)) { - $exists = true; - } - - // To check for old accounts - $mailDomain = $this->config->getSystemValue('mail_domain'); - $mailDomainSuffix = !empty($mailDomain) ? '@' . $mailDomain : ''; - if (!$exists && stristr($uid, $mailDomainSuffix) === false) { - $exists = $this->userService->userExists($uid . $mailDomainSuffix); - } - - $response->setData($exists); - return $response; - } - - /** - * @CORS - * @PublicPage - * @NoCSRFRequired - */ - public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB'): DataResponse { - $response = new DataResponse(); - - if (!$this->checkAppCredentials($token)) { - $response->setStatus(401); - return $response; - } - - if (!$this->userService->userExists($uid)) { - $response->setStatus(404); - return $response; - } - - $user = $this->userService->getUser($uid); - - if (is_null($user)) { - $response->setStatus(404); - return $response; - } - - $user->setEMailAddress($email); - $user->setQuota($quota); - if ($this->appManager->isEnabledForUser('terms_of_service')) { - $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); - if (!$tosSignatoryInserted) { - return $this->getErrorResponse($response, 'error_setting_tos', 400); - } - } - $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; - } - - /** - * @CORS - * @PublicPage - * @NoCSRFRequired - */ - public function setMailQuotaUsage(array $usage, string $token): DataResponse { - $response = new DataResponse(); - if (!$this->checkAppCredentials($token)) { - $response->setStatus(401); - return $response; - } - try { - // Explicitly cast input values to integer - $usage = array_map(fn ($value) => (int) $value, $usage); - $this->updateMailQuotaUsageInPreferences($usage); - } catch (Exception $e) { - $statusCode = 500; - $errorMessage = 'error_setting_mail_quota_usage'; - $response = $this->getErrorResponse($response, $errorMessage, $statusCode); - - $this->logger->error($errorMessage . ': ' . $e->getMessage()); - } - return $response; - } - - private function updateMailQuotaUsageInPreferences(array $usage) { - $this->mailUsageMapper->updateUsageInPreferences($usage); - } - - private function getErrorResponse(DataResponse $response, string $error, int $code) { - $response->setStatus($code); - $response->setData(['error' => $error]); - return $response; - } - - private function checkAppCredentials(string $token): bool { - $ecloud_accounts_secret = $this->userService->getConfigValue('secret'); - return hash_equals($ecloud_accounts_secret, $token); - } + public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper, SignatoryService $signatoryService, IAppManager $appManager) + { + parent::__construct($appName, $request); + $this->userService = $userService; + $this->appManager = $appManager; + $this->mailUsageMapper = $mailUsageMapper; + $this->logger = $logger; + $this->config = $config; + $this->signatoryService = $signatoryService; + } + + /** + * @CORS + * @PublicPage + * @NoCSRFRequired + */ + public function userExists(string $token, string $uid): DataResponse + { + $response = new DataResponse(); + if (!$this->checkAppCredentials($token)) { + $response->setStatus(401); + return $response; + } + + $exists = false; + + if ($this->userService->userExists($uid)) { + $exists = true; + } + + // To check for old accounts + $mailDomain = $this->config->getSystemValue('mail_domain'); + $mailDomainSuffix = !empty($mailDomain) ? '@' . $mailDomain : ''; + if (!$exists && stristr($uid, $mailDomainSuffix) === false) { + $exists = $this->userService->userExists($uid . $mailDomainSuffix); + } + + $response->setData($exists); + return $response; + } + + /** + * @CORS + * @PublicPage + * @NoCSRFRequired + */ + public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB'): DataResponse + { + $response = new DataResponse(); + + if (!$this->checkAppCredentials($token)) { + $response->setStatus(401); + return $response; + } + + if (!$this->userService->userExists($uid)) { + $response->setStatus(404); + return $response; + } + + $user = $this->userService->getUser($uid); + + if (is_null($user)) { + $response->setStatus(404); + return $response; + } + + $user->setEMailAddress($email); + $user->setQuota($quota); + if ($this->appManager->isEnabledForUser('terms_of_service')) { + $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); + if (!$tosSignatoryInserted) { + return $this->getErrorResponse($response, 'error_setting_tos', 400); + } + } + $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; + } + + /** + * @CORS + * @PublicPage + * @NoCSRFRequired + */ + public function setMailQuotaUsage(array $usage, string $token): DataResponse + { + $response = new DataResponse(); + if (!$this->checkAppCredentials($token)) { + $response->setStatus(401); + return $response; + } + try { + // Explicitly cast input values to integer + $usage = array_map(fn ($value) => (int) $value, $usage); + $this->updateMailQuotaUsageInPreferences($usage); + } catch (Exception $e) { + $statusCode = 500; + $errorMessage = 'error_setting_mail_quota_usage'; + $response = $this->getErrorResponse($response, $errorMessage, $statusCode); + + $this->logger->error($errorMessage . ': ' . $e->getMessage()); + } + return $response; + } + + private function updateMailQuotaUsageInPreferences(array $usage) + { + $this->mailUsageMapper->updateUsageInPreferences($usage); + } + + private function getErrorResponse(DataResponse $response, string $error, int $code) + { + $response->setStatus($code); + $response->setData(['error' => $error]); + return $response; + } + + private function checkAppCredentials(string $token): bool + { + $ecloud_accounts_secret = $this->userService->getConfigValue('secret'); + return hash_equals($ecloud_accounts_secret, $token); + } } -- GitLab From b3dc8e1071aea5a8bbd90888a941033e00cb02f0 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 28 Apr 2023 10:23:24 +0530 Subject: [PATCH 04/20] check tos enabled --- lib/Controller/UserController.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index ad5fe7ad..da5c95f0 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -21,6 +21,8 @@ class UserController extends ApiController private $userService; /** @var SignatoryService */ private $signatoryService; + /** @var IAppManager */ + private $appManager; private $mailUsageMapper; @@ -28,9 +30,6 @@ class UserController extends ApiController private $config; - /** @var IAppManager */ - private $appManager; - public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper, SignatoryService $signatoryService, IAppManager $appManager) { parent::__construct($appName, $request); -- GitLab From 613f315f34fb467755059b77f896bfa11af3a1c2 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 28 Apr 2023 10:24:52 +0530 Subject: [PATCH 05/20] check tos enabled --- lib/Controller/UserController.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index da5c95f0..e56a7417 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -100,11 +100,11 @@ class UserController extends ApiController $user->setEMailAddress($email); $user->setQuota($quota); if ($this->appManager->isEnabledForUser('terms_of_service')) { - $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); - if (!$tosSignatoryInserted) { - return $this->getErrorResponse($response, 'error_setting_tos', 400); - } - } + $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); + if (!$tosSignatoryInserted) { + return $this->getErrorResponse($response, 'error_setting_tos', 400); + } + } $recoveryEmailUpdated = $this->userService->setRecoveryEmail($uid, $recoveryEmail); if (!$recoveryEmailUpdated) { return $this->getErrorResponse($response, 'error_setting_recovery', 400); -- GitLab From 0b7a0ee4abe0f9c785e19799dd3eac43f2a59fc4 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 28 Apr 2023 10:27:08 +0530 Subject: [PATCH 06/20] check tos enabled --- lib/Controller/UserController.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index e56a7417..405b4579 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -99,12 +99,6 @@ class UserController extends ApiController $user->setEMailAddress($email); $user->setQuota($quota); - if ($this->appManager->isEnabledForUser('terms_of_service')) { - $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); - if (!$tosSignatoryInserted) { - return $this->getErrorResponse($response, 'error_setting_tos', 400); - } - } $recoveryEmailUpdated = $this->userService->setRecoveryEmail($uid, $recoveryEmail); if (!$recoveryEmailUpdated) { return $this->getErrorResponse($response, 'error_setting_recovery', 400); -- GitLab From 2f139da1ccc5af76bf7fee9b03d96f10d1e3cd04 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 28 Apr 2023 10:29:08 +0530 Subject: [PATCH 07/20] check tos enabled --- lib/Controller/UserController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 405b4579..d1f461e2 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -21,8 +21,8 @@ class UserController extends ApiController private $userService; /** @var SignatoryService */ private $signatoryService; - /** @var IAppManager */ - private $appManager; + /** @var IAppManager */ + private $appManager; private $mailUsageMapper; -- GitLab From 6dfd3305c9c79217c9285357d5e3d84cbaa16ec1 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 28 Apr 2023 10:30:46 +0530 Subject: [PATCH 08/20] check tos enabled --- lib/Controller/UserController.php | 270 +++++++++++++++--------------- 1 file changed, 131 insertions(+), 139 deletions(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index d1f461e2..06ad0a37 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -10,147 +10,139 @@ use OCP\ILogger; use OCP\IConfig; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http\DataResponse; -use OCP\App\IAppManager; use OCA\EcloudAccounts\Service\UserService; use OCA\TermsOfService\Service\SignatoryService ; use OCA\EcloudAccounts\Db\MailUsageMapper; -class UserController extends ApiController -{ - /** @var UserService */ - private $userService; - /** @var SignatoryService */ - private $signatoryService; - /** @var IAppManager */ - private $appManager; - - private $mailUsageMapper; - - private $logger; - - private $config; - - public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper, SignatoryService $signatoryService, IAppManager $appManager) - { - parent::__construct($appName, $request); - $this->userService = $userService; - $this->appManager = $appManager; - $this->mailUsageMapper = $mailUsageMapper; - $this->logger = $logger; - $this->config = $config; - $this->signatoryService = $signatoryService; - } - - /** - * @CORS - * @PublicPage - * @NoCSRFRequired - */ - public function userExists(string $token, string $uid): DataResponse - { - $response = new DataResponse(); - if (!$this->checkAppCredentials($token)) { - $response->setStatus(401); - return $response; - } - - $exists = false; - - if ($this->userService->userExists($uid)) { - $exists = true; - } - - // To check for old accounts - $mailDomain = $this->config->getSystemValue('mail_domain'); - $mailDomainSuffix = !empty($mailDomain) ? '@' . $mailDomain : ''; - if (!$exists && stristr($uid, $mailDomainSuffix) === false) { - $exists = $this->userService->userExists($uid . $mailDomainSuffix); - } - - $response->setData($exists); - return $response; - } - - /** - * @CORS - * @PublicPage - * @NoCSRFRequired - */ - public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB'): DataResponse - { - $response = new DataResponse(); - - if (!$this->checkAppCredentials($token)) { - $response->setStatus(401); - return $response; - } - - if (!$this->userService->userExists($uid)) { - $response->setStatus(404); - return $response; - } - - $user = $this->userService->getUser($uid); - - if (is_null($user)) { - $response->setStatus(404); - return $response; - } - - $user->setEMailAddress($email); - $user->setQuota($quota); - $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; - } - - /** - * @CORS - * @PublicPage - * @NoCSRFRequired - */ - public function setMailQuotaUsage(array $usage, string $token): DataResponse - { - $response = new DataResponse(); - if (!$this->checkAppCredentials($token)) { - $response->setStatus(401); - return $response; - } - try { - // Explicitly cast input values to integer - $usage = array_map(fn ($value) => (int) $value, $usage); - $this->updateMailQuotaUsageInPreferences($usage); - } catch (Exception $e) { - $statusCode = 500; - $errorMessage = 'error_setting_mail_quota_usage'; - $response = $this->getErrorResponse($response, $errorMessage, $statusCode); - - $this->logger->error($errorMessage . ': ' . $e->getMessage()); - } - return $response; - } - - private function updateMailQuotaUsageInPreferences(array $usage) - { - $this->mailUsageMapper->updateUsageInPreferences($usage); - } - - private function getErrorResponse(DataResponse $response, string $error, int $code) - { - $response->setStatus($code); - $response->setData(['error' => $error]); - return $response; - } - - private function checkAppCredentials(string $token): bool - { - $ecloud_accounts_secret = $this->userService->getConfigValue('secret'); - return hash_equals($ecloud_accounts_secret, $token); - } +class UserController extends ApiController { + /** @var UserService */ + private $userService; + /** @var SignatoryService */ + private $signatoryService; + + private $mailUsageMapper; + + private $logger; + + private $config; + + public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper, SignatoryService $signatoryService) { + parent::__construct($appName, $request); + $this->userService = $userService; + $this->mailUsageMapper = $mailUsageMapper; + $this->logger = $logger; + $this->config = $config; + $this->signatoryService = $signatoryService; + } + + /** + * @CORS + * @PublicPage + * @NoCSRFRequired + */ + public function userExists(string $token, string $uid): DataResponse { + $response = new DataResponse(); + if (!$this->checkAppCredentials($token)) { + $response->setStatus(401); + return $response; + } + + $exists = false; + + if ($this->userService->userExists($uid)) { + $exists = true; + } + + // To check for old accounts + $mailDomain = $this->config->getSystemValue('mail_domain'); + $mailDomainSuffix = !empty($mailDomain) ? '@' . $mailDomain : ''; + if (!$exists && stristr($uid, $mailDomainSuffix) === false) { + $exists = $this->userService->userExists($uid . $mailDomainSuffix); + } + + $response->setData($exists); + return $response; + } + + /** + * @CORS + * @PublicPage + * @NoCSRFRequired + */ + public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB'): DataResponse { + $response = new DataResponse(); + + if (!$this->checkAppCredentials($token)) { + $response->setStatus(401); + return $response; + } + + if (!$this->userService->userExists($uid)) { + $response->setStatus(404); + return $response; + } + + $user = $this->userService->getUser($uid); + + if (is_null($user)) { + $response->setStatus(404); + return $response; + } + + $user->setEMailAddress($email); + $user->setQuota($quota); + $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); + if (!$tosSignatoryInserted) { + return $this->getErrorResponse($response, 'error_setting_tos', 400); + } + $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; + } + + /** + * @CORS + * @PublicPage + * @NoCSRFRequired + */ + public function setMailQuotaUsage(array $usage, string $token): DataResponse { + $response = new DataResponse(); + if (!$this->checkAppCredentials($token)) { + $response->setStatus(401); + return $response; + } + try { + // Explicitly cast input values to integer + $usage = array_map(fn ($value) => (int) $value, $usage); + $this->updateMailQuotaUsageInPreferences($usage); + } catch (Exception $e) { + $statusCode = 500; + $errorMessage = 'error_setting_mail_quota_usage'; + $response = $this->getErrorResponse($response, $errorMessage, $statusCode); + + $this->logger->error($errorMessage . ': ' . $e->getMessage()); + } + return $response; + } + + private function updateMailQuotaUsageInPreferences(array $usage) { + $this->mailUsageMapper->updateUsageInPreferences($usage); + } + + private function getErrorResponse(DataResponse $response, string $error, int $code) { + $response->setStatus($code); + $response->setData(['error' => $error]); + return $response; + } + + private function checkAppCredentials(string $token): bool { + $ecloud_accounts_secret = $this->userService->getConfigValue('secret'); + return hash_equals($ecloud_accounts_secret, $token); + } } -- GitLab From 0f1499e185fba093fe3a79c4c903e3105a2ce8e4 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 28 Apr 2023 10:34:05 +0530 Subject: [PATCH 09/20] added app manager --- lib/Controller/UserController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 06ad0a37..1635fec7 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -10,6 +10,7 @@ use OCP\ILogger; use OCP\IConfig; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http\DataResponse; +use App\IAppManager; use OCA\EcloudAccounts\Service\UserService; use OCA\TermsOfService\Service\SignatoryService ; use OCA\EcloudAccounts\Db\MailUsageMapper; @@ -19,6 +20,8 @@ class UserController extends ApiController { private $userService; /** @var SignatoryService */ private $signatoryService; + /** @var IAppManager */ + private $appManager; private $mailUsageMapper; -- GitLab From 1690f897d1d102b7459dd776f07aaa6522f7e805 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 28 Apr 2023 10:36:54 +0530 Subject: [PATCH 10/20] added app manager --- lib/Controller/UserController.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 1635fec7..5d00575d 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -29,13 +29,14 @@ class UserController extends ApiController { private $config; - public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper, SignatoryService $signatoryService) { + public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper, SignatoryService $signatoryService, IAppManager $appManager) { parent::__construct($appName, $request); $this->userService = $userService; $this->mailUsageMapper = $mailUsageMapper; $this->logger = $logger; $this->config = $config; $this->signatoryService = $signatoryService; + $this->appManager = $appManager; } /** -- GitLab From 82c0cf868e362e3fad53a32d7dcce84495a2d65d Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 28 Apr 2023 10:38:05 +0530 Subject: [PATCH 11/20] check enabled --- lib/Controller/UserController.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 5d00575d..87369b17 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -95,9 +95,11 @@ class UserController extends ApiController { $user->setEMailAddress($email); $user->setQuota($quota); - $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); - if (!$tosSignatoryInserted) { - return $this->getErrorResponse($response, 'error_setting_tos', 400); + if ($this->appManager->isEnabledForUser('terms_of_service')) { + $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); + if (!$tosSignatoryInserted) { + return $this->getErrorResponse($response, 'error_setting_tos', 400); + } } $recoveryEmailUpdated = $this->userService->setRecoveryEmail($uid, $recoveryEmail); if (!$recoveryEmailUpdated) { -- GitLab From dd11d66facfa197e4423c55e2a98bb4a5a5e110b Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Fri, 28 Apr 2023 10:45:24 +0530 Subject: [PATCH 12/20] check enabled --- lib/Controller/UserController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 87369b17..ee56d1a6 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -10,7 +10,7 @@ use OCP\ILogger; use OCP\IConfig; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http\DataResponse; -use App\IAppManager; +use OCP\App\IAppManager; use OCA\EcloudAccounts\Service\UserService; use OCA\TermsOfService\Service\SignatoryService ; use OCA\EcloudAccounts\Db\MailUsageMapper; -- GitLab From 5c3cc6ddfe5fb2dd39d1a4b114847a4a202caad8 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Tue, 2 May 2023 16:45:03 +0530 Subject: [PATCH 13/20] tos accepted flag set --- lib/Controller/UserController.php | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index ee56d1a6..c750cbd5 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -10,18 +10,12 @@ use OCP\ILogger; use OCP\IConfig; use OCP\AppFramework\ApiController; use OCP\AppFramework\Http\DataResponse; -use OCP\App\IAppManager; use OCA\EcloudAccounts\Service\UserService; -use OCA\TermsOfService\Service\SignatoryService ; use OCA\EcloudAccounts\Db\MailUsageMapper; class UserController extends ApiController { /** @var UserService */ private $userService; - /** @var SignatoryService */ - private $signatoryService; - /** @var IAppManager */ - private $appManager; private $mailUsageMapper; @@ -29,14 +23,12 @@ class UserController extends ApiController { private $config; - public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper, SignatoryService $signatoryService, IAppManager $appManager) { + public function __construct($appName, IRequest $request, ILogger $logger, IConfig $config, UserService $userService, MailUsageMapper $mailUsageMapper) { parent::__construct($appName, $request); $this->userService = $userService; $this->mailUsageMapper = $mailUsageMapper; $this->logger = $logger; $this->config = $config; - $this->signatoryService = $signatoryService; - $this->appManager = $appManager; } /** @@ -73,7 +65,7 @@ class UserController extends ApiController { * @PublicPage * @NoCSRFRequired */ - public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB'): DataResponse { + public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', 84,72: * - postLogin(\OC\User\User $user, string $loginName, string $password, boolean $tosAccepted)): DataResponse { $response = new DataResponse(); if (!$this->checkAppCredentials($token)) { @@ -95,12 +87,7 @@ class UserController extends ApiController { $user->setEMailAddress($email); $user->setQuota($quota); - if ($this->appManager->isEnabledForUser('terms_of_service')) { - $tosSignatoryInserted = $this->signatoryService->tosSignatoryInsert($uid); - if (!$tosSignatoryInserted) { - return $this->getErrorResponse($response, 'error_setting_tos', 400); - } - } + $this->config->setUserValue($uid, 'terms_of_service', 'tosAccepted', $tosAccepted); $recoveryEmailUpdated = $this->userService->setRecoveryEmail($uid, $recoveryEmail); if (!$recoveryEmailUpdated) { return $this->getErrorResponse($response, 'error_setting_recovery', 400); -- GitLab From d452e3ea217d2feae66172b9d200c8a7b8be0d73 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Tue, 2 May 2023 16:46:04 +0530 Subject: [PATCH 14/20] tos accepted flag set --- lib/Controller/UserController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index c750cbd5..6bd33592 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -65,7 +65,7 @@ class UserController extends ApiController { * @PublicPage * @NoCSRFRequired */ - public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', 84,72: * - postLogin(\OC\User\User $user, string $loginName, string $password, boolean $tosAccepted)): DataResponse { + public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', boolean $tosAccepted = 'false'): DataResponse { $response = new DataResponse(); if (!$this->checkAppCredentials($token)) { -- GitLab From a08407ac26553f6372bad5eddb959c79fb6ec510 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Tue, 2 May 2023 16:47:25 +0530 Subject: [PATCH 15/20] tos accepted flag set --- lib/Controller/UserController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 6bd33592..5373f1ea 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -65,7 +65,7 @@ class UserController extends ApiController { * @PublicPage * @NoCSRFRequired */ - public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', boolean $tosAccepted = 'false'): DataResponse { + public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', boolean $tosAccepted = false): DataResponse { $response = new DataResponse(); if (!$this->checkAppCredentials($token)) { -- GitLab From 83a627c3ed965af9cad857e71638c8ab5d472a03 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Tue, 2 May 2023 17:00:47 +0530 Subject: [PATCH 16/20] tos accepted flag set --- lib/Controller/UserController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 5373f1ea..62f0dcd9 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -65,7 +65,7 @@ class UserController extends ApiController { * @PublicPage * @NoCSRFRequired */ - public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', boolean $tosAccepted = false): DataResponse { + public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', bool $tosAccepted = false): DataResponse { $response = new DataResponse(); if (!$this->checkAppCredentials($token)) { -- GitLab From f9e7e7bb5d77777f56ab2172a5256de733787b11 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Tue, 2 May 2023 17:02:48 +0530 Subject: [PATCH 17/20] tos accepted flag set --- lib/Controller/UserController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 62f0dcd9..94f6b1cc 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -65,7 +65,7 @@ class UserController extends ApiController { * @PublicPage * @NoCSRFRequired */ - public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', bool $tosAccepted = false): DataResponse { + public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', bool $tosAccepted = false): DataResponse { $response = new DataResponse(); if (!$this->checkAppCredentials($token)) { -- GitLab From 9f13fa9e679bb6130d41f40a10ffc48d5dd744a9 Mon Sep 17 00:00:00 2001 From: Nivesh Krishna Date: Wed, 3 May 2023 00:32:25 +0530 Subject: [PATCH 18/20] fix setuserval function --- lib/Controller/UserController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 94f6b1cc..72719e7a 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -87,7 +87,7 @@ class UserController extends ApiController { $user->setEMailAddress($email); $user->setQuota($quota); - $this->config->setUserValue($uid, 'terms_of_service', 'tosAccepted', $tosAccepted); + $this->config->setUserValue($uid, 'terms_of_service', 'tosAccepted', strval($tosAccepted)); $recoveryEmailUpdated = $this->userService->setRecoveryEmail($uid, $recoveryEmail); if (!$recoveryEmailUpdated) { return $this->getErrorResponse($response, 'error_setting_recovery', 400); -- GitLab From 9a35393622386871227b765b3a940c938f95a9f0 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Wed, 3 May 2023 11:31:54 +0530 Subject: [PATCH 19/20] tos accepted flag set --- lib/Controller/UserController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 94f6b1cc..e0f257e5 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -65,7 +65,7 @@ class UserController extends ApiController { * @PublicPage * @NoCSRFRequired */ - public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', bool $tosAccepted = false): DataResponse { + public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', bool $tosAccepted): DataResponse { $response = new DataResponse(); if (!$this->checkAppCredentials($token)) { -- GitLab From 0f9230dd219627734149625655736ce041c0d464 Mon Sep 17 00:00:00 2001 From: Avinash Gusain Date: Mon, 8 May 2023 17:20:40 +0530 Subject: [PATCH 20/20] applied suggestion --- lib/Controller/UserController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index 10e5fd5c..975e7aa7 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -65,7 +65,7 @@ class UserController extends ApiController { * @PublicPage * @NoCSRFRequired */ - public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', bool $tosAccepted): DataResponse { + public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB', bool $tosAccepted = false): DataResponse { $response = new DataResponse(); if (!$this->checkAppCredentials($token)) { @@ -87,7 +87,7 @@ class UserController extends ApiController { $user->setEMailAddress($email); $user->setQuota($quota); - $this->config->setUserValue($uid, 'terms_of_service', 'tosAccepted', strval($tosAccepted)); + $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); -- GitLab