User quota from SQL
This commit is contained in:
@@ -94,7 +94,7 @@ class EmailSync implements IUserAction
|
||||
$result = false;
|
||||
|
||||
switch ($this->properties[Opt::EMAIL_SYNC]) {
|
||||
case App::EMAIL_INITIAL:
|
||||
case App::SYNC_INITIAL:
|
||||
if (empty($ncMail) && !empty($user->email)) {
|
||||
$this->config->setUserValue(
|
||||
$user->uid, "settings", "email", $user->email
|
||||
@@ -103,7 +103,7 @@ class EmailSync implements IUserAction
|
||||
|
||||
$result = true;
|
||||
break;
|
||||
case App::EMAIL_FORCE_NC:
|
||||
case App::SYNC_FORCE_NC:
|
||||
if (!empty($ncMail) && $user->email !== $ncMail) {
|
||||
$user = $this->userRepository->findByUid($user->uid);
|
||||
if (!($user instanceof User)) {
|
||||
@@ -115,7 +115,7 @@ class EmailSync implements IUserAction
|
||||
}
|
||||
|
||||
break;
|
||||
case App::EMAIL_FORCE_SQL:
|
||||
case App::SYNC_FORCE_SQL:
|
||||
if (!empty($user->email) && $user->email !== $ncMail) {
|
||||
$this->config->setUserValue(
|
||||
$user->uid, "settings", "email", $user->email
|
||||
|
||||
137
lib/Action/QuotaSync.php
Normal file
137
lib/Action/QuotaSync.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
/**
|
||||
* Nextcloud - user_sql
|
||||
*
|
||||
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace OCA\UserSQL\Action;
|
||||
|
||||
use OCA\UserSQL\Constant\App;
|
||||
use OCA\UserSQL\Constant\Opt;
|
||||
use OCA\UserSQL\Model\User;
|
||||
use OCA\UserSQL\Properties;
|
||||
use OCA\UserSQL\Repository\UserRepository;
|
||||
use OCP\IConfig;
|
||||
use OCP\ILogger;
|
||||
|
||||
/**
|
||||
* Synchronizes the user quota.
|
||||
*
|
||||
* @author Marcin Łojewski <dev@mlojewski.me>
|
||||
*/
|
||||
class QuotaSync implements IUserAction
|
||||
{
|
||||
/**
|
||||
* @var string The application name.
|
||||
*/
|
||||
private $appName;
|
||||
/**
|
||||
* @var ILogger The logger instance.
|
||||
*/
|
||||
private $logger;
|
||||
/**
|
||||
* @var Properties The properties array.
|
||||
*/
|
||||
private $properties;
|
||||
/**
|
||||
* @var IConfig The config instance.
|
||||
*/
|
||||
private $config;
|
||||
/**
|
||||
* @var UserRepository The user repository.
|
||||
*/
|
||||
private $userRepository;
|
||||
|
||||
/**
|
||||
* The default constructor.
|
||||
*
|
||||
* @param string $appName The application name.
|
||||
* @param ILogger $logger The logger instance.
|
||||
* @param Properties $properties The properties array.
|
||||
* @param IConfig $config The config instance.
|
||||
* @param UserRepository $userRepository The user repository.
|
||||
*/
|
||||
public function __construct(
|
||||
$appName, ILogger $logger, Properties $properties, IConfig $config,
|
||||
UserRepository $userRepository
|
||||
) {
|
||||
$this->appName = $appName;
|
||||
$this->logger = $logger;
|
||||
$this->properties = $properties;
|
||||
$this->config = $config;
|
||||
$this->userRepository = $userRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
* @throws \OCP\PreConditionNotMetException
|
||||
*/
|
||||
public function doAction(User $user)
|
||||
{
|
||||
$this->logger->debug(
|
||||
"Entering QuotaSync#doAction($user->uid)", ["app" => $this->appName]
|
||||
);
|
||||
|
||||
$ncQuota = $this->config->getUserValue(
|
||||
$user->uid, "files", "quota", ""
|
||||
);
|
||||
|
||||
$result = false;
|
||||
|
||||
switch ($this->properties[Opt::QUOTA_SYNC]) {
|
||||
case App::SYNC_INITIAL:
|
||||
if (empty($ncQuota) && !empty($user->quota)) {
|
||||
$this->config->setUserValue(
|
||||
$user->uid, "files", "quota", $user->quota
|
||||
);
|
||||
}
|
||||
|
||||
$result = true;
|
||||
break;
|
||||
case App::SYNC_FORCE_NC:
|
||||
if (!empty($ncQuota) && $user->quota !== $ncQuota) {
|
||||
$user = $this->userRepository->findByUid($user->uid);
|
||||
if (!($user instanceof User)) {
|
||||
break;
|
||||
}
|
||||
|
||||
$user->quota = $ncQuota;
|
||||
$result = $this->userRepository->save($user);
|
||||
}
|
||||
|
||||
break;
|
||||
case App::SYNC_FORCE_SQL:
|
||||
if (!empty($user->quota) && $user->quota !== $ncQuota) {
|
||||
$this->config->setUserValue(
|
||||
$user->uid, "files", "quota", $user->quota
|
||||
);
|
||||
}
|
||||
|
||||
$result = true;
|
||||
break;
|
||||
}
|
||||
|
||||
$this->logger->debug(
|
||||
"Returning QuotaSync#doAction($user->uid): " . ($result ? "true"
|
||||
: "false"),
|
||||
["app" => $this->appName]
|
||||
);
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ namespace OCA\UserSQL\Backend;
|
||||
use OC\User\Backend;
|
||||
use OCA\UserSQL\Action\EmailSync;
|
||||
use OCA\UserSQL\Action\IUserAction;
|
||||
use OCA\UserSQL\Action\QuotaSync;
|
||||
use OCA\UserSQL\Cache;
|
||||
use OCA\UserSQL\Constant\App;
|
||||
use OCA\UserSQL\Constant\DB;
|
||||
@@ -116,6 +117,14 @@ final class UserBackend extends Backend
|
||||
$this->userRepository
|
||||
);
|
||||
}
|
||||
if (!empty($this->properties[Opt::QUOTA_SYNC])
|
||||
&& !empty($this->properties[DB::USER_QUOTA_COLUMN])
|
||||
) {
|
||||
$this->actions[] = new QuotaSync(
|
||||
$this->appName, $this->logger, $this->properties, $this->config,
|
||||
$this->userRepository
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -34,7 +34,7 @@ final class App
|
||||
const HOME_QUERY = "query";
|
||||
const HOME_STATIC = "static";
|
||||
|
||||
const EMAIL_FORCE_NC = "force_nc";
|
||||
const EMAIL_FORCE_SQL = "force_sql";
|
||||
const EMAIL_INITIAL = "initial";
|
||||
const SYNC_FORCE_NC = "force_nc";
|
||||
const SYNC_FORCE_SQL = "force_sql";
|
||||
const SYNC_INITIAL = "initial";
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ final class DB
|
||||
const USER_HOME_COLUMN = "db.table.user.column.home";
|
||||
const USER_NAME_COLUMN = "db.table.user.column.name";
|
||||
const USER_PASSWORD_COLUMN = "db.table.user.column.password";
|
||||
const USER_QUOTA_COLUMN = "db.table.user.column.quota";
|
||||
const USER_SALT_COLUMN = "db.table.user.column.salt";
|
||||
const USER_UID_COLUMN = "db.table.user.column.uid";
|
||||
}
|
||||
|
||||
@@ -34,5 +34,6 @@ final class Opt
|
||||
const HOME_MODE = "opt.home_mode";
|
||||
const NAME_CHANGE = "opt.name_change";
|
||||
const PASSWORD_CHANGE = "opt.password_change";
|
||||
const QUOTA_SYNC = "opt.quota_sync";
|
||||
const USE_CACHE = "opt.use_cache";
|
||||
}
|
||||
|
||||
@@ -39,9 +39,11 @@ final class Query
|
||||
const FIND_USERS = "find_users";
|
||||
const SAVE_USER = "save_user";
|
||||
|
||||
const EMAIL_PARAM = "email";
|
||||
const GID_PARAM = "gid";
|
||||
const NAME_PARAM = "name";
|
||||
const PASSWORD_PARAM = "password";
|
||||
const QUOTA_PARAM = "quota";
|
||||
const SEARCH_PARAM = "search";
|
||||
const UID_PARAM = "uid";
|
||||
}
|
||||
|
||||
@@ -36,6 +36,10 @@ class User
|
||||
* @var string The user's email address.
|
||||
*/
|
||||
public $email;
|
||||
/**
|
||||
* @var string The user quota.
|
||||
*/
|
||||
public $quota;
|
||||
/**
|
||||
* @var string The user's display name.
|
||||
*/
|
||||
|
||||
@@ -71,15 +71,18 @@ class QueryProvider implements \ArrayAccess
|
||||
$uHome = $this->properties[DB::USER_HOME_COLUMN];
|
||||
$uName = $this->properties[DB::USER_NAME_COLUMN];
|
||||
$uPassword = $this->properties[DB::USER_PASSWORD_COLUMN];
|
||||
$uQuota = $this->properties[DB::USER_QUOTA_COLUMN];
|
||||
$uSalt = $this->properties[DB::USER_SALT_COLUMN];
|
||||
$uUID = $this->properties[DB::USER_UID_COLUMN];
|
||||
|
||||
$ugGID = $this->properties[DB::USER_GROUP_GID_COLUMN];
|
||||
$ugUID = $this->properties[DB::USER_GROUP_UID_COLUMN];
|
||||
|
||||
$emailParam = Query::EMAIL_PARAM;
|
||||
$gidParam = Query::GID_PARAM;
|
||||
$nameParam = Query::NAME_PARAM;
|
||||
$passwordParam = Query::PASSWORD_PARAM;
|
||||
$quotaParam = Query::QUOTA_PARAM;
|
||||
$searchParam = Query::SEARCH_PARAM;
|
||||
$uidParam = Query::UID_PARAM;
|
||||
|
||||
@@ -91,6 +94,7 @@ class QueryProvider implements \ArrayAccess
|
||||
= "$uUID AS uid, " .
|
||||
(empty($uName) ? "null" : $uName) . " AS name, " .
|
||||
(empty($uEmail) ? "null" : $uEmail) . " AS email, " .
|
||||
(empty($uQuota) ? "null" : $uQuota) . " AS quota, " .
|
||||
(empty($uHome) ? "null" : $uHome) . " AS home, " .
|
||||
(empty($uActive) ? "true" : $uActive) . " AS active, " .
|
||||
(empty($uAvatar) ? "false" : $uAvatar) . " AS avatar, " .
|
||||
@@ -156,7 +160,9 @@ class QueryProvider implements \ArrayAccess
|
||||
Query::SAVE_USER =>
|
||||
"UPDATE $user " .
|
||||
"SET $uPassword = :$passwordParam, " .
|
||||
"$uName = :$nameParam " .
|
||||
"$uName = :$nameParam, " .
|
||||
"$uEmail = :$emailParam, " .
|
||||
"$uQuota = :$quotaParam " .
|
||||
"WHERE $uUID = :$uidParam",
|
||||
];
|
||||
}
|
||||
|
||||
@@ -107,6 +107,8 @@ class UserRepository
|
||||
Query::SAVE_USER, [
|
||||
Query::NAME_PARAM => $user->name,
|
||||
Query::PASSWORD_PARAM => $user->password,
|
||||
Query::EMAIL_PARAM => $user->email,
|
||||
Query::QUOTA_PARAM => $user->quota,
|
||||
Query::UID_PARAM => $user->uid
|
||||
]
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user