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
|
|
|
public function __construct(LoadUser $loadUser) |
35
|
|
|
{ |
36
|
|
|
$this->loadUser = $loadUser; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
|
|
public function processRequest(Request $request, Route $route) |
43
|
|
|
{ |
44
|
|
|
if ($request->attributes->has('user')) { |
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$session = $request->getSession(); |
49
|
|
|
$userId = (int)$session->get('user_id'); |
50
|
|
|
|
51
|
|
|
$user = $this->loadUser($userId); |
52
|
|
|
|
53
|
|
|
$request->attributes->set('user', $user); |
54
|
|
|
$request->attributes->set('user_id', $userId); |
55
|
|
|
|
56
|
|
|
$this->checkForRole($route, $user); |
57
|
|
|
|
58
|
|
|
if ($route->hasDefault('_guest')) { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!$userId) { |
63
|
|
|
if ($request->isXmlHttpRequest()) { |
64
|
|
|
throw new UserException(gettext('Not logged in')); |
65
|
|
|
} |
66
|
|
|
return new RedirectResponse('#/login'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Route $route |
74
|
|
|
* @param UserVO $user |
75
|
|
|
* @throws MethodNotAllowedException |
76
|
|
|
*/ |
77
|
|
|
protected function checkForRole(Route $route, UserVO $user) |
78
|
|
|
{ |
79
|
|
|
if ($route->hasDefault('_role')) { |
80
|
|
|
$role = $route->getDefault('_role'); |
81
|
|
|
if (!in_array($role, $user->roles)) { |
82
|
|
|
throw new MethodNotAllowedException([], sprintf('Need role %s', $role)); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param int $userId |
89
|
|
|
* @return AnonymusUserVO|UserVO |
90
|
|
|
*/ |
91
|
|
|
private function loadUser(int $userId) : UserVO |
92
|
|
|
{ |
93
|
|
|
if ($userId > 0) { |
94
|
|
|
try { |
95
|
|
|
return $this->loadUser->loadUserById($userId); |
96
|
|
|
} catch (UserNotFoundException $e) { |
97
|
|
|
return new AnonymusUserVO(); |
98
|
|
|
} |
99
|
|
|
} else { |
100
|
|
|
return new AnonymusUserVO(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|