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 Sep 15, 2023
1 parent d32e03d commit d5d78f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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
18 changes: 17 additions & 1 deletion apps/user_ldap/lib/Group_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@

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 Psr\Log\LoggerInterface;

class Group_LDAP extends ABackend implements GroupInterface, IGroupLDAP, IGetDisplayNameBackend, IDeleteGroupBackend {
Expand Down Expand Up @@ -664,10 +666,20 @@ public function getUserPrimaryGroup(string $dn) {
* @throws Exception
* @throws ServerNotAvailableException
*/
public function getUserGroups($uid) {
public function getUserGroups($uid): array {
if (!$this->enabled) {
return [];
}
$ncUid = $uid;

$user = $this->access->userManager->get($uid);
if ($user instanceof OfflineUser) {
/** @var IConfig $config */
$config = \OCP\Server::get(IConfig::class);
$groupStr = $config->getUserValue($uid, 'user_ldap', 'group-memberships-' . $this->access->connection->getConfigPrefix(), '[]');
return \json_decode($groupStr) ?? [];
}

$cacheKey = 'getUserGroups' . $uid;
$userGroups = $this->access->connection->getFromCache($cacheKey);
if (!is_null($userGroups)) {
Expand Down Expand Up @@ -786,6 +798,10 @@ public function getUserGroups($uid) {

$groups = array_unique($groups, SORT_LOCALE_STRING);
$this->access->connection->writeToCache($cacheKey, $groups);
/** @var IConfig $config */
$config = \OCP\Server::get(IConfig::class);
$groupStr = \json_encode($groups);
$config->setUserValue($ncUid, 'user_ldap', 'group-memberships-' . $this->access->connection->getConfigPrefix(), $groupStr);

return $groups;
}
Expand Down

0 comments on commit d5d78f8

Please sign in to comment.