Completed
Push — master ( 1f9fde...c86638 )
by Matze
05:56
created

TokenAuthentication::handleNotAuthenticatedRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
1
<?php
2
3
namespace BrainExe\Core\Middleware;
4
5
use BrainExe\Annotations\Annotations\Inject;
6
use BrainExe\Core\Annotations\Middleware;
7
use BrainExe\Core\Application\UserException;
8
use BrainExe\Core\Authentication\AnonymusUserVO;
9
use BrainExe\Core\Authentication\Exception\UserNotFoundException;
10
use BrainExe\Core\Authentication\LoadUser;
11
use BrainExe\Core\Authentication\Token;
12
use BrainExe\Core\Authentication\UserVO;
13
use BrainExe\Core\Translation\TranslationTrait;
14
use Symfony\Component\HttpFoundation\RedirectResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
18
use Symfony\Component\Routing\Route;
19
20
/**
21
 * @Middleware("Middleware.TokenAuthentication")
22
 */
23
class TokenAuthentication extends AbstractMiddleware
24
{
25
    use TranslationTrait;
26
27
    /**
28
     * @var LoadUser
29
     */
30
    private $loadUser;
31
32
    /**
33
     * @var Token
34
     */
35
    private $token;
36
37
    /**
38
     * @Inject({
39
     *  "@Core.Authentication.LoadUser",
40
     *  "@Core.Authentication.Token",
41
     * })
42
     * @param LoadUser $loadUser
43
     * @param Token $token
44
     */
45
    public function __construct(LoadUser $loadUser, Token $token)
46
    {
47
        $this->loadUser = $loadUser;
48
        $this->token    = $token;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function processRequest(Request $request, Route $route)
55
    {
56
        $token = $request->get('accessToken');
57
58
        if (empty($token)) {
59
            return null;
60
        }
61
62
        $userId = $this->token->hasUserForRole($token);
63
        if (empty($userId)) {
64
            return null;
65
        }
66
67
        $user = $this->loadUser($userId);
68
69
        $request->attributes->set('user', $user);
70
    }
71
72
    /**
73
     * @todo check/implement
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
74
     * @param Route $route
75
     * @param UserVO $user
76
     * @throws MethodNotAllowedException
77
     */
78 View Code Duplication
    protected function checkForRole(Route $route, UserVO $user)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        if ($route->hasDefault('_role')) {
81
            $role = $route->getDefault('_role');
82
            if (!in_array($role, $user->roles)) {
83
                throw new MethodNotAllowedException([], sprintf('Need role %s', $role));
84
            }
85
        }
86
    }
87
88
    /**
89
     * @param int $userId
90
     * @return AnonymusUserVO|UserVO
91
     */
92 View Code Duplication
    private function loadUser(int $userId) : UserVO
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        if ($userId > 0) {
95
            try {
96
                return $this->loadUser->loadUserById($userId);
97
            } catch (UserNotFoundException $e) {
98
                return new AnonymusUserVO();
99
            }
100
        } else {
101
            return new AnonymusUserVO();
102
        }
103
    }
104
}
105