Completed
Push — master ( 38f1ea...fd6c7c )
by Matze
04:05
created

Authentication::handleNotAuthenticatedRequest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
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\UserVO;
12
use Symfony\Component\HttpFoundation\RedirectResponse;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
16
use Symfony\Component\Routing\Route;
17
18
/**
19
 * @Middleware("Middleware.Authentication")
20
 */
21
class Authentication extends AbstractMiddleware
22
{
23
    /**
24
     * @var LoadUser
25
     */
26
    private $loadUser;
27
28
    /**
29
     * @Inject({
30
     *  "@Core.Authentication.LoadUser",
31
     * })
32
     * @param LoadUser $loadUser
33
     */
34 6
    public function __construct(LoadUser $loadUser)
35
    {
36 6
        $this->loadUser      = $loadUser;
37 6
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 4
    public function processRequest(Request $request, Route $route)
43
    {
44 4
        if ($request->attributes->has('user')) {
45
            return null;
46
        }
47
48 4
        $session = $request->getSession();
49 4
        $userId  = (int)$session->get('user_id');
50
51 4
        $user = $this->loadUser($userId);
52
53 4
        $request->attributes->set('user', $user);
54 4
        $request->attributes->set('user_id', $userId);
55
56 4
        $this->checkForRole($route, $user);
57
58 3
        if ($route->hasDefault('_guest')) {
59
            return null;
60
        }
61
62 3
        if (empty($userId)) {
63 1
            return $this->handleNotAuthenticatedRequest($request);
64
        }
65
66 2
        return null;
67
    }
68
69
    /**
70
     * @param Route $route
71
     * @param UserVO $user
72
     * @throws MethodNotAllowedException
73
     */
74 4
    protected function checkForRole(Route $route, UserVO $user)
75
    {
76 4
        if ($route->hasDefault('_role')) {
77 1
            $role = $route->getDefault('_role');
78 1
            if (!in_array($role, $user->roles)) {
79 1
                throw new MethodNotAllowedException([], sprintf('Need role %s', $role));
80
            }
81
        }
82 3
    }
83
84
    /**
85
     * @param int $userId
86
     * @return AnonymusUserVO|UserVO
87
     */
88 4
    private function loadUser(int $userId) : UserVO
89
    {
90 4
        if ($userId > 0) {
91
            try {
92 3
                return $this->loadUser->loadUserById($userId);
93
            } catch (UserNotFoundException $e) {
94
                return new AnonymusUserVO();
95
            }
96
        } else {
97 1
            return new AnonymusUserVO();
98
        }
99
    }
100
101
    /**
102
     * @param Request $request
103
     * @return RedirectResponse
104
     * @throws UserException
105
     */
106 1
    private function handleNotAuthenticatedRequest(Request $request)
107
    {
108 1
        if ($request->isXmlHttpRequest()) {
109
            throw new UserException(gettext('Not logged in'));
110
        }
111 1
        return new RedirectResponse('#/login');
112
    }
113
}
114