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

Commit 3d60e4dc authored by Nivesh Krishna's avatar Nivesh Krishna
Browse files

check if alias already registered

parent 4a533bc1
Loading
Loading
Loading
Loading
+37 −10
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ class BaseEcloudAccountCreator implements AccountCreator
{
    private string $ecloudUrl;
    private string $ecloudAccountsApiUrl;
    private string $commonApiUrl;
    protected int $quota = 1024;
    protected bool $usernameIsEmail = true;

@@ -14,6 +15,10 @@ class BaseEcloudAccountCreator implements AccountCreator
    {
        $this->ecloudUrl = endsWith($ecloudUrl, "/") ? $ecloudUrl : $ecloudUrl . "/";
        $this->ecloudAccountsApiUrl = $this->ecloudUrl . 'apps/ecloud-accounts/api/';

        $this->commonApiUrl = getenv('COMMON_SERVICES_URL');
        $this->commonApiUrl = endsWith($this->commonApiUrl, '/') ? $this->commonApiUrl : $this->commonApiUrl . '/';

        $quota = getenv('CLOUD_QUOTA_IN_MB');
        if ($quota !== false) {
            $this->quota = intval($quota);
@@ -35,7 +40,7 @@ class BaseEcloudAccountCreator implements AccountCreator
        $id = "e_cloud_account_data";
        try {
            // We check if account with uid set to email or username exists
            if ($this->isUsernameTaken($userData->username)) {
            if ($this->isUsernameTaken($userData->username) || $this->isAliasTaken($userData->username)) {
                return new \ValidatedData($id, "error_account_taken");
            }
        } catch (\Error $_) {
@@ -48,18 +53,16 @@ class BaseEcloudAccountCreator implements AccountCreator
    {
        try {
            $hmeAlias = '';
            $commonApiUrl = getenv('COMMON_SERVICES_URL');
            $commonApiUrl = endsWith($commonApiUrl, '/') ? $commonApiUrl : $commonApiUrl . '/';
            
            $aliasDomain =  getenv('ALIAS_DOMAIN');
            // Create HME Alias
            $hmeAlias = $this->createHMEAlias($email, $commonApiUrl, $commonApiVersion, $aliasDomain);
            $hmeAlias = $this->createHMEAlias($email, $this->commonApiUrl, $commonApiVersion, $aliasDomain);
            // Create Alias to new domain
            // $this->createNewDomainAlias($username, $email, $commonApiUrl, $commonApiVersion, $aliasDomain);

            // Create alias with same name as email pointing to email to block this alias
            $domain = getMailDomain();
            $this->createNewDomainAlias($username, $email, $commonApiUrl, $commonApiVersion, $domain);
            $this->createNewDomainAlias($username, $email, $this->commonApiUrl, $commonApiVersion, $domain);
        } catch (Error $e) {
            error_log('Error during alias creation for user: ' . $username . ' with email: ' . $email . ' : ' . $e->getMessage());
        }
@@ -74,11 +77,13 @@ class BaseEcloudAccountCreator implements AccountCreator
        $endpoint = $commonApiVersion . '/aliases/hide-my-email/';
        $url = $commonApiUrl . $endpoint . $resultmail;
        $data = array(
            "token" => $token,
            "domain" => $domain
        );
        $headers = [
            "Authorization: Bearer $token"
        ];

        $result = curlPostJSON($url, $data);
        $result = curlRequest('POST', $url, $headers, $data);
        $output = $result->output;
        if ($result->statusCode != 200) {
            $err = $output->message;
@@ -96,11 +101,14 @@ class BaseEcloudAccountCreator implements AccountCreator
        $url = $commonApiUrl . $endpoint . $resultmail;

        $data = array(
            "token" => $token,
            "alias" => $alias,
            "domain" => $domain
        );
        $result = curlPostJSON($url, $data);
        $headers = [
            "Authorization: Bearer $token"
        ];

        $result = curlRequest('POST', $url, $headers, $data);
        $output = $result->output;
        if ($result->statusCode != 200) {
            $err = $output->message;
@@ -183,6 +191,25 @@ class BaseEcloudAccountCreator implements AccountCreator
        return $output;
    }

    private function isAliasTaken(string $alias): bool
    {
        $token = getenv('COMMON_SERVICES_TOKEN');

        $endpoint = "v2/aliases/hide-my-email?alias=$alias";
        $url = $this->commonApiUrl . $endpoint;
      
        $headers = [
            "Authorization: Bearer $token"
        ];

        $result = curlRequest('GET', $url, $headers);
        $statusCode = $result->statusCode;
        if ($statusCode !== 200 || $statusCode !== 404) {
            throw new Error("Error with request to check if alias exists, status code : " . $statusCode);
        }
        return $statusCode === 200;
    }

    protected function createAccount(string $resultmail, string $username, string $pw, string $pw2, string $name, int $quota, string $authmail, ?string $referrerCode = null)
    {
    }
+10 −3
Original line number Diff line number Diff line
@@ -18,14 +18,21 @@ function sendAPIResponse($response_code, $message)
    respond_with_json($message);
}

function curlPostJSON(string $url, array $data) : stdClass
function curlRequest(string $method, string $url, array $headers = [], array $data = []) : stdClass
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

    curl_setopt($ch, CURLOPT_URL, $url);
    if (!empty($headers)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }

    if ('POST' === $method && !empty($data)) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    }
    
    $output = curl_exec($ch);
    $output = json_decode($output, false);
    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);