diff --git a/lib/Controller/AccountController.php b/lib/Controller/AccountController.php index 17d6e4c923ee18a5a2d396e290e005d553c4a4e5..35a14372164a04b531e2dc722bb98a02b1318234 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; @@ -137,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); @@ -156,11 +158,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]); @@ -168,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 $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]); diff --git a/lib/Exception/AddUsernameToCommonStoreException.php b/lib/Exception/AddUsernameToCommonStoreException.php new file mode 100644 index 0000000000000000000000000000000000000000..c7fa6da0b5b4d7dea382d82eebaf5c50d98a2be6 --- /dev/null +++ b/lib/Exception/AddUsernameToCommonStoreException.php @@ -0,0 +1,9 @@ +userExists($username)) { throw new Exception("Username '$username' is already taken."); @@ -253,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. @@ -282,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 = $this->getDefaultQuota() * 1024 * 1024; + $newUserEntry = [ 'mailAddress' => $userEmail, 'username' => $username, @@ -304,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). @@ -442,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->getDefaultQuota(); // Format and set the quota for the user (in megabytes) $quota = strval($quota) . ' MB'; $user->setQuota($quota); @@ -482,7 +484,17 @@ class UserService { } throw new Exception("Error checking if username '$username' is taken at common source, status code: " . (string) $statusCode); } - + /** + * Adds a username to the common data store. + * + * This method sends a POST request to the common data store API endpoint to add a username. + * If the operation is successful, the username will be added to the data store. + * If the operation fails, an exception will be thrown. + * + * @param string $username The username to add to the common data store. + * + * @throws AddUsernameToCommonStoreException If an error occurs while adding the username to the common data store. + */ public function addUsernameToCommonDataStore(string $username) : void { $commonServicesURL = $this->apiConfig['commonServicesURL']; $commonApiVersion = $this->apiConfig['commonApiVersion']; @@ -505,7 +517,10 @@ 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."); } } + private function getDefaultQuota() { + return $this->config->getSystemValueInt('default_quota_in_megabytes', 1024); + } }