|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © OXID eSales AG. All rights reserved. |
| 5 | + * See LICENSE file for license details. |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace OxidEsales\GraphQL\Base\Event\Subscriber; |
| 11 | + |
| 12 | +use OxidEsales\Eshop\Application\Model\User; |
| 13 | +use OxidEsales\EshopCommunity\Internal\Transition\ShopEvents\AfterModelUpdateEvent; |
| 14 | +use OxidEsales\EshopCommunity\Internal\Transition\ShopEvents\BeforeModelUpdateEvent; |
| 15 | +use OxidEsales\GraphQL\Base\Infrastructure\RefreshTokenRepositoryInterface; |
| 16 | +use OxidEsales\GraphQL\Base\Infrastructure\Token; |
| 17 | +use OxidEsales\GraphQL\Base\Service\UserModelService; |
| 18 | +use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
| 19 | +use Symfony\Contracts\EventDispatcher\Event; |
| 20 | + |
| 21 | +class PasswordChangeSubscriber implements EventSubscriberInterface |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Whether the password had been changed. |
| 25 | + * |
| 26 | + * @var array |
| 27 | + */ |
| 28 | + protected array $userIdWithChangedPwd = []; |
| 29 | + |
| 30 | + public function __construct( |
| 31 | + private readonly UserModelService $userModelService, |
| 32 | + private readonly RefreshTokenRepositoryInterface $refreshTokenRepository, |
| 33 | + private readonly Token $tokenInfrastructure |
| 34 | + ) { |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Handle ApplicationModelChangedEvent. |
| 39 | + * |
| 40 | + * @param Event $event Event to be handled |
| 41 | + */ |
| 42 | + public function handleBeforeUpdate(Event $event): void |
| 43 | + { |
| 44 | + /** @phpstan-ignore-next-line method.notFound */ |
| 45 | + $model = $event->getModel(); |
| 46 | + |
| 47 | + if (!$model instanceof User) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $newPassword = $model->getFieldData('oxpassword'); |
| 52 | + if (!$this->userModelService->isPasswordChanged($model->getId(), $newPassword)) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $this->userIdWithChangedPwd[$model->getId()] = true; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Handle ApplicationModelChangedEvent. |
| 61 | + * |
| 62 | + * @param Event $event Event to be handled |
| 63 | + */ |
| 64 | + public function handleAfterUpdate(Event $event): void |
| 65 | + { |
| 66 | + /** @phpstan-ignore-next-line method.notFound */ |
| 67 | + $model = $event->getModel(); |
| 68 | + |
| 69 | + if (!$model instanceof User || !isset($this->userIdWithChangedPwd[$model->getId()])) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + $this->refreshTokenRepository->invalidateUserTokens($model->getId()); |
| 74 | + $this->tokenInfrastructure->invalidateUserTokens($model->getId()); |
| 75 | + unset($this->userIdWithChangedPwd[$model->getId()]); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns an array of event names this subscriber wants to listen to. |
| 80 | + * |
| 81 | + * The array keys are event names and the value can be: |
| 82 | + * |
| 83 | + * * The method name to call (priority defaults to 0) |
| 84 | + * * An array composed of the method name to call and the priority |
| 85 | + * * An array of arrays composed of the method names to call and respective |
| 86 | + * priorities, or 0 if unset |
| 87 | + * |
| 88 | + * For instance: |
| 89 | + * |
| 90 | + * * array('eventName' => 'methodName') |
| 91 | + * * array('eventName' => array('methodName', $priority)) |
| 92 | + * * array('eventName' => array(array('methodName1', $priority), array('methodName2'))) |
| 93 | + * |
| 94 | + * @return array<class-string,string> |
| 95 | + */ |
| 96 | + public static function getSubscribedEvents(): array |
| 97 | + { |
| 98 | + return [ |
| 99 | + BeforeModelUpdateEvent::class => 'handleBeforeUpdate', |
| 100 | + AfterModelUpdateEvent::class => 'handleAfterUpdate' |
| 101 | + ]; |
| 102 | + } |
| 103 | +} |
0 commit comments