|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Rinvex\OAuth\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany; |
|
8
|
|
|
use Rinvex\OAuth\Factories\PersonalAccessTokenFactory; |
|
9
|
|
|
|
|
10
|
|
|
trait HasApiTokens |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The current access token for the authentication user. |
|
14
|
|
|
* |
|
15
|
|
|
* @var \Rinvex\OAuth\Models\AccessToken |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $accessToken; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Get all of the user's registered OAuth clients. |
|
21
|
|
|
* |
|
22
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
23
|
|
|
*/ |
|
24
|
|
|
public function clients(): MorphMany |
|
25
|
|
|
{ |
|
26
|
|
|
return $this->morphMany(config('rinvex.oauth.models.client'), 'user', 'user_type', 'user_id'); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get all of the access tokens for the user. |
|
31
|
|
|
* |
|
32
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphMany |
|
33
|
|
|
*/ |
|
34
|
|
|
public function tokens(): MorphMany |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->morphMany(config('rinvex.oauth.models.client'), 'user', 'user_type', 'user_id')->orderBy('created_at', 'desc'); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get the current access token being used by the user. |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Rinvex\OAuth\Models\AccessToken|null |
|
43
|
|
|
*/ |
|
44
|
|
|
public function token() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->accessToken; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Create a new personal access token for the user. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @param array $scopes |
|
54
|
|
|
* |
|
55
|
|
|
* @throws \Illuminate\Contracts\Container\BindingResolutionException |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Rinvex\OAuth\PersonalAccessTokenResult |
|
58
|
|
|
*/ |
|
59
|
|
|
public function createToken($name, array $scopes = []) |
|
60
|
|
|
{ |
|
61
|
|
|
return app(PersonalAccessTokenFactory::class)->make($this, $name, $scopes); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Set the current access token for the user. |
|
66
|
|
|
* |
|
67
|
|
|
* @param \Rinvex\OAuth\Models\AccessToken $accessToken |
|
68
|
|
|
* |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
|
|
public function withAccessToken($accessToken) |
|
72
|
|
|
{ |
|
73
|
|
|
$this->accessToken = $accessToken; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.