1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMartins\PassportMultiauth; |
4
|
|
|
|
5
|
|
|
use Mockery; |
6
|
|
|
use Laravel\Passport\Token; |
7
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
8
|
|
|
|
9
|
|
|
class PassportMultiauth |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Set the current user for the application with the given scopes. |
13
|
|
|
* |
14
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
15
|
|
|
* @param array $scopes |
16
|
|
|
* @param string $guard |
17
|
|
|
* @return void |
18
|
|
|
*/ |
19
|
7 |
|
public static function actingAs($user, $scopes = [], $guard = 'api') |
|
|
|
|
20
|
|
|
{ |
21
|
7 |
|
$token = Mockery::mock(Token::class)->shouldIgnoreMissing(false); |
22
|
|
|
|
23
|
7 |
|
foreach ($scopes as $scope) { |
24
|
1 |
|
$token->shouldReceive('can')->with($scope)->andReturn(true); |
25
|
|
|
} |
26
|
|
|
|
27
|
7 |
|
$guard = self::getUserGuard($user); |
28
|
|
|
|
29
|
7 |
|
$user->withAccessToken($token); |
|
|
|
|
30
|
|
|
|
31
|
7 |
|
app('auth')->guard($guard)->setUser($user); |
32
|
|
|
|
33
|
7 |
|
app('auth')->shouldUse($guard); |
34
|
7 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the user provider on configs. |
38
|
|
|
* |
39
|
|
|
* @todo Move to class specialized in check auth configs. |
40
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
41
|
|
|
* @return string|null |
42
|
|
|
*/ |
43
|
8 |
|
public static function getUserProvider(Authenticatable $user) |
44
|
|
|
{ |
45
|
8 |
|
foreach (config('auth.providers') as $provider => $config) { |
46
|
8 |
|
if ($user instanceof $config['model']) { |
47
|
8 |
|
return $provider; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the guard of specific provider to `passport` driver. |
56
|
|
|
* |
57
|
|
|
* @todo Move to class specialized in check auth configs. |
58
|
|
|
* @param string $provider |
59
|
|
|
* @return string|null |
60
|
|
|
*/ |
61
|
8 |
|
public static function getProviderGuard($provider) |
62
|
|
|
{ |
63
|
8 |
|
foreach (config('auth.guards') as $guard => $content) { |
64
|
8 |
|
if ($content['driver'] == 'passport' && $content['provider'] == $provider) { |
65
|
8 |
|
return $guard; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the user guard on provider with `passport` driver; |
74
|
|
|
* |
75
|
|
|
* @todo Move to class specialized in check auth configs. |
76
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
77
|
|
|
* @return string|null |
78
|
|
|
*/ |
79
|
7 |
|
public static function getUserGuard(Authenticatable $user) |
80
|
|
|
{ |
81
|
7 |
|
$provider = self::getUserProvider($user); |
82
|
|
|
|
83
|
7 |
|
return self::getProviderGuard($provider); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.