Skip to content

Commit ed37c70

Browse files
committed
User quota from SQL
1 parent 49a9b2e commit ed37c70

14 files changed

Lines changed: 178 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010
- WoltLab Community Framework 2.x hash algorithm
1111
- phpass hash implementation
1212
- Support for salt column
13+
- User quota synchronization
1314

1415
### Changed
1516
- Example SQL script in README file
17+
- Fixed misspelling
1618

1719
### Fixed
1820
- Table and column autocomplete in settings panel

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ Name | Description | Details
5151
**Allow password change** | Can user change its password. The password change is propagated to the database. See [Hash algorithms](#hash-algorithms). | Optional.<br/>Default: false.
5252
**Use cache** | Use database query results cache. The cache can be cleared any time with the *Clear cache* button click. | Optional.<br/>Default: false.
5353
**Hash algorithm** | How users passwords are stored in the database. See [Hash algorithms](#hash-algorithms). | Mandatory.
54-
**Email sync** | Sync e-mail address with the Nextcloud.<br/>- *None* - Disables this feature. This is the default option.<br/>- *Synchronise only once* - Copy the e-mail address to the Nextcloud storage if its not set.<br/>- *Nextcloud always wins* - Always copy the e-mail address to the database. This updates the user table.<br/>- *SQL always wins* - Always copy the e-mail address to the Nextcloud storage. | Optional.<br/>Default: *None*.<br/>Requires: user *Email* column.
54+
**Email sync** | Sync e-mail address with the Nextcloud.<br/>- *None* - Disables this feature. This is the default option.<br/>- *Synchronise only once* - Copy the e-mail address to the Nextcloud preferences if its not set.<br/>- *Nextcloud always wins* - Always copy the e-mail address to the database. This updates the user table.<br/>- *SQL always wins* - Always copy the e-mail address to the Nextcloud preferences. | Optional.<br/>Default: *None*.<br/>Requires: user *Email* column.
55+
**Quota sync** | Sync user quota with the Nextcloud.<br/>- *None* - Disables this feature. This is the default option.<br/>- *Synchronise only once* - Copy the user quota to the Nextcloud preferences if its not set.<br/>- *Nextcloud always wins* - Always copy the user quota to the database. This updates the user table.<br/>- *SQL always wins* - Always copy the user quota to the Nextcloud preferences. | Optional.<br/>Default: *None*.<br/>Requires: user *Quota* column.
5556
**Home mode** | User storage path.<br/>- *Default* - Let the Nextcloud manage this. The default option.<br/>- *Query* - Use location from the user table pointed by the *home* column.<br/>- *Static* - Use static location. The `%u` variable is replaced with the username of the user. | Optional<br/>Default: *Default*.
5657
**Home Location** | User storage path for the `static` *home mode*. | Mandatory if the *Home mode* is set to `Static`.
5758

@@ -64,6 +65,7 @@ Name | Description | Details
6465
**Table name** | The table name. | Mandatory for user backend.
6566
**Username** | Username column. | Mandatory for user backend.
6667
**Email** | E-mail column. | Mandatory for *Email sync* option.
68+
**Quota** | Quota column. | Mandatory for *Quota sync* option.
6769
**Home** | Home path column. | Mandatory for `Query` *Home sync* option.
6870
**Password** | Password hash column. | Mandatory for user backend.
6971
**Display name** | Display name column. | Optional.
@@ -111,6 +113,7 @@ CREATE TABLE sql_user
111113
username VARCHAR(16) PRIMARY KEY,
112114
display_name TEXT NULL,
113115
email TEXT NULL,
116+
quota TEXT NULL,
114117
home TEXT NULL,
115118
password TEXT NOT NULL,
116119
active TINYINT(1) NOT NULL DEFAULT '1',

js/settings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ user_sql.adminSettingsUI = function () {
7676
);
7777

7878
autocomplete(
79-
"#db-table-user-column-uid, #db-table-user-column-email, #db-table-user-column-home, #db-table-user-column-password, #db-table-user-column-name, #db-table-user-column-active, #db-table-user-column-avatar, #db-table-user-column-salt",
79+
"#db-table-user-column-uid, #db-table-user-column-email, #db-table-user-column-quota, #db-table-user-column-home, #db-table-user-column-password, #db-table-user-column-name, #db-table-user-column-active, #db-table-user-column-avatar, #db-table-user-column-salt",
8080
"/apps/user_sql/settings/autocomplete/table/user"
8181
);
8282

lib/Action/EmailSync.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function doAction(User $user)
9494
$result = false;
9595

9696
switch ($this->properties[Opt::EMAIL_SYNC]) {
97-
case App::EMAIL_INITIAL:
97+
case App::SYNC_INITIAL:
9898
if (empty($ncMail) && !empty($user->email)) {
9999
$this->config->setUserValue(
100100
$user->uid, "settings", "email", $user->email
@@ -103,7 +103,7 @@ public function doAction(User $user)
103103

104104
$result = true;
105105
break;
106-
case App::EMAIL_FORCE_NC:
106+
case App::SYNC_FORCE_NC:
107107
if (!empty($ncMail) && $user->email !== $ncMail) {
108108
$user = $this->userRepository->findByUid($user->uid);
109109
if (!($user instanceof User)) {
@@ -115,7 +115,7 @@ public function doAction(User $user)
115115
}
116116

117117
break;
118-
case App::EMAIL_FORCE_SQL:
118+
case App::SYNC_FORCE_SQL:
119119
if (!empty($user->email) && $user->email !== $ncMail) {
120120
$this->config->setUserValue(
121121
$user->uid, "settings", "email", $user->email

lib/Action/QuotaSync.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* Nextcloud - user_sql
4+
*
5+
* @copyright 2018 Marcin Łojewski <dev@mlojewski.me>
6+
* @author Marcin Łojewski <dev@mlojewski.me>
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Affero General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
namespace OCA\UserSQL\Action;
23+
24+
use OCA\UserSQL\Constant\App;
25+
use OCA\UserSQL\Constant\Opt;
26+
use OCA\UserSQL\Model\User;
27+
use OCA\UserSQL\Properties;
28+
use OCA\UserSQL\Repository\UserRepository;
29+
use OCP\IConfig;
30+
use OCP\ILogger;
31+
32+
/**
33+
* Synchronizes the user quota.
34+
*
35+
* @author Marcin Łojewski <dev@mlojewski.me>
36+
*/
37+
class QuotaSync implements IUserAction
38+
{
39+
/**
40+
* @var string The application name.
41+
*/
42+
private $appName;
43+
/**
44+
* @var ILogger The logger instance.
45+
*/
46+
private $logger;
47+
/**
48+
* @var Properties The properties array.
49+
*/
50+
private $properties;
51+
/**
52+
* @var IConfig The config instance.
53+
*/
54+
private $config;
55+
/**
56+
* @var UserRepository The user repository.
57+
*/
58+
private $userRepository;
59+
60+
/**
61+
* The default constructor.
62+
*
63+
* @param string $appName The application name.
64+
* @param ILogger $logger The logger instance.
65+
* @param Properties $properties The properties array.
66+
* @param IConfig $config The config instance.
67+
* @param UserRepository $userRepository The user repository.
68+
*/
69+
public function __construct(
70+
$appName, ILogger $logger, Properties $properties, IConfig $config,
71+
UserRepository $userRepository
72+
) {
73+
$this->appName = $appName;
74+
$this->logger = $logger;
75+
$this->properties = $properties;
76+
$this->config = $config;
77+
$this->userRepository = $userRepository;
78+
}
79+
80+
/**
81+
* @inheritdoc
82+
* @throws \OCP\PreConditionNotMetException
83+
*/
84+
public function doAction(User $user)
85+
{
86+
$this->logger->debug(
87+
"Entering QuotaSync#doAction($user->uid)", ["app" => $this->appName]
88+
);
89+
90+
$ncQuota = $this->config->getUserValue(
91+
$user->uid, "files", "quota", ""
92+
);
93+
94+
$result = false;
95+
96+
switch ($this->properties[Opt::QUOTA_SYNC]) {
97+
case App::SYNC_INITIAL:
98+
if (empty($ncQuota) && !empty($user->quota)) {
99+
$this->config->setUserValue(
100+
$user->uid, "files", "quota", $user->quota
101+
);
102+
}
103+
104+
$result = true;
105+
break;
106+
case App::SYNC_FORCE_NC:
107+
if (!empty($ncQuota) && $user->quota !== $ncQuota) {
108+
$user = $this->userRepository->findByUid($user->uid);
109+
if (!($user instanceof User)) {
110+
break;
111+
}
112+
113+
$user->quota = $ncQuota;
114+
$result = $this->userRepository->save($user);
115+
}
116+
117+
break;
118+
case App::SYNC_FORCE_SQL:
119+
if (!empty($user->quota) && $user->quota !== $ncQuota) {
120+
$this->config->setUserValue(
121+
$user->uid, "files", "quota", $user->quota
122+
);
123+
}
124+
125+
$result = true;
126+
break;
127+
}
128+
129+
$this->logger->debug(
130+
"Returning QuotaSync#doAction($user->uid): " . ($result ? "true"
131+
: "false"),
132+
["app" => $this->appName]
133+
);
134+
135+
return $result;
136+
}
137+
}

lib/Backend/UserBackend.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use OC\User\Backend;
2525
use OCA\UserSQL\Action\EmailSync;
2626
use OCA\UserSQL\Action\IUserAction;
27+
use OCA\UserSQL\Action\QuotaSync;
2728
use OCA\UserSQL\Cache;
2829
use OCA\UserSQL\Constant\App;
2930
use OCA\UserSQL\Constant\DB;
@@ -116,6 +117,14 @@ private function initActions()
116117
$this->userRepository
117118
);
118119
}
120+
if (!empty($this->properties[Opt::QUOTA_SYNC])
121+
&& !empty($this->properties[DB::USER_QUOTA_COLUMN])
122+
) {
123+
$this->actions[] = new QuotaSync(
124+
$this->appName, $this->logger, $this->properties, $this->config,
125+
$this->userRepository
126+
);
127+
}
119128
}
120129

121130
/**

lib/Constant/App.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class App
3434
const HOME_QUERY = "query";
3535
const HOME_STATIC = "static";
3636

37-
const EMAIL_FORCE_NC = "force_nc";
38-
const EMAIL_FORCE_SQL = "force_sql";
39-
const EMAIL_INITIAL = "initial";
37+
const SYNC_FORCE_NC = "force_nc";
38+
const SYNC_FORCE_SQL = "force_sql";
39+
const SYNC_INITIAL = "initial";
4040
}

lib/Constant/DB.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ final class DB
5151
const USER_HOME_COLUMN = "db.table.user.column.home";
5252
const USER_NAME_COLUMN = "db.table.user.column.name";
5353
const USER_PASSWORD_COLUMN = "db.table.user.column.password";
54+
const USER_QUOTA_COLUMN = "db.table.user.column.quota";
5455
const USER_SALT_COLUMN = "db.table.user.column.salt";
5556
const USER_UID_COLUMN = "db.table.user.column.uid";
5657
}

lib/Constant/Opt.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@ final class Opt
3434
const HOME_MODE = "opt.home_mode";
3535
const NAME_CHANGE = "opt.name_change";
3636
const PASSWORD_CHANGE = "opt.password_change";
37+
const QUOTA_SYNC = "opt.quota_sync";
3738
const USE_CACHE = "opt.use_cache";
3839
}

lib/Constant/Query.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ final class Query
3939
const FIND_USERS = "find_users";
4040
const SAVE_USER = "save_user";
4141

42+
const EMAIL_PARAM = "email";
4243
const GID_PARAM = "gid";
4344
const NAME_PARAM = "name";
4445
const PASSWORD_PARAM = "password";
46+
const QUOTA_PARAM = "quota";
4547
const SEARCH_PARAM = "search";
4648
const UID_PARAM = "uid";
4749
}

0 commit comments

Comments
 (0)