1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMartins\PassportMultiauth\Config; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
6
|
|
|
use SMartins\PassportMultiauth\Exceptions\MissingConfigException; |
7
|
|
|
|
8
|
|
|
class AuthConfigHelper |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Get the user provider on configs. |
12
|
|
|
* |
13
|
|
|
* @param Authenticatable $user |
14
|
|
|
* @return string|null |
15
|
|
|
* @throws MissingConfigException |
16
|
|
|
*/ |
17
|
3 |
|
public static function getUserProvider(Authenticatable $user) |
18
|
|
|
{ |
19
|
3 |
|
foreach (config('auth.providers') as $provider => $config) { |
20
|
3 |
|
if ($user instanceof $config['model']) { |
21
|
2 |
|
return $provider; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
1 |
|
throw MissingConfigException::provider($user); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the guard of specific provider to `passport` driver. |
30
|
|
|
* |
31
|
|
|
* @param string $provider |
32
|
|
|
* @return string |
33
|
|
|
* @throws MissingConfigException |
34
|
|
|
*/ |
35
|
4 |
|
public static function getProviderGuard($provider) |
36
|
|
|
{ |
37
|
4 |
|
foreach (config('auth.guards') as $guard => $content) { |
38
|
4 |
|
if ($content['driver'] == 'passport' && $content['provider'] == $provider) { |
39
|
3 |
|
return $guard; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
throw MissingConfigException::providerGuard($provider); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the user guard on provider with `passport` driver. |
48
|
|
|
* |
49
|
|
|
* @param Authenticatable $user |
50
|
|
|
* @return string|null |
51
|
|
|
* @throws MissingConfigException |
52
|
|
|
*/ |
53
|
2 |
|
public static function getUserGuard(Authenticatable $user) |
54
|
|
|
{ |
55
|
2 |
|
$provider = self::getUserProvider($user); |
56
|
|
|
|
57
|
2 |
|
return self::getProviderGuard($provider); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $provider |
62
|
|
|
* @return null|Illuminate\Database\Eloquent\Model |
|
|
|
|
63
|
|
|
*/ |
64
|
|
|
public static function getProviderModel($provider) |
65
|
|
|
{ |
66
|
|
|
$model = config('auth.providers.'.$provider.'.model', null); |
67
|
|
|
|
68
|
|
|
return $model ? $model : null; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|