Commit 67359a51 authored by Akhil's avatar Akhil 😶
Browse files

Added mail_quota_usage route

parent c230fc5a
......@@ -3,6 +3,7 @@
return ['routes' => [
['name' => 'user#set_account_data', 'url' => '/api/set_account_data', 'verb' => 'POST'],
['name' => 'user#user_exists', 'url' => '/api/user_exists', 'verb' => 'POST'],
['name' => 'user#set_mail_quota_usage', 'url' => '/api/set_mail_quota_usage', 'verb' => 'POST'],
[
'name' => 'user#preflighted_cors', 'url' => '/api/{path}',
'verb' => 'OPTIONS', 'requirements' => array('path' => '.+')
......
......@@ -4,10 +4,13 @@ declare(strict_types=1);
namespace OCA\EcloudAccounts\Controller;
use Exception;
use OCP\IRequest;
use OCP\ILogger;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http\DataResponse;
use OCA\EcloudAccounts\Service\UserService;
use OCA\EcloudAccounts\Db\MailUsageMapper;
class UserController extends ApiController
{
......@@ -15,10 +18,16 @@ class UserController extends ApiController
/** @var UserService */
private $userService;
public function __construct($appName, IRequest $request, UserService $userService)
private $mailUsageMapper;
private $logger;
public function __construct($appName, IRequest $request, ILogger $logger, UserService $userService, MailUsageMapper $mailUsageMapper)
{
parent::__construct($appName, $request);
$this->userService = $userService;
$this->mailUsageMapper = $mailUsageMapper;
$this->logger = $logger;
}
/**
......@@ -84,6 +93,35 @@ class UserController extends ApiController
return $response;
}
/**
* @CORS
* @PublicPage
* @NoCSRFRequired
*/
public function setMailQuotaUsage(array $usage, string $token): DataResponse
{
$response = new DataResponse();
if (!$this->checkAppCredentials($token)) {
$response->setStatus(401);
return $response;
}
try {
$this->updateMailQuotaUsageInPreferences($usage);
} catch (Exception $e) {
$statusCode = 500;
$errorMessage = 'error_setting_mail_quota_usage';
$response = $this->getErrorResponse($response, $errorMessage, $statusCode);
$this->logger->error($errorMessage . ': ' . $e->getMessage());
}
return $response;
}
private function updateMailQuotaUsageInPreferences(array $usage)
{
$this->mailUsageMapper->updateUsageInPreferences($usage);
}
private function getErrorResponse(DataResponse $response, string $error, int $code)
{
$response->setStatus($code);
......
<?php
namespace OCA\EcloudAccounts\Db;
use OCP\IDBConnection;
use OCP\IConfig;
class MailUsageMapper
{
private $db;
private $config;
public function __construct(IDBConnection $db, IConfig $config)
{
$this->db = $db;
$this->config = $config;
}
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;
}
// 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);
}
}
......@@ -104,4 +104,5 @@ class UserService
return false;
}
}
}
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