1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Permission\Exceptions; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
6
|
|
|
|
7
|
|
|
class UnauthorizedException extends HttpException |
8
|
|
|
{ |
9
|
|
|
private $requiredRoles = []; |
10
|
|
|
|
11
|
|
|
private $requiredPermissions = []; |
12
|
|
|
|
13
|
|
View Code Duplication |
public static function forRoles(array $roles): self |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$message = 'User does not have the right roles.'; |
16
|
|
|
|
17
|
|
|
if (config('permission.display_permission_in_exception')) { |
18
|
|
|
$permStr = implode(', ', $roles); |
19
|
|
|
$message = 'User does not have the right roles. Necessary roles are '.$permStr; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$exception = new static(403, $message, null, []); |
23
|
|
|
$exception->requiredRoles = $roles; |
24
|
|
|
|
25
|
|
|
return $exception; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
public static function forPermissions(array $permissions): self |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$message = 'User does not have the right permissions.'; |
31
|
|
|
|
32
|
|
|
if (config('permission.display_permission_in_exception')) { |
33
|
|
|
$permStr = implode(', ', $permissions); |
34
|
|
|
$message = 'User does not have the right permissions. Necessary permissions are '.$permStr; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$exception = new static(403, $message, null, []); |
38
|
|
|
$exception->requiredPermissions = $permissions; |
39
|
|
|
|
40
|
|
|
return $exception; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public static function forRolesOrPermissions(array $rolesOrPermissions): self |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$message = 'User does not have any of the necessary access rights.'; |
46
|
|
|
|
47
|
|
|
if (config('permission.display_permission_in_exception') && config('permission.display_role_in_exception')) { |
48
|
|
|
$permStr = implode(', ', $rolesOrPermissions); |
49
|
|
|
$message = 'User does not have the right permissions. Necessary permissions are '.$permStr; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$exception = new static(403, $message, null, []); |
53
|
|
|
$exception->requiredPermissions = $rolesOrPermissions; |
54
|
|
|
|
55
|
|
|
return $exception; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function notLoggedIn(): self |
59
|
|
|
{ |
60
|
|
|
return new static(403, 'User is not logged in.', null, []); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getRequiredRoles(): array |
64
|
|
|
{ |
65
|
|
|
return $this->requiredRoles; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getRequiredPermissions(): array |
69
|
|
|
{ |
70
|
|
|
return $this->requiredPermissions; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.