diff --git a/apps/user_ldap/lib/Group_Proxy.php b/apps/user_ldap/lib/Group_Proxy.php index 7410aa6a6e9..6dff7ea628c 100644 --- a/apps/user_ldap/lib/Group_Proxy.php +++ b/apps/user_ldap/lib/Group_Proxy.php @@ -39,12 +39,11 @@ use OCP\GroupInterface; use OCP\IConfig; use OCP\IUserManager; +/** + * @template-extends Proxy + */ class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend, IDeleteGroupBackend, IBatchMethodsBackend, IIsAdminBackend { - private $backends = []; - private ?Group_LDAP $refBackend = null; - private Helper $helper; private GroupPluginManager $groupPluginManager; - private bool $isSetUp = false; private IConfig $config; private IUserManager $ncUserManager; @@ -56,28 +55,15 @@ class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGet IConfig $config, IUserManager $ncUserManager, ) { - parent::__construct($ldap, $accessFactory); - $this->helper = $helper; + parent::__construct($helper, $ldap, $accessFactory); $this->groupPluginManager = $groupPluginManager; $this->config = $config; $this->ncUserManager = $ncUserManager; } - 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); } /** diff --git a/apps/user_ldap/lib/Proxy.php b/apps/user_ldap/lib/Proxy.php index e2e33f3007b..2c8e7f605fc 100644 --- a/apps/user_ldap/lib/Proxy.php +++ b/apps/user_ldap/lib/Proxy.php @@ -37,6 +37,9 @@ use OCA\User_LDAP\Mapping\UserMapping; use OCP\ICache; use OCP\Server; +/** + * @template T + */ abstract class Proxy { /** @var array */ private static array $accesses = []; @@ -45,7 +48,15 @@ abstract class Proxy { private ?ICache $cache = null; private AccessFactory $accessFactory; + /** @var T[] */ + protected array $backends = []; + /** @var ?T */ + protected $refBackend = null; + + protected bool $isSetUp = false; + public function __construct( + private Helper $helper, ILDAPWrapper $ldap, AccessFactory $accessFactory ) { @@ -57,6 +68,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); diff --git a/apps/user_ldap/lib/User_Proxy.php b/apps/user_ldap/lib/User_Proxy.php index 8ad3290cf48..1178b690ddd 100644 --- a/apps/user_ldap/lib/User_Proxy.php +++ b/apps/user_ldap/lib/User_Proxy.php @@ -42,13 +42,10 @@ use OCP\User\Backend\IProvideEnabledStateBackend; use OCP\UserInterface; use Psr\Log\LoggerInterface; +/** + * @template-extends Proxy + */ class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend { - /** @var User_LDAP[] */ - private array $backends = []; - private ?User_LDAP $refBackend = null; - - private bool $isSetUp = false; - private Helper $helper; private INotificationManager $notificationManager; private UserPluginManager $userPluginManager; private LoggerInterface $logger; @@ -63,35 +60,21 @@ class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP LoggerInterface $logger, DeletedUsersIndex $deletedUsersIndex, ) { - parent::__construct($ldap, $accessFactory); - $this->helper = $helper; + parent::__construct($helper, $ldap, $accessFactory); $this->notificationManager = $notificationManager; $this->userPluginManager = $userPluginManager; $this->logger = $logger; $this->deletedUsersIndex = $deletedUsersIndex; } - 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, + ); } /**