8000 feature #11593 [FrameworkBundle] Add shortcut methods to controllers … · symfony/framework-bundle@c48c52f · GitHub
[go: up one dir, main page]

Skip to content

Commit c48c52f

Browse files
committed
feature #11593 [FrameworkBundle] Add shortcut methods to controllers (Cydonia7)
This PR was merged into the 2.6-dev branch. Discussion ---------- [FrameworkBundle] Add shortcut methods to controllers | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #11166 | License | MIT | Doc PR | symfony/symfony-docs#4109 To-do list : - [x] submit changes to the documentation Added redirectToRoute, addFlash, isGranted and checkGranted to controllers. The code seems so simple I didn't feel like adding controller tests was needed since we're just shortcuting other services calls. Commits ------- 74d8c9a Add redirectToRoute, addFlash, isGranted and denyAccessUnlessGranted shortcuts to controllers.
2 parents 19e2ee0 + 8996b9a commit c48c52f

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ CHANGELOG
66

77
* Added `Controller::isCsrfTokenValid` helper
88
* Added configuration for the PropertyAccess component
9+
* Added `Controller::redirectToRoute` helper
10+
* Added `Controller::addFlash` helper
11+
* Added `Controller::isGranted` helper
12+
* Added `Controller::denyAccessUnlessGranted` helper
913

1014
2.5.0
1115
-----

Controller/Controller.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,72 @@ public function redirect($url, $status = 302)
8181
return new RedirectResponse($url, $status);
8282
}
8383

84+
/**
85+
* Returns a RedirectResponse to the given route with the given parameters.
86+
*
87+
* @param string $route The name of the route
88+
* @param array $parameters An array of parameters
89+
* @param int $status The status code to use for the Response
90+
*
91+
* @return RedirectResponse
92+
*/
93+
protected function redirectToRoute($route, array $parameters = array(), $status = 302)
94+
{
95+
return $this->redirect($this->generateUrl($route, $parameters), $status);
96+
}
97+
98+
/**
99+
* Adds a flash message to the current session for type.
100+
*
101+
* @param string $type The type
102+
* @param string $message The message
103+
*
104+
* @throws \LogicException
105+
*/
106+
protected function addFlash($type, $message)
107+
{
108+
if (!$this->container->has('session')) {
109+
throw new \LogicException('You can not use the addFlash method if sessions are disabled.');
110+
}
111+
112+
$this->get('session')->getFlashBag()->add($type, $message);
113+
}
114+
115+
/**
116+
* Checks if the attributes are granted against the current authentication token and optionally supplied object.
117+
*
118+
* @param mixed $attributes The attributes
119+
* @param mixed $object The object
120+
*
121+
* @throws \LogicException
122+
* @return bool
123+
*/
124+
protected function isGranted($attributes, $object = null)
125+
{
126+
if (!$this->container->has('security.context')) {
127+
throw new \LogicException('The SecurityBundle is not registered in your application.');
128+
}
129+
130+
return $this->get('security.context')->isGranted($attributes, $object);
131+
}
132+
133+
/**
134+
* Throws an exception unless the attributes are granted against the current authentication token and optionally
135+
* supplied object.
136+
*
137+
* @param mixed $attributes The attributes
138+
* @param mixed $object The object
139+
* @param string $message The message passed to the exception
140+
*
141+
* @throws AccessDeniedException
142+
*/
143+
protected function denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')
144+
{
145+
if (!$this->isGranted($attributes, $object)) {
146+
throw $this->createAccessDeniedException($message);
147+
}
148+
}
149+
84150
/**
85151
* Returns a rendered view.
86152
*

0 commit comments

Comments
 (0)
0