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

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

Merge branch 'update-mailbox-quota' into 'nc21'

Update quota in mailbox when  user's quota is changed

See merge request e/infra/selfhost/nextcloud-apps/ecloud-drop-account!9
parents 9afe98fc 4d24efe4
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -10,13 +10,13 @@
    <description><![CDATA[in /e/ cloud self-hosting setup, nextcloud accounts are linked to mail accounts and some other things. This app sets the mail, quota and storage of the user upon creation. 
    It also completes the account deletion by cleaning other parts of the /e/ cloud setup to ensure no more data is retained when a user requests an account deletion.
    This app uses the UserDeletedEvent to invoke scripts in the docker-welcome container of /e/ cloud setup]]></description>
    <version>1.0.0</version>
    <version>1.1.0</version>
    <licence>agpl</licence>
    <author mail="dev@e.email" homepage="https://gitlab.e.foundation/">Akhil Potukuchi</author>
    <namespace>EcloudAccounts</namespace>
    <category>tools</category>
    <bugs>https://gitlab.e.foundation/e/management/issues</bugs>
    <dependencies>
        <nextcloud min-version="21" max-version="21"/>
        <nextcloud min-version="21" max-version="22"/>
    </dependencies>
</info>
+3 −1
Original line number Diff line number Diff line
@@ -32,8 +32,9 @@ use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\User\Events\UserDeletedEvent;
use OCP\User\Events\UserChangedEvent;
use OCA\EcloudAccounts\Listeners\UserDeletedListener;

use OCA\EcloudAccounts\Listeners\UserChangedListener;

class Application extends App implements IBootstrap
{
@@ -48,6 +49,7 @@ class Application extends App implements IBootstrap
    public function register(IRegistrationContext $context): void
    {
        $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
        $context->registerEventListener(UserChangedEvent::class, UserChangedListener::class);
    }

    public function boot(IBootContext $context): void
+30 −21
Original line number Diff line number Diff line
@@ -2,22 +2,27 @@

namespace OCA\EcloudAccounts\Db;

use Exception;
use OCP\IDBConnection;
use OCP\IConfig;
use OCP\ILogger;

class MailUsageMapper
{
    private $db;
    private $config;
    private $logger;

    public function __construct(IDBConnection $db, IConfig $config)
    public function __construct(IDBConnection $db, IConfig $config, ILogger $logger)
    {
        $this->db = $db;
        $this->config = $config;
        $this->logger = $logger;
    }

    public function updateUsageInPreferences(array $usage = [])
    {
        try {
            if (empty($usage)) {
                return;
            }
@@ -39,5 +44,9 @@ class MailUsageMapper
            // Update only configvalue in case entry already exists
            $query .= ' ON DUPLICATE KEY UPDATE configvalue = VALUES(configvalue);';
            $this->db->executeQuery($query, $params);
    
        } catch (Exception $e) {
            $this->logger->error('Error updating mailbox usage! ' . $e->getMessage());
        }
    }
}
+61 −0
Original line number Diff line number Diff line
<?php

namespace OCA\EcloudAccounts\Db;

use OCP\IConfig;
use OCP\ILogger;
use OCA\EcloudAccounts\Exception\DbConnectionParamsException;
use Doctrine\DBAL\DriverManager;
use Exception;

class MailboxMapper
{

    private $config;
    private $conn;
    private $logger;

    public function __construct(IConfig $config, ILogger $logger)
    {
        $this->config = $config;
        $this->logger = $logger;
        $params = $this->getConnectionParams();
        $this->conn = DriverManager::getConnection($params);
    }


    private function getConnectionParams()
    {
        $config = $this->config->getSystemValue('user_backend_sql_raw');

        if (!$config) {
            throw new DbConnectionParamsException('Database connection params for mailbox not available in config.php!');
        }

        $params = [
            'dbname' =>  $config['db_name'],
            'charset' => $config['mariadb_charset'],
            'user' =>  $config['db_user'],
            'password' => $config['db_password'],
            'host' => $config['db_host'],
            'port' => $config['db_port'],
            'driver' => 'pdo_mysql'
        ];
        return $params;
    }

    public function updateMailboxQuota(string $username, int $quotaInMb)
    {
        try {
            $qb = $this->conn->createQueryBuilder();
            $qb->update('mailbox', 'm')
                ->set('m.quota', $quotaInMb)
                ->where('m.username = :username')
                ->setParameter('username', $username);

            $qb->execute();
        } catch (Exception $e) {
            $this->logger->error('Error setting mailbox quota of user ' . $username . ' to ' . strval($quotaInMb) . ': ' . $e->getMessage());
        }
    }
}
+11 −0
Original line number Diff line number Diff line
<?php

namespace OCA\EcloudAccounts\Exception;

class DbConnectionParamsException extends \Exception
{
  public function __construct($message = NULL, $code = 0)
  {
    parent::__construct($message, $code);
  }
}
Loading