feat(user_ldap): upstream common code into Proxy class and add public getters for backends

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2025-01-30 11:49:58 +01:00
parent 2c773033bc
commit e55806b546
No known key found for this signature in database
GPG key ID: A3E2F658B28C760A
3 changed files with 59 additions and 47 deletions

View file

@ -18,11 +18,10 @@ use OCP\GroupInterface;
use OCP\IConfig;
use OCP\IUserManager;
/**
* @template-extends Proxy<Group_LDAP>
*/
class Group_Proxy extends Proxy implements GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend, IDeleteGroupBackend, IBatchMethodsBackend, IIsAdminBackend {
private $backends = [];
private ?Group_LDAP $refBackend = null;
private bool $isSetUp = false;
public function __construct(
private Helper $helper,
ILDAPWrapper $ldap,
@ -31,24 +30,12 @@ class Group_Proxy extends Proxy implements GroupInterface, IGroupLDAP, IGetDispl
private IConfig $config,
private IUserManager $ncUserManager,
) {
parent::__construct($ldap, $accessFactory);
parent::__construct($helper, $ldap, $accessFactory);
}
protected function setup(): void {
if ($this->isSetUp) {
return;
}
$serverConfigPrefixes = $this->helper->getServerConfigurationPrefixes(true);
foreach ($serverConfigPrefixes as $configPrefix) {
$this->backends[$configPrefix] =
new Group_LDAP($this->getAccess($configPrefix), $this->groupPluginManager, $this->config, $this->ncUserManager);
if (is_null($this->refBackend)) {
$this->refBackend = $this->backends[$configPrefix];
}
}
$this->isSetUp = true;
protected function newInstance(string $configPrefix): Group_LDAP {
return new Group_LDAP($this->getAccess($configPrefix), $this->groupPluginManager, $this->config, $this->ncUserManager);
}
/**

View file

@ -12,13 +12,24 @@ use OCA\User_LDAP\Mapping\UserMapping;
use OCP\ICache;
use OCP\Server;
/**
* @template T
*/
abstract class Proxy {
/** @var array<string,Access> */
private static array $accesses = [];
private ?bool $isSingleBackend = null;
private ?ICache $cache = null;
/** @var T[] */
protected array $backends = [];
/** @var ?T */
protected $refBackend = null;
protected bool $isSetUp = false;
public function __construct(
private Helper $helper,
private ILDAPWrapper $ldap,
private AccessFactory $accessFactory,
) {
@ -28,6 +39,36 @@ abstract class Proxy {
}
}
protected function setup(): void {
if ($this->isSetUp) {
return;
}
$serverConfigPrefixes = $this->helper->getServerConfigurationPrefixes(true);
foreach ($serverConfigPrefixes as $configPrefix) {
$this->backends[$configPrefix] = $this->newInstance($configPrefix);
if (is_null($this->refBackend)) {
$this->refBackend = $this->backends[$configPrefix];
}
}
$this->isSetUp = true;
}
/**
* @return T
*/
abstract protected function newInstance(string $configPrefix): object;
/**
* @return T
*/
public function getBackend(string $configPrefix): object {
$this->setup();
return $this->backends[$configPrefix];
}
private function addAccess(string $configPrefix): void {
$userMap = Server::get(UserMapping::class);
$groupMap = Server::get(GroupMapping::class);

View file

@ -18,13 +18,10 @@ use OCP\User\Backend\IProvideEnabledStateBackend;
use OCP\UserInterface;
use Psr\Log\LoggerInterface;
/**
* @template-extends Proxy<User_LDAP>
*/
class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP, ILimitAwareCountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend {
/** @var User_LDAP[] */
private array $backends = [];
private ?User_LDAP $refBackend = null;
private bool $isSetUp = false;
public function __construct(
private Helper $helper,
ILDAPWrapper $ldap,
@ -34,30 +31,17 @@ class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP
private LoggerInterface $logger,
private DeletedUsersIndex $deletedUsersIndex,
) {
parent::__construct($ldap, $accessFactory);
parent::__construct($helper, $ldap, $accessFactory);
}
protected function setup(): void {
if ($this->isSetUp) {
return;
}
$serverConfigPrefixes = $this->helper->getServerConfigurationPrefixes(true);
foreach ($serverConfigPrefixes as $configPrefix) {
$this->backends[$configPrefix] = new User_LDAP(
$this->getAccess($configPrefix),
$this->notificationManager,
$this->userPluginManager,
$this->logger,
$this->deletedUsersIndex,
);
if (is_null($this->refBackend)) {
$this->refBackend = $this->backends[$configPrefix];
}
}
$this->isSetUp = true;
protected function newInstance(string $configPrefix): User_LDAP {
return new User_LDAP(
$this->getAccess($configPrefix),
$this->notificationManager,
$this->userPluginManager,
$this->logger,
$this->deletedUsersIndex,
);
}
/**