|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SMartins\PassportMultiauth; |
|
4
|
|
|
|
|
5
|
|
|
use Laravel\Passport\Token; |
|
6
|
|
|
use Laravel\Passport\PersonalAccessTokenFactory; |
|
7
|
|
|
use Illuminate\Container\Container; |
|
8
|
|
|
use SMartins\PassportMultiauth\Config\AuthConfigHelper; |
|
9
|
|
|
|
|
10
|
|
|
trait HasApiTokens |
|
11
|
|
|
{ |
|
12
|
|
|
use \Laravel\Passport\HasApiTokens; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Get all of the access tokens for the user relating with Provider. |
|
16
|
|
|
* |
|
17
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
18
|
|
|
*/ |
|
19
|
|
|
public function tokens() |
|
20
|
|
|
{ |
|
21
|
|
|
return $this->hasMany(Token::class, 'user_id') |
|
|
|
|
|
|
22
|
|
|
->join('oauth_access_token_providers', function ($join) { |
|
23
|
|
|
$join->on( |
|
24
|
|
|
'oauth_access_tokens.id', '=', 'oauth_access_token_id' |
|
25
|
|
|
)->where('oauth_access_token_providers.provider', '=', AuthConfigHelper::getUserProvider($this)); |
|
26
|
|
|
})->orderBy('created_at', 'desc') |
|
27
|
|
|
->select('oauth_access_tokens.*') |
|
28
|
|
|
->get(); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Create a new personal access token for the user and create . |
|
33
|
|
|
* |
|
34
|
|
|
* @param string $name |
|
35
|
|
|
* @param array $scopes |
|
36
|
|
|
* @return \Laravel\Passport\PersonalAccessTokenResult |
|
37
|
|
|
*/ |
|
38
|
|
|
public function createToken($name, array $scopes = []) |
|
39
|
|
|
{ |
|
40
|
|
|
// Backup default provider |
|
41
|
|
|
$defaultProvider = config('auth.guards.api.provider'); |
|
42
|
|
|
|
|
43
|
|
|
$userProvider = AuthConfigHelper::getUserProvider($this); |
|
44
|
|
|
|
|
45
|
|
|
// Change config to when the token is created set the provider from model creating the token. |
|
46
|
|
|
config(['auth.guards.api.provider' => $userProvider]); |
|
47
|
|
|
|
|
48
|
|
|
$token = Container::getInstance()->make(PersonalAccessTokenFactory::class)->make( |
|
49
|
|
|
$this->getKey(), $name, $scopes |
|
|
|
|
|
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
// Reset config to defaults |
|
53
|
|
|
config(['auth.guards.api.provider' => $defaultProvider]); |
|
54
|
|
|
|
|
55
|
|
|
return $token; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
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.