1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\Authentication\Controller; |
4
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Annotations\Inject; |
6
|
|
|
use BrainExe\Core\Annotations\Controller; |
7
|
|
|
use BrainExe\Core\Annotations\Route; |
8
|
|
|
use BrainExe\Core\Authentication\Token; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @Controller("Authentication.Controller.TokenController") |
13
|
|
|
*/ |
14
|
|
|
class TokenController |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Token |
19
|
|
|
*/ |
20
|
|
|
private $token; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @Inject("@Core.Authentication.Token") |
24
|
|
|
* @param Token $token |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Token $token) |
27
|
|
|
{ |
28
|
|
|
$this->token = $token; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Request $request |
33
|
|
|
* @Route("/user/tokens/", name="authenticate.tokens.get", methods="GET") |
34
|
|
|
* @return array |
35
|
|
|
*/ |
36
|
|
|
public function getTokens(Request $request) : array |
37
|
|
|
{ |
38
|
|
|
$userId = $request->attributes->getInt('user_id'); |
39
|
|
|
|
40
|
|
|
return iterator_to_array($this->token->getTokensForUser($userId)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Request $request |
45
|
|
|
* @Route("/user/tokens/", name="authenticate.tokens.new", methods="POST") |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function addToken(Request $request) : string |
49
|
|
|
{ |
50
|
|
|
$userId = $request->attributes->getInt('user_id'); |
51
|
|
|
$roles = (array)$request->request->get('roles'); |
52
|
|
|
|
53
|
|
|
return $this->token->addToken($userId, $roles); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Request $request |
58
|
|
|
* @Route("/user/tokens/{token}/", name="authenticate.tokens.revoke", methods="DELETE") |
59
|
|
|
* @param string $token |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function revoke(Request $request, string $token) : bool |
63
|
|
|
{ |
64
|
|
|
unset($request); |
65
|
|
|
|
66
|
|
|
$this->token->revoke($token); |
67
|
|
|
|
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|