8000 [Security] Expose the required roles in AccessDeniedException by Nicofuma · Pull Request #19473 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,11 @@ protected function isGranted($attributes, $object = null)
protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
{
if (!$this->isGranted($attributes, $object)) {
throw $this->createAccessDeniedException($message);
$exception = $this->createAccessDeniedException($message);
$exception->setAttributes($attributes);
$exception->setObject($object);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The object doesn't have to be an object as far as I know. Since a recent version scalars are also supported I believe. Not sure if setObject is still correct in that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

object is the name already used b 10BC0 y the security component

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The VoterInterface use subjet instead of object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I would call it Subject


throw $exception;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"symfony/filesystem": "~2.8|~3.0",
"symfony/finder": "~2.8|~3.0",
"symfony/routing": "~3.0",
"symfony/security-core": "~2.8|~3.0",
"symfony/security-core": "~3.2",
"symfony/security-csrf": "~2.8|~3.0",
"symfony/stopwatch": "~2.8|~3.0",
"symfony/templating": "~2.8|~3.0",
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Security/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.2.0
-----

* added `attributes` and `object` with getters/setters to `Symfony\Component\Security\Core\Exception\AccessDeniedException`

3.0.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,43 @@
*/
class AccessDeniedException extends \RuntimeException
{
private $attributes = array();
private $object;

public function __construct($message = 'Access Denied.', \Exception $previous = null)
{
parent::__construct($message, 403, $previous);
}

/**
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* @param array|string $attributes
*/
public function setAttributes($attributes)
{
$this->attributes = (array) $attributes;
}

/**
* @return mixed
*/
public function getObject()
{
return $this->object;
}

/**
* @param mixed $object
*/
public function setObject($object)
{
$this->object = $object;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ public function handle(GetResponseEvent $event)
}

if (!$this->accessDecisionManager->decide($token, $attributes, $request)) {
throw new AccessDeniedException();
$exception = new AccessDeniedException();
$exception->setAttributes($attributes);
$exception->setObject($request);

throw $exception;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ private function attemptSwitchUser(Request $request)
}

if (false === $this->accessDecisionManager->decide($token, array($this->role))) {
throw new AccessDeniedException();
$exception = new AccessDeniedException();
$exception->setAttributes($this->role);

throw $exception;
}

$username = $request->get($this->usernameParameter);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Security/Http/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": ">=5.5.9",
"symfony/security-core": "~2.8|~3.0",
"symfony/security-core": "~3.2",
"symfony/event-dispatcher": "~2.8|~3.0",
"symfony/http-foundation": "~2.8|~3.0",
"symfony/http-kernel": "~2.8|~3.0",
Expand Down
0