. */ namespace SP\Providers\Auth\Ldap; use SP\Core\Events\EventDispatcher; /** * Class Ldap * * @package SP\Providers\Auth\Ldap */ abstract class Ldap implements LdapInterface { /** * @var LdapParams */ protected $ldapParams; /** * @var EventDispatcher */ protected $eventDispatcher; /** * @var LdapActions */ protected $ldapActions; /** * @var LdapConnectionInterface */ protected $ldapConnection; /** * @var string */ private $server; /** * LdapBase constructor. * * @param LdapConnectionInterface $ldapConnection * @param EventDispatcher $eventDispatcher * * @throws LdapException */ public function __construct(LdapConnectionInterface $ldapConnection, EventDispatcher $eventDispatcher) { $this->ldapConnection = $ldapConnection; $this->ldapParams = $ldapConnection->getLdapParams(); $this->server = $this->pickServer(); $this->ldapConnection->setServer($this->server); $this->eventDispatcher = $eventDispatcher; $this->ldapActions = new LdapActions($ldapConnection, $eventDispatcher); } /** * Obtener el servidor de LDAP a utilizar * * @return mixed */ protected abstract function pickServer(); /** * @param LdapParams $ldapParams * @param EventDispatcher $eventDispatcher * @param bool $debug * * @return LdapInterface * @throws LdapException */ public static function factory(LdapParams $ldapParams, EventDispatcher $eventDispatcher, bool $debug) { $ldapConnection = new LdapConnection($ldapParams, $eventDispatcher, $debug); $ldapConnection->checkConnection(); switch ($ldapParams->getType()) { case LdapTypeInterface::LDAP_STD: return new LdapStd($ldapConnection, $eventDispatcher); break; case LdapTypeInterface::LDAP_ADS: return new LdapMsAds($ldapConnection, $eventDispatcher); break; } throw new LdapException(__u('LDAP type not set')); } /** * @return LdapActions */ public function getLdapActions(): LdapActions { return $this->ldapActions; } /** * Realizar la conexión al servidor de LDAP. * * @return resource * @throws LdapException */ public function connect() { return $this->ldapConnection->connectAndBind(); } /** * @param string|null $bindDn * @param string|null $bindPass * * @return bool */ public function bind(?string $bindDn = null, ?string $bindPass = null): bool { return $this->ldapConnection->bind($bindDn, $bindPass); } /** * @return string */ 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]; } }