Skip to content

Commit

Permalink
fix(ldap): store last known user groups
Browse files Browse the repository at this point in the history
- for LDAP user life cycle management

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
  • Loading branch information
blizzz committed Oct 9, 2023
1 parent 5acf6c6 commit cb3faad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 4 additions & 0 deletions apps/user_ldap/lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ public function getFromCache($key) {
return json_decode(base64_decode($this->cache->get($key) ?? ''), true);
}

public function getConfigPrefix(): string {
return $this->configPrefix;
}

/**
* @param string $key
* @param mixed $value
Expand Down
25 changes: 23 additions & 2 deletions apps/user_ldap/lib/Group_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@

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;
Expand Down Expand Up @@ -83,7 +87,7 @@ public function __construct(Access $access, GroupPluginManager $groupPluginManag
$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);
}

Expand Down Expand Up @@ -664,15 +668,28 @@ public function getUserPrimaryGroup(string $dn) {
* @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, []);
Expand Down Expand Up @@ -786,6 +803,10 @@ public function getUserGroups($uid) {

$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;
}
Expand Down

0 comments on commit cb3faad

Please sign in to comment.