|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SMartins\PassportMultiauth; |
|
4
|
|
|
|
|
5
|
|
|
use Mockery; |
|
6
|
|
|
use Laravel\Passport\Token; |
|
7
|
|
|
use Illuminate\Support\Facades\App; |
|
8
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
|
9
|
|
|
|
|
10
|
|
|
class PassportMultiauth |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Set the current user for the application with the given scopes. |
|
14
|
|
|
* |
|
15
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
|
16
|
|
|
* @param array $scopes |
|
17
|
|
|
* @return void |
|
18
|
|
|
*/ |
|
19
|
7 |
|
public static function actingAs($user, $scopes = []) |
|
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
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the guard of specific provider to `passport` driver. |
|
54
|
|
|
* |
|
55
|
|
|
* @todo Move to class specialized in check auth configs. |
|
56
|
|
|
* @param string $provider |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
8 |
|
public static function getProviderGuard($provider) |
|
60
|
|
|
{ |
|
61
|
8 |
|
foreach (config('auth.guards') as $guard => $content) { |
|
62
|
8 |
|
if ($content['driver'] == 'passport' && $content['provider'] == $provider) { |
|
63
|
8 |
|
return $guard; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get the user guard on provider with `passport` driver. |
|
70
|
|
|
* |
|
71
|
|
|
* @todo Move to class specialized in check auth configs. |
|
72
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
|
73
|
|
|
* @return string|null |
|
74
|
|
|
*/ |
|
75
|
7 |
|
public static function getUserGuard(Authenticatable $user) |
|
76
|
|
|
{ |
|
77
|
7 |
|
$provider = self::getUserProvider($user); |
|
78
|
|
|
|
|
79
|
7 |
|
return self::getProviderGuard($provider); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* If running unit test and try authenticate an user with actingAs($user) |
|
84
|
|
|
* check the guards on request to authenticate or not the user. |
|
85
|
|
|
* |
|
86
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null |
|
87
|
|
|
*/ |
|
88
|
7 |
|
public static function userActing() |
|
89
|
|
|
{ |
|
90
|
7 |
|
if (App::runningUnitTests() && $user = app('auth')->user()) { |
|
91
|
6 |
|
return $user; |
|
92
|
|
|
} |
|
93
|
1 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.