From bc0cc515712c029eeeec8097b706dbe68137bd66 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 5 Apr 2024 15:20:12 -0700 Subject: [PATCH 1/9] Removed nested try-catch --- lib/Controller/AccountController.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/Controller/AccountController.php b/lib/Controller/AccountController.php index 17d6e4c9..33aa8f57 100644 --- a/lib/Controller/AccountController.php +++ b/lib/Controller/AccountController.php @@ -156,11 +156,7 @@ class AccountController extends Controller { $this->session->remove(self::SESSION_USERNAME_CHECK); $this->session->remove(self::CAPTCHA_VERIFIED_CHECK); - try { - $this->userService->addUsernameToCommonDataStore($username); - } catch (Exception $e) { - $this->logger->logException($e, ['app' => Application::APP_ID]); - } + $this->userService->addUsernameToCommonDataStore($username); $response->setStatus(200); $response->setData(['success' => true]); -- GitLab From 56b148aba521f5f2954e6013efaa7b169ac84296 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 8 Apr 2024 11:33:13 -0700 Subject: [PATCH 2/9] exception added --- .../AddUsernameToCommonStoreException.php | 9 +++++++++ lib/Service/UserService.php | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 lib/Exception/AddUsernameToCommonStoreException.php diff --git a/lib/Exception/AddUsernameToCommonStoreException.php b/lib/Exception/AddUsernameToCommonStoreException.php new file mode 100644 index 00000000..c7fa6da0 --- /dev/null +++ b/lib/Exception/AddUsernameToCommonStoreException.php @@ -0,0 +1,9 @@ +apiConfig['commonServicesURL']; $commonApiVersion = $this->apiConfig['commonApiVersion']; @@ -505,7 +516,7 @@ class UserService { $this->curl->post($url, $params, $headers); if ($this->curl->getLastStatusCode() !== 200) { - throw new Exception("Error adding username '$username' to common data store."); + throw new AddUsernameToCommonStoreException("Error adding username '$username' to common data store."); } } } -- GitLab From dc4d6214a6f144284dd002020d6549dbdde69cd2 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 8 Apr 2024 11:35:57 -0700 Subject: [PATCH 3/9] exception added --- lib/Controller/AccountController.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/Controller/AccountController.php b/lib/Controller/AccountController.php index 33aa8f57..001ab1dc 100644 --- a/lib/Controller/AccountController.php +++ b/lib/Controller/AccountController.php @@ -23,6 +23,8 @@ use OCP\ISession; use OCP\IURLGenerator; use OCP\IUserSession; use OCP\L10N\IFactory; +use OCA\EcloudAccounts\Exception\AddUsernameToCommonStoreException; +use OCA\EcloudAccounts\Exception\LDAPUserCreationException; class AccountController extends Controller { protected $appName; @@ -164,6 +166,10 @@ class AccountController extends Controller { $this->logger->logException($e, ['app' => Application::APP_ID]); $response->setData(['message' => 'A server-side error occurred while processing your request! Please try again later.', 'success' => false]); $response->setStatus(500); + } catch (AddUsernameToCommonStoreException | Error $e) { + $this->logger->logException($e, ['app' => Application::APP_ID]); + $response->setData(['message' => 'A server-side error occurred while processing your request! Please try again later.', 'success' => false]); + $response->setStatus(500); } catch (Exception $e) { $this->logger->logException($e, ['app' => Application::APP_ID]); $response->setData(['message' => 'An error occurred while creating your account!', 'success' => false]); -- GitLab From e8cf8bd5e47baee1fbea064b8632473cfb853941 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 8 Apr 2024 14:26:13 -0700 Subject: [PATCH 4/9] exception added --- lib/Controller/AccountController.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/Controller/AccountController.php b/lib/Controller/AccountController.php index 001ab1dc..8d0de814 100644 --- a/lib/Controller/AccountController.php +++ b/lib/Controller/AccountController.php @@ -8,6 +8,8 @@ namespace OCA\EcloudAccounts\Controller; use Exception; use OCA\EcloudAccounts\AppInfo\Application; +use OCA\EcloudAccounts\Exception\AddUsernameToCommonStoreException; +use OCA\EcloudAccounts\Exception\LDAPUserCreationException; use OCA\EcloudAccounts\Service\CaptchaService; use OCA\EcloudAccounts\Service\NewsLetterService; use OCA\EcloudAccounts\Service\UserService; @@ -23,8 +25,6 @@ use OCP\ISession; use OCP\IURLGenerator; use OCP\IUserSession; use OCP\L10N\IFactory; -use OCA\EcloudAccounts\Exception\AddUsernameToCommonStoreException; -use OCA\EcloudAccounts\Exception\LDAPUserCreationException; class AccountController extends Controller { protected $appName; @@ -162,11 +162,7 @@ class AccountController extends Controller { $response->setStatus(200); $response->setData(['success' => true]); - } catch (LDAPUserCreationException | Error $e) { - $this->logger->logException($e, ['app' => Application::APP_ID]); - $response->setData(['message' => 'A server-side error occurred while processing your request! Please try again later.', 'success' => false]); - $response->setStatus(500); - } catch (AddUsernameToCommonStoreException | Error $e) { + } catch (LDAPUserCreationException | AddUsernameToCommonStoreException | Error $e) { $this->logger->logException($e, ['app' => Application::APP_ID]); $response->setData(['message' => 'A server-side error occurred while processing your request! Please try again later.', 'success' => false]); $response->setStatus(500); -- GitLab From ea54c00c0e706b1dd9f145e5c2ee358f4c497717 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Mon, 8 Apr 2024 16:25:54 -0700 Subject: [PATCH 5/9] changed exception --- lib/Controller/AccountController.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/Controller/AccountController.php b/lib/Controller/AccountController.php index 8d0de814..3dae3d79 100644 --- a/lib/Controller/AccountController.php +++ b/lib/Controller/AccountController.php @@ -162,10 +162,14 @@ class AccountController extends Controller { $response->setStatus(200); $response->setData(['success' => true]); - } catch (LDAPUserCreationException | AddUsernameToCommonStoreException | Error $e) { + } catch (LDAPUserCreationException | Error $e) { $this->logger->logException($e, ['app' => Application::APP_ID]); $response->setData(['message' => 'A server-side error occurred while processing your request! Please try again later.', 'success' => false]); $response->setStatus(500); + } catch (AddUsernameToCommonStoreException $e) { + $this->logger->logException($e, ['app' => Application::APP_ID]); + $response->setStatus(200); + $response->setData(['success' => true]); } catch (Exception $e) { $this->logger->logException($e, ['app' => Application::APP_ID]); $response->setData(['message' => 'An error occurred while creating your account!', 'success' => false]); -- GitLab From 4dc950f091570e56d0f6f8db0d20abefa211faa8 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 12 Apr 2024 15:35:10 -0700 Subject: [PATCH 6/9] Convertion of Quota in MB for nextcloud --- lib/Controller/AccountController.php | 4 ++-- lib/Service/UserService.php | 19 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/Controller/AccountController.php b/lib/Controller/AccountController.php index 3dae3d79..35a14372 100644 --- a/lib/Controller/AccountController.php +++ b/lib/Controller/AccountController.php @@ -139,10 +139,10 @@ class AccountController extends Controller { try { $mainDomain = $this->userService->getMainDomain(); $userEmail = $username.'@'.$mainDomain; - $newUserEntry = $this->userService->registerUser($displayname, $recoveryEmail, $username, $userEmail, $password); + $this->userService->registerUser($displayname, $recoveryEmail, $username, $userEmail, $password); sleep(2); - $this->userService->setAccountDataLocally($username, $userEmail, $newUserEntry['quota']); + $this->userService->setAccountDataLocally($username, $userEmail); $this->userService->createHMEAlias($username, $userEmail); $this->userService->createNewDomainAlias($username, $userEmail); $this->userService->setTOS($username, true); diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index c39f4c04..fc29f48d 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -242,11 +242,11 @@ class UserService { * @param string $userEmail The email address of the user. * @param string $password The password chosen by the user. * - * @return array An array containing information about the registered user. + * @return void * @throws Exception If the username or recovery email is already taken. * @throws LDAPUserCreationException If there is an error adding new entry to LDAP store */ - public function registerUser(string $displayname, string $recoveryEmail, string $username, string $userEmail, string $password): array { + public function registerUser(string $displayname, string $recoveryEmail, string $username, string $userEmail, string $password): void { if ($this->userExists($username)) { throw new Exception("Username '$username' is already taken."); @@ -254,7 +254,7 @@ class UserService { if (!empty($recoveryEmail)) { $this->validateRecoveryEmail($recoveryEmail); } - return $this->addNewUserToLDAP($displayname, $recoveryEmail, $username, $userEmail, $password); + $this->addNewUserToLDAP($displayname, $recoveryEmail, $username, $userEmail, $password); } /** * Validates the recovery email address. @@ -283,15 +283,17 @@ class UserService { * @param string $userEmail The email address of the new user. * @param string $password The password of the new user. * - * @return array Information about the added user. + * @return void * @throws LDAPUserCreationException If there is an error adding new entry to LDAP store */ - private function addNewUserToLDAP(string $displayName, string $recoveryEmail, string $username, string $userEmail, string $password): ?array { + private function addNewUserToLDAP(string $displayName, string $recoveryEmail, string $username, string $userEmail, string $password): void { $connection = $this->LDAPConnectionService->getLDAPConnection(); $base = $this->LDAPConnectionService->getLDAPBaseUsers()[0]; $newUserDN = "username=$username," . $base; + $quota = $this->LDAPConnectionService->getLdapQuota() * 1024 * 1024; + $quota = intval($quota); $newUserEntry = [ 'mailAddress' => $userEmail, 'username' => $username, @@ -305,13 +307,12 @@ class UserService { 'userClusterID' => $this->apiConfig['userCluserId'], 'objectClass' => $this->apiConfig['objectClass'] ]; - + $ret = ldap_add($connection, $newUserDN, $newUserEntry); if (!$ret) { throw new LDAPUserCreationException("Error while adding entry to LDAP for username: " . $username . ' Error: ' . ldap_error($connection), ldap_errno($connection)); } - return $newUserEntry; } /** * Check if a recovery email address is available (not already taken by another user). @@ -443,14 +444,14 @@ class UserService { * * @return void */ - public function setAccountDataLocally(string $uid, string $mailAddress, string $quota): void { + public function setAccountDataLocally(string $uid, string $mailAddress): void { $user = $this->getUser($uid); if (is_null($user)) { throw new Exception("User with username '$uid' not found."); } // Set the email address for the user $user->setEMailAddress($mailAddress); - + $quota = $this->LDAPConnectionService->getLdapQuota(); // Format and set the quota for the user (in megabytes) $quota = strval($quota) . ' MB'; $user->setQuota($quota); -- GitLab From f1e4193d8fd73a55fa55b8139e1e8008755946cc Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 12 Apr 2024 15:37:07 -0700 Subject: [PATCH 7/9] php fix --- lib/Service/UserService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index fc29f48d..965ffe08 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -242,7 +242,7 @@ class UserService { * @param string $userEmail The email address of the user. * @param string $password The password chosen by the user. * - * @return void + * @return void * @throws Exception If the username or recovery email is already taken. * @throws LDAPUserCreationException If there is an error adding new entry to LDAP store */ -- GitLab From 39ab8cf5764177abf4f8e54da13f90bcd1ebf090 Mon Sep 17 00:00:00 2001 From: Ronak Patel Date: Fri, 12 Apr 2024 18:30:13 -0700 Subject: [PATCH 8/9] added config --- lib/Service/UserService.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index 965ffe08..bb49693c 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -292,8 +292,8 @@ class UserService { $newUserDN = "username=$username," . $base; - $quota = $this->LDAPConnectionService->getLdapQuota() * 1024 * 1024; - $quota = intval($quota); + $quota = $this->getDefaultQuota() * 1024 * 1024; + $newUserEntry = [ 'mailAddress' => $userEmail, 'username' => $username, @@ -451,7 +451,7 @@ class UserService { } // Set the email address for the user $user->setEMailAddress($mailAddress); - $quota = $this->LDAPConnectionService->getLdapQuota(); + $quota = $this->getDefaultQuota(); // Format and set the quota for the user (in megabytes) $quota = strval($quota) . ' MB'; $user->setQuota($quota); @@ -520,4 +520,7 @@ class UserService { throw new AddUsernameToCommonStoreException("Error adding username '$username' to common data store."); } } + private function getDefaultQuota() { + return $this->config->getSystemValue('default_quota_in_megabytes', 1024); + } } -- GitLab From d0104d380b7ab056c4eff9123666d51bb5392ee4 Mon Sep 17 00:00:00 2001 From: Akhil Date: Fri, 12 Apr 2024 14:39:42 +0000 Subject: [PATCH 9/9] Apply 1 suggestion(s) to 1 file(s) --- lib/Service/UserService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Service/UserService.php b/lib/Service/UserService.php index bb49693c..a347655c 100644 --- a/lib/Service/UserService.php +++ b/lib/Service/UserService.php @@ -521,6 +521,6 @@ class UserService { } } private function getDefaultQuota() { - return $this->config->getSystemValue('default_quota_in_megabytes', 1024); + return $this->config->getSystemValueInt('default_quota_in_megabytes', 1024); } } -- GitLab