Skip to content

Commit a483168

Browse files
committed
Default group option. issue#107
1 parent d773528 commit a483168

6 files changed

Lines changed: 67 additions & 32 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313
- UID user table column
1414
- GID user table column
1515
- HMAC hash implementation
16+
- Default group option
1617

1718
## [4.4.1] - 2020-02-02
1819
### Fixed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ Name | Description | Details
6060
**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.
6161
**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.
6262
**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 pointed by the *Home Location* option. | Optional<br/>Default: *Default*.
63-
**Home Location** | User storage path for the `Static` *Home mode*. The `%u` variable is replaced with the username of the user. | Mandatory if the *Home mode* is set to `Static`.
63+
**Home location** | User storage path for the `Static` *Home mode*. The `%u` variable is replaced with the username of the user. | Mandatory if the *Home mode* is set to `Static`.
64+
**Default group** | Default group for all 'User SQL' users. | Optional.
6465

6566
#### User table
6667

lib/Backend/GroupBackend.php

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use OCA\UserSQL\Cache;
2525
use OCA\UserSQL\Constant\DB;
26+
use OCA\UserSQL\Constant\Opt;
2627
use OCA\UserSQL\Model\Group;
2728
use OCA\UserSQL\Properties;
2829
use OCA\UserSQL\Repository\GroupRepository;
@@ -42,6 +43,8 @@ final class GroupBackend extends ABackend implements
4243
IGroupDetailsBackend,
4344
IIsAdminBackend
4445
{
46+
const USER_SQL_GID = "user_sql";
47+
4548
/**
4649
* @var string The application name.
4750
*/
@@ -105,12 +108,30 @@ public function getGroups($search = "", $limit = null, $offset = null)
105108
return $groups;
106109
}
107110

108-
$groups = $this->groupRepository->findAllBySearchTerm(
109-
"%" . $search . "%", $limit, $offset
111+
$groups = $this->groupRepository->findAllBySearchTerm("%" . $search . "%", $limit, $offset);
112+
$groups = $this->setCacheAndMap($cacheKey, $groups);
113+
114+
$this->logger->debug(
115+
"Returning getGroups($search, $limit, $offset): count(" . count(
116+
$groups
117+
) . ")", ["app" => $this->appName]
110118
);
111119

120+
return $groups;
121+
}
122+
123+
/**
124+
* Set groups in cache and map them to GIDs.
125+
*
126+
* @param $cacheKey string Cache key.
127+
* @param $groups array Fetched groups.
128+
*
129+
* @return array Array of GIDs.
130+
*/
131+
private function setCacheAndMap($cacheKey, $groups)
132+
{
112133
if ($groups === false) {
113-
return [];
134+
return $this->defaultGroupSet() ? [self::USER_SQL_GID] : [];
114135
}
115136

116137
foreach ($groups as $group) {
@@ -122,17 +143,22 @@ function ($group) {
122143
return $group->gid;
123144
}, $groups
124145
);
146+
if ($this->defaultGroupSet()) {
147+
$groups[] = self::USER_SQL_GID;
148+
}
125149

126150
$this->cache->set($cacheKey, $groups);
127-
$this->logger->debug(
128-
"Returning getGroups($search, $limit, $offset): count(" . count(
129-
$groups
130-
) . ")", ["app" => $this->appName]
131-
);
132-
133151
return $groups;
134152
}
135153

154+
/**
155+
* @return bool Whether default group option is set.
156+
*/
157+
private function defaultGroupSet()
158+
{
159+
return !empty($this->properties[Opt::DEFAULT_GROUP]);
160+
}
161+
136162
/**
137163
* @inheritdoc
138164
*/
@@ -154,7 +180,7 @@ public function countUsersInGroup(string $gid, string $search = ""): int
154180
return $count;
155181
}
156182

157-
$count = $this->groupRepository->countAll($gid, "%" . $search . "%");
183+
$count = $this->groupRepository->countAll($this->substituteGid($gid), "%" . $search . "%");
158184

159185
if ($count === false) {
160186
return 0;
@@ -169,6 +195,18 @@ public function countUsersInGroup(string $gid, string $search = ""): int
169195
return $count;
170196
}
171197

198+
/**
199+
* Substitute GID to '%' if it's default group.
200+
*
201+
* @param $gid string Group ID.
202+
*
203+
* @return string '%' if it's default group otherwise given GID.
204+
*/
205+
private function substituteGid($gid)
206+
{
207+
return $this->defaultGroupSet() && $gid === self::USER_SQL_GID ? "%" : $gid;
208+
}
209+
172210
/**
173211
* @inheritdoc
174212
*/
@@ -222,22 +260,8 @@ public function getUserGroups($uid)
222260
}
223261

