1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Middleware; |
4
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Annotations\Inject; |
6
|
|
|
use BrainExe\Core\Annotations\Middleware; |
7
|
|
|
|
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
|
|
|
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
|
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
|
|
|
* @param LoadUser $loadUser |
40
|
|
|
* @param Token $token |
41
|
|
|
*/ |
42
|
4 |
|
public function __construct(LoadUser $loadUser, Token $token) |
43
|
|
|
{ |
44
|
4 |
|
$this->loadUser = $loadUser; |
45
|
4 |
|
$this->token = $token; |
46
|
4 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
3 |
|
public function processRequest(Request $request, Route $route) |
52
|
|
|
{ |
53
|
3 |
|
$token = $request->get('accessToken'); |
54
|
|
|
|
55
|
3 |
|
if (empty($token)) { |
56
|
1 |
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
$userId = $this->token->hasUserForRole($token); |
60
|
2 |
|
if (empty($userId)) { |
61
|
1 |
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$user = $this->loadUser($userId); |
65
|
|
|
|
66
|
1 |
|
$request->attributes->set('user', $user); |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @todo check/implement |
|
|
|
|
71
|
|
|
* @param Route $route |
72
|
|
|
* @param UserVO $user |
73
|
|
|
* @throws MethodNotAllowedException |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
protected function checkForRole(Route $route, UserVO $user) |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
if ($route->hasDefault('_role')) { |
78
|
|
|
$role = $route->getDefault('_role'); |
79
|
|
|
if (!in_array($role, $user->roles)) { |
80
|
|
|
throw new MethodNotAllowedException([], sprintf('Need role %s', $role)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param int $userId |
87
|
|
|
* @return AnonymusUserVO|UserVO |
88
|
|
|
*/ |
89
|
1 |
View Code Duplication |
private function loadUser(int $userId) : UserVO |
|
|
|
|
90
|
|
|
{ |
91
|
1 |
|
if ($userId > 0) { |
92
|
|
|
try { |
93
|
1 |
|
return $this->loadUser->loadUserById($userId); |
94
|
|
|
} catch (UserNotFoundException $e) { |
95
|
|
|
return new AnonymusUserVO(); |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
return new AnonymusUserVO(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.