1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the bootstrap-bundle package. |
5
|
|
|
* |
6
|
|
|
* (c) 2018 WEBEWEB |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WBW\Bundle\BootstrapBundle\Exception; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
15
|
|
|
use WBW\Library\Core\IO\HTTPInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Bad user role exception. |
19
|
|
|
* |
20
|
|
|
* @author webeweb <https://github.com/webeweb/> |
21
|
|
|
* @package WBW\Bundle\BootstrapBundle\Exception |
22
|
|
|
*/ |
23
|
|
|
class BadUserRoleException extends AbstractBootstrapException { |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Redirect. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $redirect; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Roles. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $roles; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Route. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $route; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* User. |
48
|
|
|
* |
49
|
|
|
* @var UserInterface |
50
|
|
|
*/ |
51
|
|
|
private $user; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Constructor. |
55
|
|
|
* |
56
|
|
|
* @param UserInterface $user The user. |
57
|
|
|
* @param array $roles The roles. |
58
|
|
|
* @param string $route The route. |
59
|
|
|
* @param string $redirect The redirect. |
60
|
|
|
*/ |
61
|
|
|
public function __construct(UserInterface $user, array $roles, $route, $redirect) { |
62
|
|
|
parent::__construct(sprintf("User \"%s\" is not allowed to access to \"%s\" with roles [%s]", $user->getUsername(), $route, implode(",", $roles)), HTTPInterface::HTTP_STATUS_FORBIDDEN); |
63
|
|
|
$this->setRedirect($redirect); |
64
|
|
|
$this->setRoles($roles); |
65
|
|
|
$this->setRoute($route); |
66
|
|
|
$this->setUser($user); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the redirect. |
71
|
|
|
* |
72
|
|
|
* @return string Returns the redirect. |
73
|
|
|
*/ |
74
|
|
|
public function getRedirect() { |
75
|
|
|
return $this->redirect; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the roles. |
80
|
|
|
* |
81
|
|
|
* @return array Returns the roles. |
82
|
|
|
*/ |
83
|
|
|
public function getRoles() { |
84
|
|
|
return $this->roles; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the route. |
89
|
|
|
* |
90
|
|
|
* @return string Returns the route. |
91
|
|
|
*/ |
92
|
|
|
public function getRoute() { |
93
|
|
|
return $this->route; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the user. |
98
|
|
|
* |
99
|
|
|
* @return UserInterface Returns the user. |
100
|
|
|
*/ |
101
|
|
|
public function getUser() { |
102
|
|
|
return $this->user; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the redirect. |
107
|
|
|
* |
108
|
|
|
* @param string $redirect The redirect. |
109
|
|
|
* @return BadUserRoleException Returns this bad user role exception. |
110
|
|
|
*/ |
111
|
|
|
protected function setRedirect($redirect) { |
112
|
|
|
$this->redirect = $redirect; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Set the roles; |
118
|
|
|
* |
119
|
|
|
* @param array $roles The roles. |
120
|
|
|
* @return BadUserRoleException Returns this bad user role exception. |
121
|
|
|
*/ |
122
|
|
|
public function setRoles(array $roles) { |
123
|
|
|
$this->roles = $roles; |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Set the route. |
129
|
|
|
* |
130
|
|
|
* @param string $route The route. |
131
|
|
|
* @return BadUserRoleException Returns this bad user role exception. |
132
|
|
|
*/ |
133
|
|
|
protected function setRoute($route) { |
134
|
|
|
$this->route = $route; |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Set the user. |
140
|
|
|
* |
141
|
|
|
* @param UserInterface $user The user. |
142
|
|
|
* @return BadUserRoleException Returns this bad user role exception. |
143
|
|
|
*/ |
144
|
|
|
protected function setUser(UserInterface $user) { |
145
|
|
|
$this->user = $user; |
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|