*/ class Ldap extends AbstractAdapter implements ServiceManagerAwareInterface, EventManagerAwareInterface { const USURPATION_USERNAMES_SEP = '='; /** * @var ServiceManager */ protected $serviceManager; /** * @var EventManager */ protected $eventManager; /** * @var LdapAuthAdapter */ protected $ldapAuthAdapter; /** * @var ModuleOptions */ protected $options; /** * @var string */ protected $usernameUsurpe; /** * * @param AuthEvent $e * @return boolean * @throws UnexpectedValueException * @see ChainableAdapter */ public function authenticate(AuthEvent $e) { if ($this->isSatisfied()) { $storage = $this->getStorage()->read(); $e->setIdentity($storage['identity']) ->setCode(AuthenticationResult::SUCCESS) ->setMessages(['Authentication successful.']); return; } $username = $e->getRequest()->getPost()->get('identity'); $credential = $e->getRequest()->getPost()->get('credential'); $success = $this->authenticateUsername($username, $credential); // Failure! if (! $success) { $e->setCode(AuthenticationResult::FAILURE) ->setMessages(['LDAP bind failed.']); $this->setSatisfied(false); return false; } $e->setIdentity($this->usernameUsurpe ?: $username); $this->setSatisfied(true); $storage = $this->getStorage()->read(); $storage['identity'] = $e->getIdentity(); $this->getStorage()->write($storage); $e->setCode(AuthenticationResult::SUCCESS) ->setMessages(['Authentication successful.']); $this->getEventManager()->trigger('userAuthenticated', $e); } /** * Authentifie l'identifiant et le mot de passe spécifiés. * * @param string $username Identifiant de connexion * @param string $credential Mot de passe * @return boolean */ public function authenticateUsername($username, $credential) { // si 2 logins sont fournis, cela active l'usurpation d'identité (à n'utiliser que pour les tests) : // - le format attendu est "loginUsurpateur=loginUsurpé" // - le mot de passe attendu est celui du compte usurpateur (loginUsurpateur) $this->usernameUsurpe = null; if (strpos($username, self::USURPATION_USERNAMES_SEP) > 0) { list($username, $this->usernameUsurpe) = explode(self::USURPATION_USERNAMES_SEP, $username, 2); if (!in_array($username, $this->getOptions()->getUsurpationAllowedUsernames())) { $this->usernameUsurpe = null; } } // LDAP auth $result = $this->getLdapAuthAdapter()->setUsername($username)->setPassword($credential)->authenticate(); $success = $result->isValid(); // verif existence du login usurpé if ($this->usernameUsurpe) { // s'il nexiste pas, échec de l'authentification if (!$this->getLdapAuthAdapter()->getLdap()->searchEntries("(supannAliasLogin=$this->usernameUsurpe)")) { $this->usernameUsurpe = null; $success = false; } } return $success; } /** * @param ModuleOptions $options */ public function setOptions(ModuleOptions $options) { $this->options = $options; } /** * @return ModuleOptions */ public function getOptions() { if (!$this->options instanceof ModuleOptions) { $options = array_merge( $this->getServiceManager()->get('zfcuser_module_options')->toArray(), $this->getServiceManager()->get('unicaen-auth_module_options')->toArray()); $this->setOptions(new ModuleOptions($options)); } return $this->options; } /** * @return \UnicaenApp\Options\ModuleOptions */ public function getAppModuleOptions() { return $this->getServiceManager()->get('unicaen-app_module_options'); } /** * get ldap connection adapter * * @return LdapAuthAdapter */ public function getLdapAuthAdapter() { if (null === $this->ldapAuthAdapter) { $options = []; if (($config = $this->getAppModuleOptions()->getLdap())) { foreach ($config['connection'] as $name => $connection) { $options[$name] = $connection['params']; } } $this->ldapAuthAdapter = new LdapAuthAdapter($options); // NB: array(array) } return $this->ldapAuthAdapter; } /** * set ldap connection adapter * * @param LdapAuthAdapter $authAdapter * @return Ldap */ public function setLdapAuthAdapter(LdapAuthAdapter $authAdapter) { $this->ldapAuthAdapter = $authAdapter; return $this; } /** * Get service manager * * @return ServiceManager */ public function getServiceManager() { return $this->serviceManager; } /** * Set service manager * * @param ServiceManager $serviceManager * @return Ldap */ public function setServiceManager(ServiceManager $serviceManager) { $this->serviceManager = $serviceManager; return $this; } /** * Retrieve EventManager instance * * @return EventManagerInterface */ public function getEventManager() { return $this->eventManager; } /** * Inject an EventManager instance * * @param EventManagerInterface $eventManager * @return Ldap */ public function setEventManager(EventManagerInterface $eventManager) { $this->eventManager = $eventManager; return $this; } }