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
|
|
|
* @param string $guard |
|
|
|
|
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
7 |
|
public static function actingAs($user, $scopes = []) |
21
|
|
|
{ |
22
|
7 |
|
$token = Mockery::mock(Token::class)->shouldIgnoreMissing(false); |
23
|
|
|
|
24
|
7 |
|
foreach ($scopes as $scope) { |
25
|
1 |
|
$token->shouldReceive('can')->with($scope)->andReturn(true); |
26
|
|
|
} |
27
|
|
|
|
28
|
7 |
|
$guard = self::getUserGuard($user); |
29
|
|
|
|
30
|
7 |
|
$user->withAccessToken($token); |
|
|
|
|
31
|
|
|
|
32
|
7 |
|
app('auth')->guard($guard)->setUser($user); |
33
|
|
|
|
34
|
7 |
|
app('auth')->shouldUse($guard); |
35
|
7 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the user provider on configs. |
39
|
|
|
* |
40
|
|
|
* @todo Move to class specialized in check auth configs. |
41
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
42
|
|
|
* @return string|null |
43
|
|
|
*/ |
44
|
8 |
|
public static function getUserProvider(Authenticatable $user) |
45
|
|
|
{ |
46
|
8 |
|
foreach (config('auth.providers') as $provider => $config) { |
47
|
8 |
|
if ($user instanceof $config['model']) { |
48
|
8 |
|
return $provider; |
49
|
|
|
} |
50
|
|
|
} |
51
|
1 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the guard of specific provider to `passport` driver. |
55
|
|
|
* |
56
|
|
|
* @todo Move to class specialized in check auth configs. |
57
|
|
|
* @param string $provider |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
8 |
|
public static function getProviderGuard($provider) |
61
|
|
|
{ |
62
|
8 |
|
foreach (config('auth.guards') as $guard => $content) { |
63
|
8 |
|
if ($content['driver'] == 'passport' && $content['provider'] == $provider) { |
64
|
8 |
|
return $guard; |
65
|
|
|
} |
66
|
|
|
} |
67
|
1 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the user guard on provider with `passport` driver. |
71
|
|
|
* |
72
|
|
|
* @todo Move to class specialized in check auth configs. |
73
|
|
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user |
74
|
|
|
* @return string|null |
75
|
|
|
*/ |
76
|
7 |
|
public static function getUserGuard(Authenticatable $user) |
77
|
|
|
{ |
78
|
7 |
|
$provider = self::getUserProvider($user); |
79
|
|
|
|
80
|
7 |
|
return self::getProviderGuard($provider); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* If running unit test and try authenticate an user with actingAs($user) |
85
|
|
|
* check the guards on request to authenticate or not the user. |
86
|
|
|
* |
87
|
|
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null |
88
|
|
|
*/ |
89
|
7 |
|
public static function hasUserActing() |
90
|
|
|
{ |
91
|
7 |
|
if (App::runningUnitTests() && $user = app('auth')->user()) { |
92
|
6 |
|
return $user; |
93
|
|
|
} |
94
|
1 |
|
} |
95
|
|
|
} |
96
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.