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

Commit c2417183 authored by Akhil's avatar Akhil 🙂
Browse files

Added HME alias creation

parent baf9c0f5
Loading
Loading
Loading
Loading
+0 −8
Original line number Diff line number Diff line
@@ -5,10 +5,8 @@
```php
    'e_welcome_secret' => 'secret', // Secret to authenticate request to the welcome server
    'e_welcome_domain' => 'welcome.ecloud.global', // Domain of welcome server
    'user_folder_sharding' => false, // Whether or not user folder sharding has to be enabled
    'ecloud-accounts' => [
        'secret' => 'ecloud-accounts-secret', // Secret for incoming requests to authenticate against
        'realdatadirectory' => '/var/www/realdatadirectory' // Directory where folders for sharding are mounted
    ]
```

@@ -16,12 +14,6 @@

- This plugin creates an endpoint `/apps/ecloud-accounts/api/set_account_data` that is to be used to set user's email, quota,recovery email and create the user's folder if necessary

## User folder sharding
- When user folder sharding is enabled, the user's folder is created in one of the folders in the specified "real" data directory and the folder is assigned to the user randomly
- Then a `symlink` is created linking the user's folder in the nextcloud data directory to the user's folder in the "real" data directory
- If the `user_folder_sharding` config key is set to `true`, ensure to set `realdatadirectory` config key in the `ecloud-accounts` configuration to the location where your folders are mounted 
- In case `user_folder_sharding` is not set in your `config.php`, it defaults to `false`

## Drop account

- The drop account functionality plugin works in conjunction with the drop_account plugin : https://apps.nextcloud.com/apps/drop_account
+5 −10
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ class UserController extends ApiController
     * @PublicPage
     * @NoCSRFRequired
     */
    public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $quota = '1024 MB'): DataResponse
    public function setAccountData(string $token, string $uid, string $email, string $recoveryEmail, string $hmeAlias, string $quota = '1024 MB'): DataResponse
    {
        
        $response = new DataResponse();
@@ -81,15 +81,10 @@ class UserController extends ApiController
        if (!$recoveryEmailUpdated) {
            return $this->getErrorResponse($response, 'error_setting_recovery', 400);
        }

        $createdFolder = true;
        if ($this->userService->isShardingEnabled()) {
            $createdFolder = $this->userService->createUserFolder($uid);
        }
        if (!$createdFolder) {
            $response->setStatus(500);
        $hmeAliasAdded = $this->userService->addHMEAliasInConfig($uid, $hmeAlias);
        if (!$hmeAliasAdded) {
            return $this->getErrorResponse($response, 'error_adding_hme_alias', 400);
        }

        return $response;
    }

+16 −42
Original line number Diff line number Diff line
@@ -28,18 +28,13 @@ class UserService
        $this->appConfig = $this->config->getSystemValue($appName);
    }

    public function isShardingEnabled(): bool
    {
        $shardingEnabled = $this->config->getSystemValue('user_folder_sharding', false);
        return $shardingEnabled;
    }

    public function getConfigValue(string $key)
    public function getConfigValue(string $key, mixed $default = false)
    {
        if (!empty($this->appConfig[$key])) {
            return $this->appConfig[$key];
        }
        return false;
        return $default;
    }


@@ -63,46 +58,25 @@ class UserService
        }
    }

    public function createUserFolder(string $uid): bool
    public function getHMEAliasesFromConfig($uid) : array
    {

        $realDataDir = $this->getConfigValue('realdatadirectory');
        $ncDataDir = $this->config->getSystemValue('datadirectory');
        $ncUserFolder = $ncDataDir . '/' . $uid;

        // return false if no realDataDir specified and sharding is enabled
        // As user data directory can't be created in correct location
        if (!$realDataDir) {
            return false;
        $aliases = $this->config->getUserValue($uid, 'hide-my-email', 'email-aliases', []);
        if (!empty($aliases)) {
            $aliases = json_decode($aliases, true);
        }

        // Folder already exists 
        if (file_exists($ncUserFolder)) {
            return true;
        return $aliases;
    }

        // Randomly assign a directory for the new user
        $directories = glob($realDataDir . '/*', GLOB_ONLYDIR);
        $folderIndex = random_int(0, count($directories) - 1);
        $folder = $directories[$folderIndex];
        $realUserFolder = $folder . '/' .  $uid;

    public function addHMEAliasInConfig($uid, $alias) : bool
    {
        $aliases = $this->getHMEAliasesFromConfig($uid);
        $aliases[] = $alias;
        $aliases = json_encode($aliases);
        try {
            $created = mkdir($realUserFolder);
            if (!$created) {
                $this->logger->error('Error while creating user folder for user: ' . $uid);
                return false;
            }
            $linked = symlink($realUserFolder, $ncUserFolder);
            if (!$linked) {
                $this->logger->error('Error while linking user folder for user: ' . $uid);
                return false;
            }
            $this->config->setUserValue($uid, 'hide-my-email', 'email-aliases', $aliases);
            return true;
        } catch (Exception $e) {
            $this->logger->error("Error while creating user folder and linking for user: " . $uid);
        } catch(UnexpectedValueException $e) {
            return false;
        } 
    }

}