|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maklad\Permission; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use function collect; |
|
7
|
|
|
use function config; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Helpers |
|
11
|
|
|
* @package Maklad\Permission |
|
12
|
|
|
*/ |
|
13
|
|
|
class Helpers |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param string $guard |
|
17
|
|
|
* |
|
18
|
4 |
|
* @return string|null |
|
19
|
|
|
*/ |
|
20
|
4 |
|
public function getModelForGuard(string $guard): ?string |
|
21
|
4 |
|
{ |
|
22
|
4 |
|
return collect(config('auth.guards')) |
|
23
|
4 |
|
->map(function ($guard) { |
|
24
|
|
|
return config("auth.providers.{$guard['provider']}.model"); |
|
25
|
|
|
})->get($guard); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Collection $expected |
|
30
|
|
|
* @param string $given |
|
31
|
|
|
* |
|
32
|
6 |
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
6 |
|
public function getGuardDoesNotMatchMessage(Collection $expected, string $given): string |
|
35
|
|
|
{ |
|
36
|
|
|
$expectedStr = $expected->implode(', '); |
|
37
|
|
|
return "The given role or permission should use guard `$expectedStr` instead of `$given`."; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $name |
|
42
|
|
|
* @param string $guardName |
|
43
|
1 |
|
* |
|
44
|
|
|
* @return string |
|
45
|
1 |
|
*/ |
|
46
|
|
|
public function getPermissionAlreadyExistsMessage(string $name, string $guardName): string |
|
47
|
|
|
{ |
|
48
|
|
|
return "A permission `$name` already exists for guard `$guardName`."; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @param string $guardName |
|
54
|
7 |
|
* |
|
55
|
|
|
* @return string |
|
56
|
7 |
|
*/ |
|
57
|
|
|
public function getPermissionDoesNotExistMessage(string $name, string $guardName): string |
|
58
|
|
|
{ |
|
59
|
|
|
return "There is no permission named `$name` for guard `$guardName`."; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $name |
|
64
|
|
|
* @param string $guardName |
|
65
|
1 |
|
* |
|
66
|
|
|
* @return string |
|
67
|
1 |
|
*/ |
|
68
|
|
|
public function getRoleAlreadyExistsMessage(string $name, string $guardName): string |
|
69
|
|
|
{ |
|
70
|
|
|
return "A role `$name` already exists for guard `$guardName`."; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $name |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $guardName |
|
77
|
2 |
|
* |
|
78
|
|
|
* @return string |
|
79
|
2 |
|
*/ |
|
80
|
|
|
public function getRoleDoesNotExistMessage(string $name, string $guardName): string |
|
81
|
|
|
{ |
|
82
|
|
|
return "There is no role named `$name` for guard `$guardName`."; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param string $roles |
|
87
|
4 |
|
* |
|
88
|
|
|
* @return string |
|
89
|
4 |
|
*/ |
|
90
|
4 |
|
public function getUnauthorizedRoleMessage(string $roles): string |
|
91
|
4 |
|
{ |
|
92
|
|
|
$message = "User does not have the right roles `$roles`."; |
|
93
|
|
|
if (! config('permission.display_permission_in_exception')) { |
|
94
|
4 |
|
$message = 'User does not have the right roles.'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $message; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param string $permissions |
|
102
|
3 |
|
* |
|
103
|
|
|
* @return string |
|
104
|
3 |
|
*/ |
|
105
|
3 |
|
public function getUnauthorizedPermissionMessage(string $permissions): string |
|
106
|
3 |
|
{ |
|
107
|
|
|
$message = "User does not have the right permissions `$permissions`."; |
|
108
|
|
|
if (! config('permission.display_permission_in_exception')) { |
|
109
|
3 |
|
$message = 'User does not have the right permissions.'; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $message; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
/** |
|
116
|
|
|
* @return string |
|
117
|
2 |
|
*/ |
|
118
|
|
|
public function getUserNotLoggedINMessage(): string |
|
119
|
|
|
{ |
|
120
|
123 |
|
return 'User is not logged in.'; |
|
121
|
|
|
} |
|
122
|
123 |
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
public function isNotLumen(): bool |
|
127
|
|
|
{ |
|
128
|
|
|
return ! (stripos(app()->version(), 'lumen') !== false); |
|
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return bool |
|
133
|
|
|
*/ |
|
134
|
|
|
public function checkVersion(): bool |
|
135
|
|
|
{ |
|
136
|
|
|
return ($this->isNotLumen() && app()::VERSION < '5.4'); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param array $items |
|
141
|
|
|
* @return array |
|
142
|
|
|
*/ |
|
143
|
|
|
public function flattenArray(array $items): array |
|
144
|
|
|
{ |
|
145
|
|
|
return collect($items)->map(function ($item) { |
|
|
|
|
|
|
146
|
|
|
return is_string($item) ? explode('|', $item): $item; |
|
147
|
|
|
})->flatten()->all(); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|