File size: 3,199 Bytes
cf14c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php

declare(strict_types=1);

namespace Presentation\RequestHandlers;

use Easy\Http\Message\RequestMethod;
use Easy\Router\Attributes\Middleware;
use Easy\Router\Attributes\Route;
use Presentation\Middlewares\ViewMiddleware;
use Presentation\Response\RedirectResponse;
use Presentation\Response\ViewResponse;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shared\Infrastructure\CommandBus\Dispatcher;
use Shared\Infrastructure\CommandBus\Exception\NoHandlerFoundException;
use User\Application\Commands\ReadUserCommand;
use User\Domain\Entities\UserEntity;
use User\Domain\Exceptions\InvalidTokenException;
use User\Domain\Exceptions\UserNotFoundException;
use User\Domain\ValueObjects\RecoveryToken;
use User\Domain\ValueObjects\Role;

/** 
 * Class PasswordResetRequestHandler
 *
 * A class representing an API route for handling user requests to reset their 
 * passwords.
 */
#[Middleware(ViewMiddleware::class)]
#[Route(path: '/[locale:locale]?/recovery/[uuid:id]/[*:token]', method: RequestMethod::GET)]
class PasswordResetRequestHandler extends AbstractRequestHandler implements
    RequestHandlerInterface
{
    /**
     * Constructs an instance of this class, initializing the dispatcher.
     *
     * @param Dispatcher $dispatcher The dispatcher used to dispatch CommandBus 
     * commands.
     */
    public function __construct(
        private Dispatcher $dispatcher
    ) {
    }

    /**
     * Handles incoming server requests for password recovery.
     *
     * If user is already recovered, it will redirect accordingly to its role.
     * Otherwise, it tries to read user and validate the recovery token. If user 
     * is not found or recovery token is invalid, redirect to login page. Upon 
     * successful validation, it will render the password reset view.
     *
     * @param ServerRequestInterface $request The incoming server request
     * @return ResponseInterface Returns a view response to display the password 
     * reset form, or redirects in case of errors or if user is already logged 
     * in.
     * @throws NoHandlerFoundException Thrown if no appropriate handler is found 
     * for the dispatched command
     */
    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $user = $request->getAttribute(UserEntity::class);
        if ($user) {
            return new RedirectResponse(
                $user->getRole() == Role::ADMIN ? '/admin' : '/app'
            );
        }

        $id = $request->getAttribute('id');
        $token = new RecoveryToken($request->getAttribute('token'));

        $cmd = new ReadUserCommand($id);

        try {
            /** @var UserEntity */
            $user = $this->dispatcher->dispatch($cmd);
            $user->validateRecoveryToken($token);
        } catch (UserNotFoundException | InvalidTokenException) {
            return new RedirectResponse('/login');
        }

        $data = [
            'id' => $id,
            'token' => $token->value,
        ];

        return new ViewResponse(
            '/templates/auth/reset.twig',
            $data
        );
    }
}