Loading lib/Controller/AccountController.php +19 −2 Original line number Diff line number Diff line Loading @@ -30,6 +30,8 @@ class AccountController extends Controller { private $session; private $userSession; private $urlGenerator; private const SESSION_USERNAME_CHECK = 'username_check_passed'; public function __construct( $AppName, IRequest $request, Loading Loading @@ -92,6 +94,12 @@ class AccountController extends Controller { return $response; } if (!$this->session->get(self::SESSION_USERNAME_CHECK)) { $response->setData(['message' => 'Username is already taken.', 'success' => false]); $response->setStatus(400); return $response; } $inputData = [ 'username' => ['value' => $username, 'maxLength' => 30], 'displayname' => ['value' => $displayname, 'maxLength' => 30], Loading Loading @@ -132,6 +140,8 @@ class AccountController extends Controller { $response->setData(['message' => $e->getMessage(), 'success' => false]); $response->setStatus(500); } $this->session->remove(self::SESSION_USERNAME_CHECK); return $response; } /** Loading Loading @@ -168,9 +178,16 @@ class AccountController extends Controller { public function checkUsernameAvailable(string $username) : DataResponse { $response = new DataResponse(); $response->setStatus(400); if (!$this->userService->userExists($username)) { if (empty($username)) { return $response; } if (!$this->userService->userExists($username) && !$this->userService->isUsernameTaken($username)) { $response->setStatus(200); } $this->session->set('username_check_passed', true); return $response; } Loading lib/Service/CurlService.php +13 −0 Original line number Diff line number Diff line Loading @@ -14,6 +14,8 @@ namespace OCA\EcloudAccounts\Service; use Exception; class CurlService { private int $lastStatusCode = 0; /** * GET alias for request method * Loading Loading @@ -44,6 +46,14 @@ class CurlService { return $this->request('DELETE', $url, $params, $headers, $userOptions); } /** * @return int */ public function getLastStatusCode() : int { return $this->getLastStatusCode; } /** * Curl run request Loading Loading @@ -90,6 +100,9 @@ class CurlService { $response = curl_exec($ch); $this->lastStatusCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($errno = curl_errno($ch)) { var_dump($errno); $errorMessage = curl_strerror($errno); Loading lib/Service/UserService.php +29 −0 Original line number Diff line number Diff line Loading @@ -389,4 +389,33 @@ class UserService { $quota = strval($quota) . ' MB'; $user->setQuota($quota); } public function isUsernameTaken(string $username) : bool { $commonApiUrl = $this->apiConfig['commonApiUrl']; $commonApiVersion = $this->apiConfig['commonApiVersion']; if (!isset($commonApiUrl) || empty($commonApiUrl)) { return false; } $endpoint = $commonApiVersion . '/users/'; $url = $commonApiUrl . $endpoint . $username; $token = $this->apiConfig['commonServiceToken']; $headers = [ "Authorization: Bearer $token" ]; $this->curl->get($url, [], $headers); $statusCode = $this->curl->getLastStatusCode(); if ($statusCode === 404) { return false; } if ($statusCode === 200) { return true; } throw new Exception("Error checking if username is taken at common source, status code: " . (string) $statusCode); } } Loading
lib/Controller/AccountController.php +19 −2 Original line number Diff line number Diff line Loading @@ -30,6 +30,8 @@ class AccountController extends Controller { private $session; private $userSession; private $urlGenerator; private const SESSION_USERNAME_CHECK = 'username_check_passed'; public function __construct( $AppName, IRequest $request, Loading Loading @@ -92,6 +94,12 @@ class AccountController extends Controller { return $response; } if (!$this->session->get(self::SESSION_USERNAME_CHECK)) { $response->setData(['message' => 'Username is already taken.', 'success' => false]); $response->setStatus(400); return $response; } $inputData = [ 'username' => ['value' => $username, 'maxLength' => 30], 'displayname' => ['value' => $displayname, 'maxLength' => 30], Loading Loading @@ -132,6 +140,8 @@ class AccountController extends Controller { $response->setData(['message' => $e->getMessage(), 'success' => false]); $response->setStatus(500); } $this->session->remove(self::SESSION_USERNAME_CHECK); return $response; } /** Loading Loading @@ -168,9 +178,16 @@ class AccountController extends Controller { public function checkUsernameAvailable(string $username) : DataResponse { $response = new DataResponse(); $response->setStatus(400); if (!$this->userService->userExists($username)) { if (empty($username)) { return $response; } if (!$this->userService->userExists($username) && !$this->userService->isUsernameTaken($username)) { $response->setStatus(200); } $this->session->set('username_check_passed', true); return $response; } Loading
lib/Service/CurlService.php +13 −0 Original line number Diff line number Diff line Loading @@ -14,6 +14,8 @@ namespace OCA\EcloudAccounts\Service; use Exception; class CurlService { private int $lastStatusCode = 0; /** * GET alias for request method * Loading Loading @@ -44,6 +46,14 @@ class CurlService { return $this->request('DELETE', $url, $params, $headers, $userOptions); } /** * @return int */ public function getLastStatusCode() : int { return $this->getLastStatusCode; } /** * Curl run request Loading Loading @@ -90,6 +100,9 @@ class CurlService { $response = curl_exec($ch); $this->lastStatusCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($errno = curl_errno($ch)) { var_dump($errno); $errorMessage = curl_strerror($errno); Loading
lib/Service/UserService.php +29 −0 Original line number Diff line number Diff line Loading @@ -389,4 +389,33 @@ class UserService { $quota = strval($quota) . ' MB'; $user->setQuota($quota); } public function isUsernameTaken(string $username) : bool { $commonApiUrl = $this->apiConfig['commonApiUrl']; $commonApiVersion = $this->apiConfig['commonApiVersion']; if (!isset($commonApiUrl) || empty($commonApiUrl)) { return false; } $endpoint = $commonApiVersion . '/users/'; $url = $commonApiUrl . $endpoint . $username; $token = $this->apiConfig['commonServiceToken']; $headers = [ "Authorization: Bearer $token" ]; $this->curl->get($url, [], $headers); $statusCode = $this->curl->getLastStatusCode(); if ($statusCode === 404) { return false; } if ($statusCode === 200) { return true; } throw new Exception("Error checking if username is taken at common source, status code: " . (string) $statusCode); } }