您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

StorePasswordListener.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_External\Listener;
  26. use OCA\Files_External\Lib\Auth\Password\LoginCredentials;
  27. use OCP\EventDispatcher\Event;
  28. use OCP\EventDispatcher\IEventListener;
  29. use OCP\Security\ICredentialsManager;
  30. use OCP\User\Events\PasswordUpdatedEvent;
  31. use OCP\User\Events\UserLoggedInEvent;
  32. /** @template-implements IEventListener<PasswordUpdatedEvent|UserLoggedInEvent> */
  33. class StorePasswordListener implements IEventListener {
  34. /** @var ICredentialsManager */
  35. private $credentialsManager;
  36. public function __construct(ICredentialsManager $credentialsManager) {
  37. $this->credentialsManager = $credentialsManager;
  38. }
  39. public function handle(Event $event): void {
  40. if (!$event instanceof UserLoggedInEvent && !$event instanceof PasswordUpdatedEvent) {
  41. return;
  42. }
  43. if ($event instanceof UserLoggedInEvent && $event->isTokenLogin()) {
  44. return;
  45. }
  46. $storedCredentials = $this->credentialsManager->retrieve($event->getUser()->getUID(), LoginCredentials::CREDENTIALS_IDENTIFIER);
  47. if (!$storedCredentials) {
  48. return;
  49. }
  50. $newCredentials = $storedCredentials;
  51. $shouldUpdate = false;
  52. if (($storedCredentials['password'] ?? null) !== $event->getPassword() && $event->getPassword() !== null) {
  53. $shouldUpdate = true;
  54. $newCredentials['password'] = $event->getPassword();
  55. }
  56. if ($event instanceof UserLoggedInEvent && ($storedCredentials['user'] ?? null) !== $event->getLoginName()) {
  57. $shouldUpdate = true;
  58. $newCredentials['user'] = $event->getLoginName();
  59. }
  60. if ($shouldUpdate) {
  61. $this->credentialsManager->store($event->getUser()->getUID(), LoginCredentials::CREDENTIALS_IDENTIFIER, $newCredentials);
  62. }
  63. }
  64. }