1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
class Guard |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Return a collection of guard names suitable for the $model. |
12
|
|
|
* |
13
|
|
|
* @param string|Model $model model class object or name |
14
|
|
|
* @return Collection |
15
|
|
|
*/ |
16
|
|
|
public static function getNames($model): Collection |
17
|
|
|
{ |
18
|
|
|
if (is_object($model)) { |
19
|
|
|
if (\method_exists($model, 'guardName')) { |
20
|
|
|
$guardName = $model->guardName(); |
21
|
|
|
} else { |
22
|
|
|
$guardName = $model->guard_name ?? null; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (! isset($guardName)) { |
27
|
|
|
$class = is_object($model) ? get_class($model) : $model; |
28
|
|
|
|
29
|
|
|
$guardName = (new \ReflectionClass($class))->getDefaultProperties()['guard_name'] ?? null; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($guardName) { |
33
|
|
|
return collect($guardName); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return self::getConfigAuthGuards($class); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get list of relevant guards for the $class model based on config(auth) settings. |
41
|
|
|
* |
42
|
|
|
* Lookup flow: |
43
|
|
|
* - get names of models for guards defined in auth.guards where a provider is set |
44
|
|
|
* - filter for provider models matching the model $class being checked |
45
|
|
|
* - keys() gives just the names of the matched guards |
46
|
|
|
* - if logged in, filter out any guards for which the user does not pass the auth->guard->check() test |
47
|
|
|
* - return collection of guard names |
48
|
|
|
* |
49
|
|
|
* @param string $class |
50
|
|
|
* @return Collection |
51
|
|
|
*/ |
52
|
|
|
protected static function getConfigAuthGuards(string $class): Collection |
53
|
|
|
{ |
54
|
|
|
return collect(config('auth.guards')) |
55
|
|
|
->map(function ($guard) { |
56
|
|
|
if (! isset($guard['provider'])) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return config("auth.providers.{$guard['provider']}.model"); |
61
|
|
|
}) |
62
|
|
|
->filter(function ($model) use ($class) { |
63
|
|
|
return $class === $model; |
64
|
|
|
}) |
65
|
|
|
->keys() |
66
|
|
|
->filter(function ($guard) { |
67
|
|
|
return app('request')->user() ? app('auth')->guard($guard)->check() : true; |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Lookup a guard name relevant for the $class model and the current user. |
73
|
|
|
* |
74
|
|
|
* @param string|Model $class model class object or name |
75
|
|
|
* @return string guard name |
76
|
|
|
*/ |
77
|
|
|
public static function getDefaultName($class): string |
78
|
|
|
{ |
79
|
|
|
$default = config('auth.defaults.guard'); |
80
|
|
|
|
81
|
|
|
return static::getNames($class)->first() ?: $default; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: