Merge branch 'develop' into develop-14

This commit is contained in:
Marcin Łojewski
2018-07-09 20:19:50 +02:00
committed by GitHub
58 changed files with 504 additions and 75 deletions

View File

@@ -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
View 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;
}
}

View File

@@ -23,6 +23,7 @@ namespace OCA\UserSQL\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;
@@ -130,6 +131,14 @@ final class UserBackend extends ABackend implements
$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
);
}
}
/**

View File

@@ -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";
}

View File

@@ -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";
}

View File

@@ -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";
}

View File

@@ -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";
}

View File

@@ -22,8 +22,8 @@
namespace OCA\UserSQL\Crypto;
/**
* Abstract Unix Crypt hashing implementation.
* The hashing algorithm depends on the chosen salt.
* Abstract Unix Crypt hash implementation.
* The hash algorithm depends on the chosen salt.
*
* @see crypt()
* @author Marcin Łojewski <dev@mlojewski.me>
@@ -52,12 +52,9 @@ abstract class AbstractCrypt extends AbstractAlgorithm
}
/**
* Generate a salt string for the hashing algorithm.
* Generate a salt string for the hash algorithm.
*
* @return string The salt string.
*/
protected function getSalt()
{
return "";
}
protected abstract function getSalt();
}

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* Courier MD5 hashing implementation.
* Courier MD5 hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* Courier MD5 RAW hashing implementation.
* Courier MD5 RAW hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* Courier SHA1 hashing implementation.
* Courier SHA1 hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* Courier SHA256 hashing implementation.
* Courier SHA256 hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* Unix Crypt hashing implementation.
* Unix Crypt hash implementation.
*
* @see crypt()
* @author Marcin Łojewski <dev@mlojewski.me>
@@ -56,4 +56,12 @@ class Crypt extends AbstractCrypt
{
return "Unix (Crypt)";
}
/**
* Not used.
*/
protected function getSalt()
{
return null;
}
}

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* Argon2 Crypt hashing implementation.
* Argon2 Crypt hash implementation.
*
* @see crypt()
* @author Marcin Łojewski <dev@mlojewski.me>

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* Blowfish Crypt hashing implementation.
* Blowfish Crypt hash implementation.
*
* @see crypt()
* @author Marcin Łojewski <dev@mlojewski.me>

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* Extended DES Crypt hashing implementation.
* Extended DES Crypt hash implementation.
*
* @see crypt()
* @author Marcin Łojewski <dev@mlojewski.me>

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* MD5 Crypt hashing implementation.
* MD5 Crypt hash implementation.
*
* @see crypt()
* @author Marcin Łojewski <dev@mlojewski.me>

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* SHA256 Crypt hashing implementation.
* SHA256 Crypt hash implementation.
*
* @see crypt()
* @author Marcin Łojewski <dev@mlojewski.me>

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* SHA512 Crypt hashing implementation.
* SHA512 Crypt hash implementation.
*
* @see crypt()
* @author Marcin Łojewski <dev@mlojewski.me>

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* Standard DES Crypt hashing implementation.
* Standard DES Crypt hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* Joomla hashing implementation.
* Joomla hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* MD5 hashing implementation.
* MD5 hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* phpass hashing implementation.
* phpass hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* SHA1 hashing implementation.
* SHA1 hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* SHA512 Whirlpool hashing implementation.
* SHA512 Whirlpool hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* SSHA* hashing implementation.
* SSHA* hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* SSHA256 hashing implementation.
* SSHA256 hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

View File

@@ -24,7 +24,7 @@ namespace OCA\UserSQL\Crypto;
use OCP\IL10N;
/**
* SSHA512 hashing implementation.
* SSHA512 hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/

63
lib/Crypto/WCF2.php Normal file
View File

@@ -0,0 +1,63 @@
<?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\Crypto;
/**
* WCF2 hash implementation.
*
* @author Marcin Łojewski <dev@mlojewski.me>
*/
class WCF2 extends AbstractCrypt
{
/**
* @inheritdoc
*/
public function checkPassword($password, $dbHash)
{
return hash_equals($dbHash, crypt(crypt($password, $dbHash), $dbHash));
}
/**
* @inheritdoc
*/
public function getPasswordHash($password)
{
$salt = $this->getSalt();
return crypt(crypt($password, $salt), $salt);
}
/**
* @inheritdoc
*/
protected function getSalt()
{
return "$2a$08$" . Utils::randomString(22, self::SALT_ALPHABET) . "$";
}
/**
* @inheritdoc
*/
protected function getAlgorithmName()
{
return "WoltLab Community Framework 2.x";
}
}

View File

@@ -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.
*/

View File

@@ -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",
];
}

View File

@@ -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
]
);