session = $session; $this->credentialsManager = $credentialsManager; $this->credentialsStore = $credentialsStore; $this->ldapFactory = $ldapFactory; $this ->setIdentifier('password::logincredentials') ->setScheme(self::SCHEME_PASSWORD) ->setText($l->t('Log-in credentials, save in database')) ->addParameters([ ]); $eventDispatcher->addServiceListener(UserLoggedInEvent::class, StorePasswordListener::class); $eventDispatcher->addServiceListener(PasswordUpdatedEvent::class, StorePasswordListener::class); } private function getCredentials(IUser $user): array { $credentials = $this->credentialsManager->retrieve($user->getUID(), self::CREDENTIALS_IDENTIFIER); if (is_null($credentials)) { // nothing saved in db, try to get it from the session and save it try { $sessionCredentials = $this->credentialsStore->getLoginCredentials(); if ($sessionCredentials->getUID() !== $user->getUID()) { // Can't take the credentials from the session as they are not the same user throw new CredentialsUnavailableException(); } $credentials = [ 'user' => $sessionCredentials->getLoginName(), 'password' => $sessionCredentials->getPassword(), ]; $this->credentialsManager->store($user->getUID(), self::CREDENTIALS_IDENTIFIER, $credentials); } catch (CredentialsUnavailableException $e) { throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved'); } } return $credentials; } /** * @return void */ public function manipulateStorageConfig(StorageConfig &$storage, ?IUser $user = null) { if (!isset($user)) { throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved'); } $credentials = $this->getCredentials($user); $loginKey = $storage->getBackendOption('login_ldap_attr'); if ($loginKey) { $backend = $user->getBackend(); if ($backend instanceof IUserBackend && $backend->getBackendName() === 'LDAP') { $value = $this->getLdapPropertyForUser($user, $loginKey); if ($value === null) { throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute not set for user ' . $user->getUID()); } $storage->setBackendOption('user', $value); } else { throw new InsufficientDataForMeaningfulAnswerException('Custom ldap attribute configured but user ' . $user->getUID() . ' is not an ldap user'); } } else { $storage->setBackendOption('user', $credentials['user']); } $storage->setBackendOption('password', $credentials['password']); } private function getLdapPropertyForUser(IUser $user, string $property): ?string { return $this->ldapFactory->getLDAPProvider()->getUserAttribute($user->getUID(), $property); } }