Skip to content

Commit

Permalink
[Security] Expose the required roles in AccessDeniedException
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicofuma committed Apr 28, 2016
1 parent f146f84 commit 9001f9e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
Expand Up @@ -18,8 +18,43 @@
*/
class AccessDeniedException extends \RuntimeException
{
private $attributes = [];
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 $attributes
*/
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;
}

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

/**
* @param mixed $object
*/
public function setObject($object)
{
$this->object = $object;
}
}
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;
}
}
}
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(array($this->role));

throw $exception;
}

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

0 comments on commit 9001f9e

Please sign in to comment.