1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the core-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\CoreBundle\Exception; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
15
|
|
|
use WBW\Bundle\CoreBundle\Model\Attribute\StringOriginUrlTrait; |
16
|
|
|
use WBW\Bundle\CoreBundle\Model\Attribute\StringRedirectUrlTrait; |
17
|
|
|
use WBW\Bundle\CoreBundle\Model\UserTrait; |
18
|
|
|
use WBW\Library\Core\Network\HTTP\HTTPInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Bad user role exception. |
22
|
|
|
* |
23
|
|
|
* @author webeweb <https://github.com/webeweb/> |
24
|
|
|
* @package WBW\Bundle\CoreBundle\Exception |
25
|
|
|
*/ |
26
|
|
|
class BadUserRoleException extends AbstractException { |
27
|
|
|
|
28
|
|
|
use StringOriginUrlTrait; |
29
|
|
|
use StringRedirectUrlTrait; |
30
|
|
|
use UserTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Roles. |
34
|
|
|
* |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $roles; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* |
42
|
|
|
* @param UserInterface $user The user. |
43
|
|
|
* @param array $roles The roles. |
44
|
|
|
* @param string $redirectUrl The redirect. |
45
|
|
|
* @param string $originUrl The route. |
46
|
|
|
*/ |
47
|
|
|
public function __construct(UserInterface $user, array $roles, $redirectUrl, $originUrl) { |
48
|
|
|
$format = "User \"%s\" is not allowed to access to \"%s\" with roles [%s]"; |
49
|
|
|
parent::__construct(sprintf($format, $user->getUsername(), $originUrl, implode(",", $roles)), HTTPInterface::HTTP_STATUS_FORBIDDEN); |
50
|
|
|
$this->setOriginUrl($originUrl); |
51
|
|
|
$this->setRedirectUrl($redirectUrl); |
52
|
|
|
$this->setRoles($roles); |
53
|
|
|
$this->setUser($user); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the roles. |
58
|
|
|
* |
59
|
|
|
* @return array Returns the roles. |
60
|
|
|
*/ |
61
|
|
|
public function getRoles() { |
62
|
|
|
return $this->roles; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the roles; |
67
|
|
|
* |
68
|
|
|
* @param array $roles The roles. |
69
|
|
|
* @return BadUserRoleException Returns this bad user role exception. |
70
|
|
|
*/ |
71
|
|
|
protected function setRoles(array $roles) { |
72
|
|
|
$this->roles = $roles; |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|