1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gameap\Http\Controllers\API; |
4
|
|
|
|
5
|
|
|
use Gameap\Http\Controllers\AuthController; |
|
|
|
|
6
|
|
|
use Gameap\Http\Requests\API\GeneratePersonalAccessTokenRequest; |
7
|
|
|
use Gameap\Models\User; |
8
|
|
|
use Gameap\Services\PersonalAccessTokenService; |
9
|
|
|
use Illuminate\Contracts\Auth\Factory as AuthFactory; |
10
|
|
|
use Illuminate\Support\Facades\Auth; |
11
|
|
|
|
12
|
|
|
class TokensController extends AuthController |
13
|
|
|
{ |
14
|
|
|
/** @var PersonalAccessTokenService */ |
15
|
|
|
private $personalAccessTokenService; |
16
|
|
|
|
17
|
|
|
/** @var AuthFactory */ |
18
|
|
|
protected $authFactory; |
19
|
|
|
|
20
|
|
|
public function __construct( |
21
|
|
|
PersonalAccessTokenService $personalAccessTokenService, |
22
|
|
|
AuthFactory $auth |
23
|
|
|
) |
24
|
|
|
{ |
25
|
|
|
parent::__construct(); |
26
|
|
|
|
27
|
|
|
$this->personalAccessTokenService = $personalAccessTokenService; |
28
|
|
|
$this->authFactory = $auth; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function list() |
32
|
|
|
{ |
33
|
|
|
/** @var User $currentUser */ |
34
|
|
|
$currentUser = $this->authFactory->guard()->user(); |
35
|
|
|
|
36
|
|
|
return response()->json(collect($currentUser->tokens)->map(function ($token) { |
37
|
|
|
return [ |
38
|
|
|
'id' => $token->id, |
39
|
|
|
'name' => $token->name, |
40
|
|
|
'abilities' => $token->abilities, |
41
|
|
|
'last_used_at' => $token->last_used_at, |
42
|
|
|
'created_at' => $token->created_at, |
43
|
|
|
]; |
44
|
|
|
})); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function abilities() |
48
|
|
|
{ |
49
|
|
|
/** @var User $currentUser */ |
50
|
|
|
$currentUser = $this->authFactory->guard()->user(); |
51
|
|
|
$tokenAbilities = $this->personalAccessTokenService->getGrouppedAbilitiesDescriptions($currentUser); |
52
|
|
|
|
53
|
|
|
return response()->json($tokenAbilities); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function store(GeneratePersonalAccessTokenRequest $request) |
57
|
|
|
{ |
58
|
|
|
/** @var User $currentUser */ |
59
|
|
|
$currentUser = $this->authFactory->guard()->user(); |
60
|
|
|
$token = $currentUser->createToken($request->token_name, $request->abilities); |
61
|
|
|
|
62
|
|
|
return response()->json([ |
63
|
|
|
'token' => $token->plainTextToken, |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function destroy($tokenId) |
68
|
|
|
{ |
69
|
|
|
/** @var User $currentUser */ |
70
|
|
|
$currentUser = $this->authFactory->guard()->user(); |
71
|
|
|
$currentUser->tokens()->where('id', $tokenId)->delete(); |
72
|
|
|
|
73
|
|
|
return response()->json([ |
74
|
|
|
'message' => 'Token deleted', |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
} |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: