Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(user_ldap): upstream common code into Proxy class and add public…
… getters for backends

Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc authored and AndyScherzinger committed Feb 9, 2025
commit 8eb0041df8689b339efb251ea0776515d5cabaaf
25 changes: 6 additions & 19 deletions apps/user_ldap/lib/Group_Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@
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,
Expand All @@ -31,24 +30,12 @@ public function __construct(
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);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions apps/user_ldap/lib/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@
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,
) {
Expand All @@ -28,6 +39,36 @@ public function __construct(
}
}

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);
Expand Down
40 changes: 12 additions & 28 deletions apps/user_ldap/lib/User_Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
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,
Expand All @@ -34,30 +31,17 @@ public function __construct(
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,
);
}

/**
Expand Down