8000 [Security] Expose the required roles in AccessDeniedException · symfony/symfony@9001f9e · GitHub
[go: up one dir, main page]

Skip to content

Commit 9001f9e

Browse files
committed
[Security] Expose the required roles in AccessDeniedException
1 parent f146f84 commit 9001f9e

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

src/Symfony/Component/Security/Core/Exception/AccessDeniedException.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,43 @@
1818
*/
1919
class AccessDeniedException extends \RuntimeException
2020
{
21+
private $attributes = [];
22+
private $object;
23+
2124
public function __construct($message = 'Access Denied.', \Exception $previous = null)
2225
{
2326
parent::__construct($message, 403, $previous);
2427
}
28+
29+
/**
30+
* @return array
31+
*/
32+
public function getAttributes()
33+
{
34+
return $this->attributes;
35+
}
36+
37+
/**
38+
* @param array $attributes
39+
*/
40+
public function setAttributes(array $attributes)
41+
{
42+
$this->attributes = $attributes;
43+
}
44+
45+
/**
46+
* @return mixed
47+
*/
48+
public function getObject()
49+
{
50+
return $this->object;
51+
}
52+
53+
/**
54+
* @param mixed $object
55+
*/
56+
public function setObject($object)
57+
{
58+
$this->object = $object;
59+
}
2560
}

src/Symfony/Component/Security/Http/Firewall/AccessListener.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ public function handle(GetResponseEvent $event)
6767
}
6868

6969
if (!$this->accessDecisionManager->decide($token, $attributes, $request)) {
70-
throw new AccessDeniedException();
70+
$exception = new AccessDeniedException();
71+
$exception->setAttributes($attributes);
72+
$exception->setObject($request);
73+
74+
throw $exception;
7175
}
7276
}
7377
}

src/Symfony/Component/Security/Http/Firewall/SwitchUserListener.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ private function attemptSwitchUser(Request $request)
122122
}
123123

124124
if (false === $this->accessDecisionManager->decide($token, array($this->role))) {
125-
throw new AccessDeniedException();
125+
$exception = new AccessDeniedException();
126+
$exception->setAttributes(array($this->role));
127+
128+
throw $exception;
126129
}
127130

128131
$username = $request->get($this->usernameParameter);

0 commit comments

Comments
 (0)
0