1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Middleware; |
4
|
|
|
|
5
|
|
|
use BrainExe\Core\Annotations\Middleware; |
6
|
|
|
use BrainExe\Core\Authentication\AnonymusUserVO; |
7
|
|
|
use BrainExe\Core\Authentication\Exception\UserNotFoundException; |
8
|
|
|
use BrainExe\Core\Authentication\LoadUser; |
9
|
|
|
use BrainExe\Core\Authentication\Token; |
10
|
|
|
use BrainExe\Core\Authentication\UserVO; |
11
|
|
|
use BrainExe\Core\Translation\TranslationTrait; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException; |
14
|
|
|
use Symfony\Component\Routing\Route; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @Middleware("Middleware.TokenAuthentication") |
18
|
|
|
*/ |
19
|
|
|
class TokenAuthentication extends AbstractMiddleware |
20
|
|
|
{ |
21
|
|
|
use TranslationTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var LoadUser |
25
|
|
|
*/ |
26
|
|
|
private $loadUser; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Token |
30
|
|
|
*/ |
31
|
|
|
private $token; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param LoadUser $loadUser |
35
|
|
|
* @param Token $token |
36
|
|
|
*/ |
37
|
3 |
|
public function __construct(LoadUser $loadUser, Token $token) |
38
|
|
|
{ |
39
|
3 |
|
$this->loadUser = $loadUser; |
40
|
3 |
|
$this->token = $token; |
41
|
3 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
3 |
|
public function processRequest(Request $request, Route $route) |
47
|
|
|
{ |
48
|
3 |
|
$token = $request->get('accessToken'); |
49
|
|
|
|
50
|
3 |
|
if (null === $token) { |
51
|
1 |
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
$userId = $this->token->hasUserForRole($token); |
55
|
2 |
|
if (null === $userId) { |
56
|
1 |
|
return null; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$user = $this->loadUser($userId); |
60
|
|
|
|
61
|
1 |
|
$request->attributes->set('user', $user); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @todo check/implement |
|
|
|
|
66
|
|
|
* @param Route $route |
67
|
|
|
* @param UserVO $user |
68
|
|
|
* @throws MethodNotAllowedException |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
protected function checkForRole(Route $route, UserVO $user) |
71
|
|
|
{ |
72
|
|
|
if ($route->hasDefault('_role')) { |
73
|
|
|
$role = $route->getDefault('_role'); |
74
|
|
|
if (!in_array($role, $user->roles, true)) { |
75
|
|
|
throw new MethodNotAllowedException([], sprintf('Need role %s', $role)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param int $userId |
82
|
|
|
* @return AnonymusUserVO|UserVO |
83
|
|
|
*/ |
84
|
1 |
View Code Duplication |
private function loadUser(int $userId) : UserVO |
85
|
|
|
{ |
86
|
1 |
|
if ($userId > 0) { |
87
|
|
|
try { |
88
|
1 |
|
return $this->loadUser->loadUserById($userId); |
89
|
|
|
} catch (UserNotFoundException $e) { |
90
|
|
|
return new AnonymusUserVO(); |
91
|
|
|
} |
92
|
|
|
} else { |
93
|
|
|
return new AnonymusUserVO(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.