. */ namespace SP\Providers\Auth\Ldap; use Exception; use SP\Core\Events\EventDispatcher; use SP\Domain\Auth\Ports\LdapActionsService; use SP\Domain\Auth\Ports\LdapConnectionInterface; use SP\Domain\Auth\Ports\LdapService; use SP\Domain\Core\Events\EventDispatcherInterface; use function SP\__u; /** * Class LdapBase * * @package SP\Providers\Auth\Ldap */ abstract class LdapBase implements LdapService { protected string $server; /** * LdapBase constructor. * * @param LdapConnectionInterface $ldapConnection * @param LdapActionsService $ldapActions * @param LdapParams $ldapParams * @param EventDispatcher $eventDispatcher */ public function __construct( protected readonly LdapConnectionInterface $ldapConnection, protected readonly LdapActionsService $ldapActions, protected readonly LdapParams $ldapParams, protected readonly EventDispatcherInterface $eventDispatcher ) { $this->server = $this->pickServer(); } abstract protected function pickServer(): string; /** * @param EventDispatcher $eventDispatcher * @param LdapConnectionInterface $ldapConnection * @param LdapActionsService $ldapActions * @param LdapParams|null $ldapParams * @return LdapService * @throws LdapException * @throws Exception */ public static function factory( EventDispatcherInterface $eventDispatcher, LdapConnectionInterface $ldapConnection, LdapActionsService $ldapActions, ?LdapParams $ldapParams = null ): LdapService { if (null !== $ldapParams) { $ldapConnection = $ldapConnection->mutate($ldapParams); $ldapActions = $ldapActions->mutate($ldapParams); } $ldapConnection->checkConnection(); switch ($ldapParams->getType()) { case LdapTypeEnum::STD: return new LdapStd($ldapConnection, $ldapActions, $ldapParams, $eventDispatcher); case LdapTypeEnum::ADS: return new LdapMsAds($ldapConnection, $ldapActions, $ldapParams, $eventDispatcher); case LdapTypeEnum::AZURE: throw new LdapException(__u('To be implemented')); } throw LdapException::error(__u('LDAP type not set')); } public function actions(): LdapActionsService { return $this->ldapActions; } /** * @throws LdapException */ public function connect(?string $bindDn = null, ?string $bindPass = null): void { $this->ldapConnection->connect($bindDn, $bindPass); } public function getServer(): string { return $this->server; } /** * @return string */ protected function getGroupFromParams(): string { if (stripos($this->ldapParams->getGroup(), 'cn') === 0) { return LdapUtil::getGroupName($this->ldapParams->getGroup()) ?: ''; } return $this->ldapParams->getGroup() ?? ''; } /** * @return string * @throws LdapException */ protected function getGroupDn(): string { if (stripos($this->ldapParams->getGroup(), 'cn') === 0) { return $this->ldapParams->getGroup(); } return $this->ldapActions->searchGroupsDn($this->getGroupObjectFilter())[0] ?? ''; } }