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

@@ -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)) {

View File

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

View File

@@ -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",