Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit c7166395 authored by Akhil's avatar Akhil
Browse files

Check if username is taken at common db

parent 0dfa4c36
Loading
Loading
Loading
Loading
+19 −2
Original line number Diff line number Diff line
@@ -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,
@@ -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],
@@ -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;
	}
	/**
@@ -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;
	}

+13 −0
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@ namespace OCA\EcloudAccounts\Service;
use Exception;

class CurlService {

	private int $lastStatusCode = 0;
	/**
	 * GET alias for request method
	 *
@@ -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
@@ -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);
+29 −0
Original line number Diff line number Diff line
@@ -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);
	}
}