@@ -42,62 +42,62 @@ |
||
| 42 | 42 | * Username and password from login credentials, saved in DB |
| 43 | 43 | */ |
| 44 | 44 | class LoginCredentials extends AuthMechanism { |
| 45 | - public const CREDENTIALS_IDENTIFIER = 'password::logincredentials/credentials'; |
|
| 46 | - |
|
| 47 | - /** @var ISession */ |
|
| 48 | - protected $session; |
|
| 49 | - |
|
| 50 | - /** @var ICredentialsManager */ |
|
| 51 | - protected $credentialsManager; |
|
| 52 | - |
|
| 53 | - /** @var CredentialsStore */ |
|
| 54 | - private $credentialsStore; |
|
| 55 | - |
|
| 56 | - public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager, CredentialsStore $credentialsStore, IEventDispatcher $eventDispatcher) { |
|
| 57 | - $this->session = $session; |
|
| 58 | - $this->credentialsManager = $credentialsManager; |
|
| 59 | - $this->credentialsStore = $credentialsStore; |
|
| 60 | - |
|
| 61 | - $this |
|
| 62 | - ->setIdentifier('password::logincredentials') |
|
| 63 | - ->setScheme(self::SCHEME_PASSWORD) |
|
| 64 | - ->setText($l->t('Log-in credentials, save in database')) |
|
| 65 | - ->addParameters([ |
|
| 66 | - ]); |
|
| 67 | - |
|
| 68 | - $eventDispatcher->addServiceListener(UserLoggedInEvent::class, StorePasswordListener::class); |
|
| 69 | - $eventDispatcher->addServiceListener(PasswordUpdatedEvent::class, StorePasswordListener::class); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - private function getCredentials(IUser $user): array { |
|
| 73 | - $credentials = $this->credentialsManager->retrieve($user->getUID(), self::CREDENTIALS_IDENTIFIER); |
|
| 74 | - |
|
| 75 | - if (is_null($credentials)) { |
|
| 76 | - // nothing saved in db, try to get it from the session and save it |
|
| 77 | - try { |
|
| 78 | - $sessionCredentials = $this->credentialsStore->getLoginCredentials(); |
|
| 79 | - |
|
| 80 | - $credentials = [ |
|
| 81 | - 'user' => $sessionCredentials->getLoginName(), |
|
| 82 | - 'password' => $sessionCredentials->getPassword() |
|
| 83 | - ]; |
|
| 84 | - |
|
| 85 | - $this->credentialsManager->store($user->getUID(), self::CREDENTIALS_IDENTIFIER, $credentials); |
|
| 86 | - } catch (CredentialsUnavailableException $e) { |
|
| 87 | - throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved'); |
|
| 88 | - } |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return $credentials; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) { |
|
| 95 | - if (!isset($user)) { |
|
| 96 | - throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved'); |
|
| 97 | - } |
|
| 98 | - $credentials = $this->getCredentials($user); |
|
| 99 | - |
|
| 100 | - $storage->setBackendOption('user', $credentials['user']); |
|
| 101 | - $storage->setBackendOption('password', $credentials['password']); |
|
| 102 | - } |
|
| 45 | + public const CREDENTIALS_IDENTIFIER = 'password::logincredentials/credentials'; |
|
| 46 | + |
|
| 47 | + /** @var ISession */ |
|
| 48 | + protected $session; |
|
| 49 | + |
|
| 50 | + /** @var ICredentialsManager */ |
|
| 51 | + protected $credentialsManager; |
|
| 52 | + |
|
| 53 | + /** @var CredentialsStore */ |
|
| 54 | + private $credentialsStore; |
|
| 55 | + |
|
| 56 | + public function __construct(IL10N $l, ISession $session, ICredentialsManager $credentialsManager, CredentialsStore $credentialsStore, IEventDispatcher $eventDispatcher) { |
|
| 57 | + $this->session = $session; |
|
| 58 | + $this->credentialsManager = $credentialsManager; |
|
| 59 | + $this->credentialsStore = $credentialsStore; |
|
| 60 | + |
|
| 61 | + $this |
|
| 62 | + ->setIdentifier('password::logincredentials') |
|
| 63 | + ->setScheme(self::SCHEME_PASSWORD) |
|
| 64 | + ->setText($l->t('Log-in credentials, save in database')) |
|
| 65 | + ->addParameters([ |
|
| 66 | + ]); |
|
| 67 | + |
|
| 68 | + $eventDispatcher->addServiceListener(UserLoggedInEvent::class, StorePasswordListener::class); |
|
| 69 | + $eventDispatcher->addServiceListener(PasswordUpdatedEvent::class, StorePasswordListener::class); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + private function getCredentials(IUser $user): array { |
|
| 73 | + $credentials = $this->credentialsManager->retrieve($user->getUID(), self::CREDENTIALS_IDENTIFIER); |
|
| 74 | + |
|
| 75 | + if (is_null($credentials)) { |
|
| 76 | + // nothing saved in db, try to get it from the session and save it |
|
| 77 | + try { |
|
| 78 | + $sessionCredentials = $this->credentialsStore->getLoginCredentials(); |
|
| 79 | + |
|
| 80 | + $credentials = [ |
|
| 81 | + 'user' => $sessionCredentials->getLoginName(), |
|
| 82 | + 'password' => $sessionCredentials->getPassword() |
|
| 83 | + ]; |
|
| 84 | + |
|
| 85 | + $this->credentialsManager->store($user->getUID(), self::CREDENTIALS_IDENTIFIER, $credentials); |
|
| 86 | + } catch (CredentialsUnavailableException $e) { |
|
| 87 | + throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved'); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return $credentials; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + public function manipulateStorageConfig(StorageConfig &$storage, IUser $user = null) { |
|
| 95 | + if (!isset($user)) { |
|
| 96 | + throw new InsufficientDataForMeaningfulAnswerException('No login credentials saved'); |
|
| 97 | + } |
|
| 98 | + $credentials = $this->getCredentials($user); |
|
| 99 | + |
|
| 100 | + $storage->setBackendOption('user', $credentials['user']); |
|
| 101 | + $storage->setBackendOption('password', $credentials['password']); |
|
| 102 | + } |
|
| 103 | 103 | } |