Commit 4d24efe4 authored by Akhil's avatar Akhil 😶
Browse files

Update quota in mailbox when user's quota is changed

parent 9afe98fc
......@@ -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>
......@@ -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
......
......@@ -2,42 +2,51 @@
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 = [])
{
if (empty($usage)) {
return;
}
$dbTablePrefix = $this->config->getSystemValue('dbtableprefix', '');
$params = [];
$preferencesTable = $dbTablePrefix . 'preferences';
$query = 'INSERT INTO ' . $preferencesTable . ' (userid, appid, configkey, configvalue) VALUES ';
// Add values to the insert query and params array
foreach ($usage as $username => $usedSpace) {
$query .= ' (?, "ecloud-accounts", "mailQuotaUsage", ?),';
$params[] = $username;
$params[] = $usedSpace;
}
try {
if (empty($usage)) {
return;
}
$dbTablePrefix = $this->config->getSystemValue('dbtableprefix', '');
$params = [];
$preferencesTable = $dbTablePrefix . 'preferences';
$query = 'INSERT INTO ' . $preferencesTable . ' (userid, appid, configkey, configvalue) VALUES ';
// Remove the dangling comma at the end
$query = rtrim($query, ',');
// Update only configvalue in case entry already exists
$query .= ' ON DUPLICATE KEY UPDATE configvalue = VALUES(configvalue);';
$this->db->executeQuery($query, $params);
// Add values to the insert query and params array
foreach ($usage as $username => $usedSpace) {
$query .= ' (?, "ecloud-accounts", "mailQuotaUsage", ?),';
$params[] = $username;
$params[] = $usedSpace;
}
// Remove the dangling comma at the end
$query = rtrim($query, ',');
// 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());
}
}
}
<?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());
}
}
}
<?php
namespace OCA\EcloudAccounts\Exception;
class DbConnectionParamsException extends \Exception
{
public function __construct($message = NULL, $code = 0)
{
parent::__construct($message, $code);
}
}
<?php
declare(strict_types=1);
namespace OCA\EcloudAccounts\Listeners;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Util;
use OCP\User\Events\UserChangedEvent;
use OCA\EcloudAccounts\Db\MailboxMapper;
class UserChangedListener implements IEventListener
{
private const QUOTA_FEATURE = 'quota';
private $util;
private $mailboxMapper;
public function __construct(Util $util, MailboxMapper $mailboxMapper)
{
$this->util = $util;
$this->mailboxMapper = $mailboxMapper;
}
public function handle(Event $event): void
{
if (!($event instanceof UserChangedEvent)) {
return;
}
$feature = $event->getFeature();
if ($feature !== self::QUOTA_FEATURE) {
return;
}
$user = $event->getUser();
$username = $user->getUID();
$updatedQuota = $event->getValue();
$quotaInBytes = (int) $this->util->computerFileSize($updatedQuota);
$this->mailboxMapper->updateMailboxQuota($username, $quotaInBytes);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment