From a483168890684de0ab8a8b920497ef2219f70b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20=C5=81ojewski?= Date: Mon, 13 Apr 2020 16:07:55 +0200 Subject: Default group option. issue#107 --- CHANGELOG.md | 1 + README.md | 3 +- lib/Backend/GroupBackend.php | 84 ++++++++++++++++++++++++++++++-------------- lib/Constant/Opt.php | 1 + lib/Query/QueryProvider.php | 7 ++-- templates/admin.php | 3 +- 6 files changed, 67 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe7f8c1..e2214c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - UID user table column - GID user table column - HMAC hash implementation +- Default group option ## [4.4.1] - 2020-02-02 ### Fixed diff --git a/README.md b/README.md index 5d7b1ac..e2bb19d 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,8 @@ Name | Description | Details **Email sync** | Sync e-mail address with the Nextcloud.
- *None* - Disables this feature. This is the default option.
- *Synchronise only once* - Copy the e-mail address to the Nextcloud preferences if its not set.
- *Nextcloud always wins* - Always copy the e-mail address to the database. This updates the user table.
- *SQL always wins* - Always copy the e-mail address to the Nextcloud preferences. | Optional.
Default: *None*.
Requires: user *Email* column. **Quota sync** | Sync user quota with the Nextcloud.
- *None* - Disables this feature. This is the default option.
- *Synchronise only once* - Copy the user quota to the Nextcloud preferences if its not set.
- *Nextcloud always wins* - Always copy the user quota to the database. This updates the user table.
- *SQL always wins* - Always copy the user quota to the Nextcloud preferences. | Optional.
Default: *None*.
Requires: user *Quota* column. **Home mode** | User storage path.
- *Default* - Let the Nextcloud manage this. The default option.
- *Query* - Use location from the user table pointed by the *home* column.
- *Static* - Use static location pointed by the *Home Location* option. | Optional
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 diff --git a/lib/Backend/GroupBackend.php b/lib/Backend/GroupBackend.php index 0f1ef05..c41a40d 100644 --- a/lib/Backend/GroupBackend.php +++ b/lib/Backend/GroupBackend.php @@ -23,6 +23,7 @@ namespace OCA\UserSQL\Backend; use OCA\UserSQL\Cache; use OCA\UserSQL\Constant\DB; +use OCA\UserSQL\Constant\Opt; use OCA\UserSQL\Model\Group; use OCA\UserSQL\Properties; use OCA\UserSQL\Repository\GroupRepository; @@ -42,6 +43,8 @@ final class GroupBackend extends ABackend implements IGroupDetailsBackend, IIsAdminBackend { + const USER_SQL_GID = "user_sql"; + /** * @var string The application name. */ @@ -105,12 +108,30 @@ final class GroupBackend extends ABackend implements return $groups; } - $groups = $this->groupRepository->findAllBySearchTerm( - "%" . $search . "%", $limit, $offset + $groups = $this->groupRepository->findAllBySearchTerm("%" . $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) { - return []; + return $this->defaultGroupSet() ? [self::USER_SQL_GID] : []; } foreach ($groups as $group) { @@ -122,17 +143,22 @@ final class GroupBackend extends ABackend implements return $group->gid; }, $groups ); + if ($this->defaultGroupSet()) { + $groups[] = self::USER_SQL_GID; + } $this->cache->set($cacheKey, $groups); - $this->logger->debug( - "Returning getGroups($search, $limit, $offset): count(" . count( - $groups - ) . ")", ["app" => $this->appName] - ); - return $groups; } + /** + * @return bool Whether default group option is set. + */ + private function defaultGroupSet() + { + return !empty($this->properties[Opt::DEFAULT_GROUP]); + } + /** * @inheritdoc */ @@ -154,7 +180,7 @@ final class GroupBackend extends ABackend implements return $count; } - $count = $this->groupRepository->countAll($gid, "%" . $search . "%"); + $count = $this->groupRepository->countAll($this->substituteGid($gid), "%" . $search . "%"); if ($count === false) { return 0; @@ -169,6 +195,18 @@ final class GroupBackend extends ABackend implements 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 */ @@ -222,22 +260,8 @@ final class GroupBackend extends ABackend implements } $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( "Returning getUserGroups($uid): count(" . count( $groups @@ -256,6 +280,10 @@ final class GroupBackend extends ABackend implements "Entering groupExists($gid)", ["app" => $this->appName] ); + if ($this->defaultGroupSet() && $gid === self::USER_SQL_GID) { + return true; + } + $group = $this->getGroup($gid); if ($group === false) { @@ -339,7 +367,7 @@ final class GroupBackend extends ABackend implements } $uids = $this->groupRepository->findAllUidsBySearchTerm( - $gid, "%" . $search . "%", $limit, $offset + $this->substituteGid($gid), "%" . $search . "%", $limit, $offset ); if ($uids === false) { @@ -403,6 +431,10 @@ final class GroupBackend extends ABackend implements "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); if (!($group instanceof Group)) { diff --git a/lib/Constant/Opt.php b/lib/Constant/Opt.php index 52583fb..005122c 100644 --- a/lib/Constant/Opt.php +++ b/lib/Constant/Opt.php @@ -34,6 +34,7 @@ final class Opt const CRYPTO_PARAM_0 = "opt.crypto_param_0"; const CRYPTO_PARAM_1 = "opt.crypto_param_1"; const CRYPTO_PARAM_2 = "opt.crypto_param_2"; + const DEFAULT_GROUP = "opt.default_group"; const EMAIL_LOGIN = "opt.email_login"; const EMAIL_SYNC = "opt.email_sync"; const HOME_LOCATION = "opt.home_location"; diff --git a/lib/Query/QueryProvider.php b/lib/Query/QueryProvider.php index 28c25c6..7252cda 100644 --- a/lib/Query/QueryProvider.php +++ b/lib/Query/QueryProvider.php @@ -137,9 +137,8 @@ class QueryProvider implements \ArrayAccess Query::COUNT_GROUPS => "SELECT COUNT(ug.$ugGID) " . "FROM $userGroup ug " . - "WHERE ug.$ugGID = :$gidParam " . - "AND ug.$ugUID " . - "LIKE :$searchParam", + "WHERE ug.$ugGID LIKE :$gidParam " . + "AND ug.$ugUID LIKE :$searchParam", Query::COUNT_USERS => "SELECT COUNT(u.$uUID) AS count " . @@ -155,7 +154,7 @@ class QueryProvider implements \ArrayAccess Query::FIND_GROUP_USERS => "SELECT ug.$ugUID AS uid " . "FROM $userGroup ug " . - "WHERE ug.$ugGID = :$gidParam " . + "WHERE ug.$ugGID LIKE :$gidParam " . "AND ug.$ugUID LIKE :$searchParam " . "ORDER BY ug.$ugUID", diff --git a/templates/admin.php b/templates/admin.php index 036a639..548d805 100644 --- a/templates/admin.php +++ b/templates/admin.php @@ -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-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_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"]); ?>
-- cgit v1.2.3