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\DatabaseUserProvider; |
10
|
|
|
use BrainExe\Core\Authentication\UserVO; |
11
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException; |
15
|
|
|
use Symfony\Component\Routing\Route; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @Middleware("Middleware.Authentication", priority=8) |
19
|
|
|
*/ |
20
|
|
|
class Authentication extends AbstractMiddleware |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
private $guestsAllowed; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var DatabaseUserProvider |
30
|
|
|
*/ |
31
|
|
|
private $userProvider; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
private $allowedPrivateIps; |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @Inject({ |
40
|
|
|
* "%application.guests_allowed%", |
41
|
|
|
* "@DatabaseUserProvider", |
42
|
|
|
* }) |
43
|
|
|
* @param boolean $guestsAllowed |
44
|
|
|
* @param DatabaseUserProvider $userProvider |
45
|
|
|
*/ |
46
|
7 |
|
public function __construct($guestsAllowed, DatabaseUserProvider $userProvider) |
47
|
|
|
{ |
48
|
7 |
|
$this->guestsAllowed = $guestsAllowed; |
49
|
7 |
|
$this->userProvider = $userProvider; |
50
|
7 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
1 |
|
public function processResponse(Request $request, Response $response) |
56
|
|
|
{ |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
5 |
|
public function processRequest(Request $request, Route $route) |
63
|
|
|
{ |
64
|
5 |
|
$session = $request->getSession(); |
65
|
5 |
|
$userId = $session->get('user_id'); |
66
|
|
|
|
67
|
5 |
|
$user = $this->loadUser($userId); |
68
|
|
|
|
69
|
5 |
|
$request->attributes->set('user', $user); |
70
|
5 |
|
$request->attributes->set('user_id', $userId); |
71
|
|
|
|
72
|
5 |
|
$this->checkForRole($route, $user); |
73
|
|
|
|
74
|
4 |
|
if ($this->guestsAllowed || $route->hasDefault('_guest')) { |
75
|
1 |
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
3 |
|
if (!$userId) { |
79
|
1 |
|
if ($request->isXmlHttpRequest()) { |
80
|
|
|
throw new UserException(gettext('Not logged in')); |
81
|
|
|
} |
82
|
1 |
|
return new RedirectResponse('#/login'); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param Route $route |
90
|
|
|
* @param UserVO $user |
91
|
|
|
* @throws MethodNotAllowedException |
92
|
|
|
*/ |
93
|
5 |
|
protected function checkForRole(Route $route, $user) |
94
|
|
|
{ |
95
|
5 |
|
if ($route->hasDefault('_role')) { |
96
|
1 |
|
$role = $route->getDefault('_role'); |
97
|
1 |
|
if (!in_array($role, $user->roles)) { |
98
|
1 |
|
throw new MethodNotAllowedException([]); |
99
|
|
|
} |
100
|
|
|
} |
101
|
4 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param int $userId |
105
|
|
|
* @return AnonymusUserVO|UserVO |
106
|
|
|
*/ |
107
|
5 |
|
private function loadUser($userId) |
108
|
|
|
{ |
109
|
5 |
|
if ($userId > 0) { |
110
|
4 |
|
$user = $this->userProvider->loadUserById($userId); |
111
|
4 |
|
return $user; |
112
|
|
|
} else { |
113
|
1 |
|
$user = new AnonymusUserVO(); |
114
|
1 |
|
return $user; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.