Default group option. issue#107

This commit is contained in:
Marcin Łojewski
2020-04-13 16:07:55 +02:00
parent d7735280a0
commit a483168890
6 changed files with 67 additions and 32 deletions

View File

@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- UID user table column - UID user table column
- GID user table column - GID user table column
- HMAC hash implementation - HMAC hash implementation
- Default group option
## [4.4.1] - 2020-02-02 ## [4.4.1] - 2020-02-02
### Fixed ### Fixed

View File

@@ -60,7 +60,8 @@ Name | Description | Details
**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. **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.
**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. **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.
**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*. **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*.
**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`. **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`.
**Default group** | Default group for all 'User SQL' users. | Optional.
#### User table #### User table

View File

@@ -23,6 +23,7 @@ namespace OCA\UserSQL\Backend;
use OCA\UserSQL\Cache; use OCA\UserSQL\Cache;
use OCA\UserSQL\Constant\DB; use OCA\UserSQL\Constant\DB;
use OCA\UserSQL\Constant\Opt;
use OCA\UserSQL\Model\Group; use OCA\UserSQL\Model\Group;
use OCA\UserSQL\Properties; use OCA\UserSQL\Properties;
use OCA\UserSQL\Repository\GroupRepository; use OCA\UserSQL\Repository\GroupRepository;
@@ -42,6 +43,8 @@ final class GroupBackend extends ABackend implements
IGroupDetailsBackend, IGroupDetailsBackend,
IIsAdminBackend IIsAdminBackend
{ {
const USER_SQL_GID = "user_sql";
/** /**
* @var string The application name. * @var string The application name.
*/ */
@@ -105,12 +108,30 @@ final class GroupBackend extends ABackend implements
return $groups; return $groups;
} }
$groups = $this->groupRepository->findAllBySearchTerm( $groups = $this->groupRepository->findAllBySearchTerm("%" . $search . "%", $limit, $offset);
"%" . $search . "%", $limit, $offset $groups = $this->setCacheAndMap($cacheKey, $groups);
$this->logger->debug(
"Returning getGroups($search, $limit, $offset): count(" . count(
$groups
) . ")", ["app" => $this->appName]
); );
return $groups;
}
/**
* Set groups in cache and map them to GIDs.
*
* @param $cacheKey string Cache key.
* @param $groups array Fetched groups.
*
* @return array Array of GIDs.
*/
private function setCacheAndMap($cacheKey, $groups)
{
if ($groups === false) { if ($groups === false) {
return []; return $this->defaultGroupSet() ? [self::USER_SQL_GID] : [];
} }
foreach ($groups as $group) { foreach ($groups as $group) {
@@ -122,17 +143,22 @@ final class GroupBackend extends ABackend implements
return $group->gid; return $group->gid;
}, $groups }, $groups
); );
if ($this->defaultGroupSet()) {
$groups[] = self::USER_SQL_GID;
}
$this->cache->set($cacheKey, $groups); $this->cache->set($cacheKey, $groups);
$this->logger->debug(
"Returning getGroups($search, $limit, $offset): count(" . count(
$groups
) . ")", ["app" => $this->appName]
);
return $groups; return $groups;
} }
/**
* @return bool Whether default group option is set.
*/
private function defaultGroupSet()
{
return !empty($this->properties[Opt::DEFAULT_GROUP]);
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
@@ -154,7 +180,7 @@ final class GroupBackend extends ABackend implements
return $count; return $count;
} }
$count = $this->groupRepository->countAll($gid, "%" . $search . "%"); $count = $this->groupRepository->countAll($this->substituteGid($gid), "%" . $search . "%");
if ($count === false) { if ($count === false) {
return 0; return 0;
@@ -169,6 +195,18 @@ final class GroupBackend extends ABackend implements
return $count; return $count;
} }
/**
* Substitute GID to '%' if it's default group.
*
* @param $gid string Group ID.
*
* @return string '%' if it's default group otherwise given GID.
*/
private function substituteGid($gid)
{
return $this->defaultGroupSet() && $gid === self::USER_SQL_GID ? "%" : $gid;
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
@@ -222,22 +260,8 @@ final class GroupBackend extends ABackend implements
} }
$groups = $this->groupRepository->findAllByUid($uid); $groups = $this->groupRepository->findAllByUid($uid);
$groups = $this->setCacheAndMap($cacheKey, $groups);
if ($groups === false) {
return [];
}
foreach ($groups as $group) {
$this->cache->set("group_" . $group->gid, $group);
}
$groups = array_map(
function ($group) {
return $group->gid;
}, $groups
);
$this->cache->set($cacheKey, $groups);
$this->logger->debug( $this->logger->debug(
"Returning getUserGroups($uid): count(" . count( "Returning getUserGroups($uid): count(" . count(
$groups $groups
@@ -256,6 +280,10 @@ final class GroupBackend extends ABackend implements
"Entering groupExists($gid)", ["app" => $this->appName] "Entering groupExists($gid)", ["app" => $this->appName]
); );
if ($this->defaultGroupSet() && $gid === self::USER_SQL_GID) {
return true;
}
$group = $this->getGroup($gid); $group = $this->getGroup($gid);
if ($group === false) { if ($group === false) {
@@ -339,7 +367,7 @@ final class GroupBackend extends ABackend implements
} }
$uids = $this->groupRepository->findAllUidsBySearchTerm( $uids = $this->groupRepository->findAllUidsBySearchTerm(
$gid, "%" . $search . "%", $limit, $offset $this->substituteGid($gid), "%" . $search . "%", $limit, $offset
); );
if ($uids === false) { if ($uids === false) {
@@ -403,6 +431,10 @@ final class GroupBackend extends ABackend implements
"Entering getGroupDetails($gid)", ["app" => $this->appName] "Entering getGroupDetails($gid)", ["app" => $this->appName]
); );
if ($this->defaultGroupSet() && $gid === self::USER_SQL_GID) {
return ["displayName" => $this->properties[Opt::DEFAULT_GROUP]];
}
$group = $this->getGroup($gid); $group = $this->getGroup($gid);
if (!($group instanceof Group)) { if (!($group instanceof Group)) {

View File

@@ -34,6 +34,7 @@ final class Opt
const CRYPTO_PARAM_0 = "opt.crypto_param_0"; const CRYPTO_PARAM_0 = "opt.crypto_param_0";
const CRYPTO_PARAM_1 = "opt.crypto_param_1"; const CRYPTO_PARAM_1 = "opt.crypto_param_1";
const CRYPTO_PARAM_2 = "opt.crypto_param_2"; const CRYPTO_PARAM_2 = "opt.crypto_param_2";
const DEFAULT_GROUP = "opt.default_group";
const EMAIL_LOGIN = "opt.email_login"; const EMAIL_LOGIN = "opt.email_login";
const EMAIL_SYNC = "opt.email_sync"; const EMAIL_SYNC = "opt.email_sync";
const HOME_LOCATION = "opt.home_location"; const HOME_LOCATION = "opt.home_location";

View File

@@ -137,9 +137,8 @@ class QueryProvider implements \ArrayAccess
Query::COUNT_GROUPS => Query::COUNT_GROUPS =>
"SELECT COUNT(ug.$ugGID) " . "SELECT COUNT(ug.$ugGID) " .
"FROM $userGroup ug " . "FROM $userGroup ug " .
"WHERE ug.$ugGID = :$gidParam " . "WHERE ug.$ugGID LIKE :$gidParam " .
"AND ug.$ugUID " . "AND ug.$ugUID LIKE :$searchParam",
"LIKE :$searchParam",
Query::COUNT_USERS => Query::COUNT_USERS =>
"SELECT COUNT(u.$uUID) AS count " . "SELECT COUNT(u.$uUID) AS count " .
@@ -155,7 +154,7 @@ class QueryProvider implements \ArrayAccess
Query::FIND_GROUP_USERS => Query::FIND_GROUP_USERS =>
"SELECT ug.$ugUID AS uid " . "SELECT ug.$ugUID AS uid " .
"FROM $userGroup ug " . "FROM $userGroup ug " .
"WHERE ug.$ugGID = :$gidParam " . "WHERE ug.$ugGID LIKE :$gidParam " .
"AND ug.$ugUID LIKE :$searchParam " . "AND ug.$ugUID LIKE :$searchParam " .
"ORDER BY ug.$ugUID", "ORDER BY ug.$ugUID",

View File

@@ -144,7 +144,8 @@ function print_select_options(
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"]); 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"]);
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"]); 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"]);
print_select_options($l, "opt-home_mode", "Home mode", ["" => "Default", "query" => "Query", "static" => "Static"], $_["opt.home_mode"]); print_select_options($l, "opt-home_mode", "Home mode", ["" => "Default", "query" => "Query", "static" => "Static"], $_["opt.home_mode"]);
print_text_input($l, "opt-home_location", "Home Location", $_["opt.home_location"]); ?> print_text_input($l, "opt-home_location", "Home location", $_["opt.home_location"]);
print_text_input($l, "opt-default_group", "Default group", $_["opt.default_group"]); ?>
</fieldset> </fieldset>
</div> </div>
<div class="section clear-left"> <div class="section clear-left">