224262
$groups = $this->groupRepository->findAllByUid($uid);
263+
$groups = $this->setCacheAndMap($cacheKey, $groups);
225264

226-
if ($groups === false) {
227-
return [];
228-
}
229-
230-
foreach ($groups as $group) {
231-
$this->cache->set("group_" . $group->gid, $group);
232-
}
233-
234-
$groups = array_map(
235-
function ($group) {
236-
return $group->gid;
237-
}, $groups
238-
);
239-
240-
$this->cache->set($cacheKey, $groups);
241265
$this->logger->debug(
242266
"Returning getUserGroups($uid): count(" . count(
243267
$groups
@@ -256,6 +280,10 @@ public function groupExists($gid)
256280
"Entering groupExists($gid)", ["app" => $this->appName]
257281
);
258282

283+
if ($this->defaultGroupSet() && $gid === self::USER_SQL_GID) {
284+
return true;
285+
}
286+
259287
$group = $this->getGroup($gid);
260288

261289
if ($group === false) {
@@ -339,7 +367,7 @@ public function usersInGroup($gid, $search = "", $limit = -1, $offset = 0)
339367
}
340368

341369
$uids = $this->groupRepository->findAllUidsBySearchTerm(
342-
$gid, "%" . $search . "%", $limit, $offset
370+
$this->substituteGid($gid), "%" . $search . "%", $limit, $offset
343371
);
344372

345373
if ($uids === false) {
@@ -403,6 +431,10 @@ public function getGroupDetails(string $gid): array
403431
"Entering getGroupDetails($gid)", ["app" => $this->appName]
404432
);
405433

434+
if ($this->defaultGroupSet() && $gid === self::USER_SQL_GID) {
435+
return ["displayName" => $this->properties[Opt::DEFAULT_GROUP]];
436+
}
437+
406438
$group = $this->getGroup($gid);
407439

408440
if (!($group instanceof Group)) {

lib/Constant/Opt.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ final class Opt
3434
const CRYPTO_PARAM_0 = "opt.crypto_param_0";
3535
const CRYPTO_PARAM_1 = "opt.crypto_param_1";
3636
const CRYPTO_PARAM_2 = "opt.crypto_param_2";
37+
const DEFAULT_GROUP = "opt.default_group";
3738
const EMAIL_LOGIN = "opt.email_login";
3839
const EMAIL_SYNC = "opt.email_sync";
3940
const HOME_LOCATION = "opt.home_location";

lib/Query/QueryProvider.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,8 @@ private function loadQueries()
137137
Query::COUNT_GROUPS =>
138138
"SELECT COUNT(ug.$ugGID) " .
139139
"FROM $userGroup ug " .
140-
"WHERE ug.$ugGID = :$gidParam " .
141-
"AND ug.$ugUID " .
142-
"LIKE :$searchParam",
140+
"WHERE ug.$ugGID LIKE :$gidParam " .
141+
"AND ug.$ugUID LIKE :$searchParam",
143142

144143
Query::COUNT_USERS =>
145144
"SELECT COUNT(u.$uUID) AS count " .
@@ -155,7 +154,7 @@ private function loadQueries()
155154
Query::FIND_GROUP_USERS =>
156155
"SELECT ug.$ugUID AS uid " .
157156
"FROM $userGroup ug " .
158-
"WHERE ug.$ugGID = :$gidParam " .
157+
"WHERE ug.$ugGID LIKE :$gidParam " .
159158
"AND ug.$ugUID LIKE :$searchParam " .
160159
"ORDER BY ug.$ugUID",
161160

templates/admin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ function print_select_options(
144144
print_select_options($l, "opt-email_sync", "Email sync", ["" => "None", "initial" => "Synchronise only once", "force_nc"=>"Nextcloud always wins", "force_sql"=>"SQL always wins"], $_["opt.email_sync"]);
145145
print_select_options($l, "opt-quota_sync", "Quota sync", ["" => "None", "initial" => "Synchronise only once", "force_nc"=>"Nextcloud always wins", "force_sql"=>"SQL always wins"], $_["opt.quota_sync"]);
146146
print_select_options($l, "opt-home_mode", "Home mode", ["" => "Default", "query" => "Query", "static" => "Static"], $_["opt.home_mode"]);
147-
print_text_input($l, "opt-home_location", "Home Location", $_["opt.home_location"]); ?>
147+
print_text_input($l, "opt-home_location", "Home location", $_["opt.home_location"]);
148+
print_text_input($l, "opt-default_group", "Default group", $_["opt.default_group"]); ?>
148149
</fieldset>
149150
</div>
150151
<div class="section clear-left">

0 commit comments

Comments
 (0)