aboutsummaryrefslogtreecommitdiffstats
path: root/apps/user_ldap/lib
diff options
context:
space:
mode:
authorArthur Schiwon <blizzz@arthur-schiwon.de>2023-09-15 18:49:30 +0200
committerArthur Schiwon <blizzz@arthur-schiwon.de>2023-10-09 19:46:27 +0200
commitcb3faad5b5631a2f020872545c53a85d9d5b0692 (patch)
treef472b981ff62a61320e638ad95b28aa272ae0434 /apps/user_ldap/lib
parent5acf6c68aafc9d42e2563652a1c09f51db205031 (diff)
downloadnextcloud-server-cb3faad5b5631a2f020872545c53a85d9d5b0692.tar.gz
nextcloud-server-cb3faad5b5631a2f020872545c53a85d9d5b0692.zip
fix(ldap): store last known user groups
- for LDAP user life cycle management Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'apps/user_ldap/lib')
-rw-r--r--apps/user_ldap/lib/Connection.php4
-rw-r--r--apps/user_ldap/lib/Group_LDAP.php25
2 files changed, 27 insertions, 2 deletions
diff --git a/apps/user_ldap/lib/Connection.php b/apps/user_ldap/lib/Connection.php
index b47e51fdf70..14d3111f1d3 100644
--- a/apps/user_ldap/lib/Connection.php
+++ b/apps/user_ldap/lib/Connection.php
@@ -298,6 +298,10 @@ class Connection extends LDAPUtility {
return json_decode(base64_decode($this->cache->get($key) ?? ''), true);
}
+ public function getConfigPrefix(): string {
+ return $this->configPrefix;
+ }
+
/**
* @param string $key
* @param mixed $value
diff --git a/apps/user_ldap/lib/Group_LDAP.php b/apps/user_ldap/lib/Group_LDAP.php
index b3ff63d3b5c..9afad6ad2ff 100644
--- a/apps/user_ldap/lib/Group_LDAP.php
+++ b/apps/user_ldap/lib/Group_LDAP.php
@@ -46,12 +46,16 @@ namespace OCA\User_LDAP;
use Exception;
use OC\ServerNotAvailableException;
+use OCA\User_LDAP\User\OfflineUser;
use OCP\Cache\CappedMemoryCache;
use OCP\GroupInterface;
use OCP\Group\Backend\ABackend;
use OCP\Group\Backend\IDeleteGroupBackend;
use OCP\Group\Backend\IGetDisplayNameBackend;
+use OCP\IConfig;
+use OCP\Server;
use Psr\Log\LoggerInterface;
+use function json_decode;
class Group_LDAP extends ABackend implements GroupInterface, IGroupLDAP, IGetDisplayNameBackend, IDeleteGroupBackend {
protected bool $enabled = false;
@@ -83,7 +87,7 @@ class Group_LDAP extends ABackend implements GroupInterface, IGroupLDAP, IGetDis
$this->cachedGroupsByMember = new CappedMemoryCache();
$this->cachedNestedGroups = new CappedMemoryCache();
$this->groupPluginManager = $groupPluginManager;
- $this->logger = \OCP\Server::get(LoggerInterface::class);
+ $this->logger = Server::get(LoggerInterface::class);
$this->ldapGroupMemberAssocAttr = strtolower((string)$gAssoc);
}
@@ -664,15 +668,28 @@ class Group_LDAP extends ABackend implements GroupInterface, IGroupLDAP, IGetDis
* @throws Exception
* @throws ServerNotAvailableException
*/
- public function getUserGroups($uid) {
+ public function getUserGroups($uid): array {
if (!$this->enabled) {
return [];
}
+ $ncUid = $uid;
+
$cacheKey = 'getUserGroups' . $uid;
$userGroups = $this->access->connection->getFromCache($cacheKey);
if (!is_null($userGroups)) {
return $userGroups;
}
+
+ $user = $this->access->userManager->get($uid);
+ if ($user instanceof OfflineUser) {
+ // We load known group memberships from configuration for remnants,
+ // because LDAP server does not contain them anymore
+ /** @var IConfig $config */
+ $config = Server::get(IConfig::class);
+ $groupStr = $config->getUserValue($uid, 'user_ldap', 'cached-group-memberships-' . $this->access->connection->getConfigPrefix(), '[]');
+ return json_decode($groupStr) ?? [];
+ }
+
$userDN = $this->access->username2dn($uid);
if (!$userDN) {
$this->access->connection->writeToCache($cacheKey, []);
@@ -786,6 +803,10 @@ class Group_LDAP extends ABackend implements GroupInterface, IGroupLDAP, IGetDis
$groups = array_unique($groups, SORT_LOCALE_STRING);
$this->access->connection->writeToCache($cacheKey, $groups);
+ /** @var IConfig $config */
+ $config = Server::get(IConfig::class);
+ $groupStr = \json_encode($groups);
+ $config->setUserValue($ncUid, 'user_ldap', 'cached-group-memberships-' . $this->access->connection->getConfigPrefix(), $groupStr);
return $groups;
}