123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Controller;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
- use Symfony\Component\Security\Core\Exception\AccessDeniedException;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Sonata\AdminBundle\Exception\ModelManagerException;
- use Symfony\Component\HttpFoundation\Request;
- use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
- use Sonata\AdminBundle\Admin\BaseFieldDescription;
- use Sonata\AdminBundle\Util\AdminObjectAclData;
- class CRUDController extends Controller
- {
- /**
- * The related Admin class
- *
- * @var \Sonata\AdminBundle\Admin\AdminInterface
- */
- protected $admin;
- /**
- * @param mixed $data
- * @param integer $status
- * @param array $headers
- *
- * @return Response with json encoded data
- */
- protected function renderJson($data, $status = 200, $headers = array())
- {
- // fake content-type so browser does not show the download popup when this
- // response is rendered through an iframe (used by the jquery.form.js plugin)
- // => don't know yet if it is the best solution
- if ($this->get('request')->get('_xml_http_request')
- && strpos($this->get('request')->headers->get('Content-Type'), 'multipart/form-data') === 0) {
- $headers['Content-Type'] = 'text/plain';
- } else {
- $headers['Content-Type'] = 'application/json';
- }
- return new Response(json_encode($data), $status, $headers);
- }
- /**
- *
- * @return boolean true if the request is done by an ajax like query
- */
- protected function isXmlHttpRequest()
- {
- return $this->get('request')->isXmlHttpRequest() || $this->get('request')->get('_xml_http_request');
- }
- /**
- * Returns the correct RESTful verb, given either by the request itself or
- * via the "_method" parameter.
- *
- * @return string HTTP method, either
- */
- protected function getRestMethod()
- {
- $request = $this->getRequest();
- if (Request::getHttpMethodParameterOverride() || !$request->request->has('_method')) {
- return $request->getMethod();
- }
- return $request->request->get('_method');
- }
- /**
- * Sets the Container associated with this Controller.
- *
- * @param ContainerInterface $container A ContainerInterface instance
- */
- public function setContainer(ContainerInterface $container = null)
- {
- $this->container = $container;
- $this->configure();
- }
- /**
- * Contextualize the admin class depends on the current request
- *
- * @throws \RuntimeException
- * @return void
- */
- protected function configure()
- {
- $adminCode = $this->container->get('request')->get('_sonata_admin');
- if (!$adminCode) {
- throw new \RuntimeException(sprintf('There is no `_sonata_admin` defined for the controller `%s` and the current route `%s`', get_class($this), $this->container->get('request')->get('_route')));
- }
- $this->admin = $this->container->get('sonata.admin.pool')->getAdminByAdminCode($adminCode);
- if (!$this->admin) {
- throw new \RuntimeException(sprintf('Unable to find the admin class related to the current controller (%s)', get_class($this)));
- }
- $rootAdmin = $this->admin;
- if ($this->admin->isChild()) {
- $this->admin->setCurrentChild(true);
- $rootAdmin = $rootAdmin->getParent();
- }
- $request = $this->container->get('request');
- $rootAdmin->setRequest($request);
- if ($request->get('uniqid')) {
- $this->admin->setUniqid($request->get('uniqid'));
- }
- }
- /**
- * return the base template name
- *
- * @return string the template name
- */
- protected function getBaseTemplate()
- {
- if ($this->isXmlHttpRequest()) {
- return $this->admin->getTemplate('ajax');
- }
- return $this->admin->getTemplate('layout');
- }
- /**
- * @param string $view
- * @param array $parameters
- * @param Response $response
- *
- * @return Response
- */
- public function render($view, array $parameters = array(), Response $response = null)
- {
- $parameters['admin'] = isset($parameters['admin']) ? $parameters['admin'] : $this->admin;
- $parameters['base_template'] = isset($parameters['base_template']) ? $parameters['base_template'] : $this->getBaseTemplate();
- $parameters['admin_pool'] = $this->get('sonata.admin.pool');
- return parent::render($view, $parameters, $response);
- }
- /**
- * return the Response object associated to the list action
- *
- * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
- *
- * @return Response
- */
- public function listAction()
- {
- if (false === $this->admin->isGranted('LIST')) {
- throw new AccessDeniedException();
- }
- $datagrid = $this->admin->getDatagrid();
- $formView = $datagrid->getForm()->createView();
- // set the theme for the current Admin Form
- $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());
- return $this->render($this->admin->getTemplate('list'), array(
- 'action' => 'list',
- 'form' => $formView,
- 'datagrid' => $datagrid,
- 'csrf_token' => $this->getCsrfToken('sonata.batch'),
- ));
- }
- /**
- * execute a batch delete
- *
- * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
- *
- * @param \Sonata\AdminBundle\Datagrid\ProxyQueryInterface $query
- *
- * @return \Symfony\Component\HttpFoundation\RedirectResponse
- */
- public function batchActionDelete(ProxyQueryInterface $query)
- {
- if (false === $this->admin->isGranted('DELETE')) {
- throw new AccessDeniedException();
- }
- $modelManager = $this->admin->getModelManager();
- try {
- $modelManager->batchDelete($this->admin->getClass(), $query);
- $this->addFlash('sonata_flash_success', 'flash_batch_delete_success');
- } catch ( ModelManagerException $e ) {
- $this->addFlash('sonata_flash_error', 'flash_batch_delete_error');
- }
- return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
- }
- /**
- * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException|\Symfony\Component\Security\Core\Exception\AccessDeniedException
- *
- * @param mixed $id
- *
- * @return Response|RedirectResponse
- */
- public function deleteAction($id)
- {
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- if (false === $this->admin->isGranted('DELETE', $object)) {
- throw new AccessDeniedException();
- }
- if ($this->getRestMethod() == 'DELETE') {
- // check the csrf token
- $this->validateCsrfToken('sonata.delete');
- try {
- $this->admin->delete($object);
- if ($this->isXmlHttpRequest()) {
- return $this->renderJson(array('result' => 'ok'));
- }
- $this->addFlash(
- 'sonata_flash_success',
- $this->admin->trans('flash_delete_success', array('%name%' => $this->admin->toString($object)),
- 'SonataAdminBundle')
- );
- } catch (ModelManagerException $e) {
- if ($this->isXmlHttpRequest()) {
- return $this->renderJson(array('result' => 'error'));
- }
- $this->addFlash(
- 'sonata_flash_error',
- $this->admin->trans('flash_delete_error', array('%name%' => $this->admin->toString($object)),
- 'SonataAdminBundle')
- );
- }
- return new RedirectResponse($this->admin->generateUrl('list'));
- }
- return $this->render($this->admin->getTemplate('delete'), array(
- 'object' => $object,
- 'action' => 'delete',
- 'csrf_token' => $this->getCsrfToken('sonata.delete')
- ));
- }
- /**
- * return the Response object associated to the edit action
- *
- *
- * @param mixed $id
- *
- * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
- * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
- *
- * @return Response
- */
- public function editAction($id = null)
- {
- // the key used to lookup the template
- $templateKey = 'edit';
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- if (false === $this->admin->isGranted('EDIT', $object)) {
- throw new AccessDeniedException();
- }
- $this->admin->setSubject($object);
- /** @var $form \Symfony\Component\Form\Form */
- $form = $this->admin->getForm();
- $form->setData($object);
- if ($this->getRestMethod() == 'POST') {
- $form->submit($this->get('request'));
- $isFormValid = $form->isValid();
- // persist if the form was valid and if in preview mode the preview was approved
- if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
- $this->admin->update($object);
- if ($this->isXmlHttpRequest()) {
- return $this->renderJson(array(
- 'result' => 'ok',
- 'objectId' => $this->admin->getNormalizedIdentifier($object)
- ));
- }
- $this->addFlash('sonata_flash_success', $this->admin->trans('flash_edit_success', array('%name%' => $this->admin->toString($object)), 'SonataAdminBundle'));
- // redirect to edit mode
- return $this->redirectTo($object);
- }
- // show an error message if the form failed validation
- if (!$isFormValid) {
- if (!$this->isXmlHttpRequest()) {
- $this->addFlash('sonata_flash_error', $this->admin->trans('flash_edit_error', array('%name%' => $this->admin->toString($object)), 'SonataAdminBundle'));
- }
- } elseif ($this->isPreviewRequested()) {
- // enable the preview template if the form was valid and preview was requested
- $templateKey = 'preview';
- $this->admin->getShow();
- }
- }
- $view = $form->createView();
- // set the theme for the current Admin Form
- $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
- return $this->render($this->admin->getTemplate($templateKey), array(
- 'action' => 'edit',
- 'form' => $view,
- 'object' => $object,
- ));
- }
- /**
- * redirect the user depend on this choice
- *
- * @param object $object
- *
- * @return Response
- */
- protected function redirectTo($object)
- {
- $url = false;
- if ($this->get('request')->get('btn_update_and_list')) {
- $url = $this->admin->generateUrl('list');
- }
- if ($this->get('request')->get('btn_create_and_list')) {
- $url = $this->admin->generateUrl('list');
- }
- if ($this->get('request')->get('btn_create_and_create')) {
- $params = array();
- if ($this->admin->hasActiveSubClass()) {
- $params['subclass'] = $this->get('request')->get('subclass');
- }
- $url = $this->admin->generateUrl('create', $params);
- }
- if (!$url) {
- $url = $this->admin->generateObjectUrl('edit', $object);
- }
- return new RedirectResponse($url);
- }
- /**
- * return the Response object associated to the batch action
- *
- * @throws \RuntimeException
- * @return Response
- */
- public function batchAction()
- {
- $restMethod = $this->getRestMethod();
- if ('POST' !== $restMethod) {
- throw $this->createNotFoundException(sprintf('Invalid request type "%s", POST expected', $restMethod));
- }
- // check the csrf token
- $this->validateCsrfToken('sonata.batch');
- $confirmation = $this->get('request')->get('confirmation', false);
- if ($data = json_decode($this->get('request')->get('data'), true)) {
- $action = $data['action'];
- $idx = $data['idx'];
- $allElements = $data['all_elements'];
- $this->get('request')->request->replace($data);
- } else {
- $this->get('request')->request->set('idx', $this->get('request')->get('idx', array()));
- $this->get('request')->request->set('all_elements', $this->get('request')->get('all_elements', false));
- $action = $this->get('request')->get('action');
- $idx = $this->get('request')->get('idx');
- $allElements = $this->get('request')->get('all_elements');
- $data = $this->get('request')->request->all();
- unset($data['_sonata_csrf_token']);
- }
- $batchActions = $this->admin->getBatchActions();
- if (!array_key_exists($action, $batchActions)) {
- throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
- }
- $camelizedAction = BaseFieldDescription::camelize($action);
- $isRelevantAction = sprintf('batchAction%sIsRelevant', ucfirst($camelizedAction));
- if (method_exists($this, $isRelevantAction)) {
- $nonRelevantMessage = call_user_func(array($this, $isRelevantAction), $idx, $allElements);
- } else {
- $nonRelevantMessage = count($idx) != 0 || $allElements; // at least one item is selected
- }
- if (!$nonRelevantMessage) { // default non relevant message (if false of null)
- $nonRelevantMessage = 'flash_batch_empty';
- }
- $datagrid = $this->admin->getDatagrid();
- $datagrid->buildPager();
- if (true !== $nonRelevantMessage) {
- $this->addFlash('sonata_flash_info', $nonRelevantMessage);
- return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
- }
- $askConfirmation = isset($batchActions[$action]['ask_confirmation']) ? $batchActions[$action]['ask_confirmation'] : true;
- if ($askConfirmation && $confirmation != 'ok') {
- $actionLabel = $this->admin->trans($this->admin->getTranslationLabel($action, 'action'));
- $formView = $datagrid->getForm()->createView();
- return $this->render($this->admin->getTemplate('batch_confirmation'), array(
- 'action' => 'list',
- 'action_label' => $actionLabel,
- 'datagrid' => $datagrid,
- 'form' => $formView,
- 'data' => $data,
- 'csrf_token' => $this->getCsrfToken('sonata.batch'),
- ));
- }
- // execute the action, batchActionXxxxx
- $finalAction = sprintf('batchAction%s', ucfirst($camelizedAction));
- if (!method_exists($this, $finalAction)) {
- throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $finalAction));
- }
- $query = $datagrid->getQuery();
- $query->setFirstResult(null);
- $query->setMaxResults(null);
- if (count($idx) > 0) {
- $this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
- } elseif (!$allElements) {
- $query = null;
- }
- return call_user_func(array($this, $finalAction), $query);
- }
- /**
- * return the Response object associated to the create action
- *
- * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
- * @return Response
- */
- public function createAction()
- {
- // the key used to lookup the template
- $templateKey = 'edit';
- if (false === $this->admin->isGranted('CREATE')) {
- throw new AccessDeniedException();
- }
- $object = $this->admin->getNewInstance();
- $this->admin->setSubject($object);
- /** @var $form \Symfony\Component\Form\Form */
- $form = $this->admin->getForm();
- $form->setData($object);
- if ($this->getRestMethod()== 'POST') {
- $form->submit($this->get('request'));
- $isFormValid = $form->isValid();
- // persist if the form was valid and if in preview mode the preview was approved
- if ($isFormValid && (!$this->isInPreviewMode() || $this->isPreviewApproved())) {
- $this->admin->create($object);
- if ($this->isXmlHttpRequest()) {
- return $this->renderJson(array(
- 'result' => 'ok',
- 'objectId' => $this->admin->getNormalizedIdentifier($object)
- ));
- }
- $this->addFlash('sonata_flash_success', $this->admin->trans('flash_create_success', array('%name%' => $this->admin->toString($object)), 'SonataAdminBundle'));
- // redirect to edit mode
- return $this->redirectTo($object);
- }
- // show an error message if the form failed validation
- if (!$isFormValid) {
- if (!$this->isXmlHttpRequest()) {
- $this->addFlash('sonata_flash_error', $this->admin->trans('flash_create_error', array('%name%' => $this->admin->toString($object)), 'SonataAdminBundle'));
- }
- } elseif ($this->isPreviewRequested()) {
- // pick the preview template if the form was valid and preview was requested
- $templateKey = 'preview';
- $this->admin->getShow();
- }
- }
- $view = $form->createView();
- // set the theme for the current Admin Form
- $this->get('twig')->getExtension('form')->renderer->setTheme($view, $this->admin->getFormTheme());
- return $this->render($this->admin->getTemplate($templateKey), array(
- 'action' => 'create',
- 'form' => $view,
- 'object' => $object,
- ));
- }
- /**
- * Returns true if the preview is requested to be shown
- *
- * @return boolean
- */
- protected function isPreviewRequested()
- {
- return ($this->get('request')->get('btn_preview') !== null);
- }
- /**
- * Returns true if the preview has been approved
- *
- * @return boolean
- */
- protected function isPreviewApproved()
- {
- return ($this->get('request')->get('btn_preview_approve') !== null);
- }
- /**
- * Returns true if the request is in the preview workflow
- *
- * That means either a preview is requested or the preview has already been shown
- * and it got approved/declined.
- *
- * @return boolean
- */
- protected function isInPreviewMode()
- {
- return $this->admin->supportsPreviewMode()
- && ($this->isPreviewRequested()
- || $this->isPreviewApproved()
- || $this->isPreviewDeclined());
- }
- /**
- * Returns true if the preview has been declined
- *
- * @return boolean
- */
- protected function isPreviewDeclined()
- {
- return ($this->get('request')->get('btn_preview_decline') !== null);
- }
- /**
- * return the Response object associated to the view action
- *
- * @param null $id
- *
- * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
- * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
- *
- * @return Response
- */
- public function showAction($id = null)
- {
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- if (false === $this->admin->isGranted('VIEW', $object)) {
- throw new AccessDeniedException();
- }
- $this->admin->setSubject($object);
- return $this->render($this->admin->getTemplate('show'), array(
- 'action' => 'show',
- 'object' => $object,
- 'elements' => $this->admin->getShow(),
- ));
- }
- /**
- * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException|\Symfony\Component\Security\Core\Exception\AccessDeniedException
- *
- * @param mixed $id
- *
- * @return Response
- */
- public function historyAction($id = null)
- {
- if (false === $this->admin->isGranted('EDIT')) {
- throw new AccessDeniedException();
- }
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- $manager = $this->get('sonata.admin.audit.manager');
- if (!$manager->hasReader($this->admin->getClass())) {
- throw new NotFoundHttpException(sprintf('unable to find the audit reader for class : %s', $this->admin->getClass()));
- }
- $reader = $manager->getReader($this->admin->getClass());
- $revisions = $reader->findRevisions($this->admin->getClass(), $id);
- return $this->render($this->admin->getTemplate('history'), array(
- 'action' => 'history',
- 'object' => $object,
- 'revisions' => $revisions,
- ));
- }
- /**
- * @param null $id
- * @param string $revision
- *
- * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
- * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
- *
- * @return Response
- */
- public function historyViewRevisionAction($id = null, $revision = null)
- {
- if (false === $this->admin->isGranted('EDIT')) {
- throw new AccessDeniedException();
- }
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- $manager = $this->get('sonata.admin.audit.manager');
- if (!$manager->hasReader($this->admin->getClass())) {
- throw new NotFoundHttpException(sprintf('unable to find the audit reader for class : %s', $this->admin->getClass()));
- }
- $reader = $manager->getReader($this->admin->getClass());
- // retrieve the revisioned object
- $object = $reader->find($this->admin->getClass(), $id, $revision);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the targeted object `%s` from the revision `%s` with classname : `%s`', $id, $revision, $this->admin->getClass()));
- }
- $this->admin->setSubject($object);
- return $this->render($this->admin->getTemplate('show'), array(
- 'action' => 'show',
- 'object' => $object,
- 'elements' => $this->admin->getShow(),
- ));
- }
- /**
- * @param Request $request
- *
- * @throws \RuntimeException
- * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
- *
- * @return Response
- */
- public function exportAction(Request $request)
- {
- if (false === $this->admin->isGranted('EXPORT')) {
- throw new AccessDeniedException();
- }
- $format = $request->get('format');
- $allowedExportFormats = (array) $this->admin->getExportFormats();
- if (!in_array($format, $allowedExportFormats) ) {
- throw new \RuntimeException(sprintf('Export in format `%s` is not allowed for class: `%s`. Allowed formats are: `%s`', $format, $this->admin->getClass(), implode(', ', $allowedExportFormats)));
- }
- $filename = sprintf('export_%s_%s.%s',
- strtolower(substr($this->admin->getClass(), strripos($this->admin->getClass(), '\\') + 1)),
- date('Y_m_d_H_i_s', strtotime('now')),
- $format
- );
- return $this->get('sonata.admin.exporter')->getResponse($format, $filename, $this->admin->getDataSourceIterator());
- }
- /**
- * Gets ACL users
- *
- * @return \Traversable
- */
- protected function getAclUsers()
- {
- $aclUsers = array();
- $userManagerServiceName = $this->container->getParameter('sonata.admin.security.acl_user_manager');
- if ($userManagerServiceName !== null && $this->has($userManagerServiceName)) {
- $userManager = $this->get($userManagerServiceName);
- if (method_exists($userManager, 'findUsers')) {
- $aclUsers = $userManager->findUsers();
- }
- }
- return is_array($aclUsers) ? new \ArrayIterator($aclUsers) : $aclUsers;
- }
- /**
- * return the Response object associated to the acl action
- *
- * @param null $id
- *
- * @throws \Symfony\Component\Security\Core\Exception\AccessDeniedException
- * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
- *
- * @return Response
- */
- public function aclAction($id = null)
- {
- if (!$this->admin->isAclEnabled()) {
- throw new NotFoundHttpException('ACL are not enabled for this admin');
- }
- $id = $this->get('request')->get($this->admin->getIdParameter());
- $object = $this->admin->getObject($id);
- if (!$object) {
- throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
- }
- if (false === $this->admin->isGranted('MASTER', $object)) {
- throw new AccessDeniedException();
- }
- $this->admin->setSubject($object);
- $aclUsers = $this->getAclUsers();
- $adminObjectAclManipulator = $this->get('sonata.admin.object.manipulator.acl.admin');
- $adminObjectAclData = new AdminObjectAclData(
- $this->admin,
- $object,
- $aclUsers,
- $adminObjectAclManipulator->getMaskBuilderClass()
- );
- $form = $adminObjectAclManipulator->createForm($adminObjectAclData);
- $request = $this->getRequest();
- if ($request->getMethod() === 'POST') {
- $form->submit($request);
- if ($form->isValid()) {
- $adminObjectAclManipulator->updateAcl($adminObjectAclData);
- $this->addFlash('sonata_flash_success', 'flash_acl_edit_success');
- return new RedirectResponse($this->admin->generateObjectUrl('acl', $object));
- }
- }
- return $this->render($this->admin->getTemplate('acl'), array(
- 'action' => 'acl',
- 'permissions' => $adminObjectAclData->getUserPermissions(),
- 'object' => $object,
- 'users' => $aclUsers,
- 'form' => $form->createView()
- ));
- }
- /**
- * Adds a flash message for type.
- *
- * @param string $type
- * @param string $message
- */
- protected function addFlash($type, $message)
- {
- $this->get('session')
- ->getFlashBag()
- ->add($type, $message);
- }
- /**
- * Validate CSRF token for action with out form
- *
- * @param string $intention
- *
- * @throws \RuntimeException
- */
- protected function validateCsrfToken($intention)
- {
- if (!$this->container->has('form.csrf_provider')) {
- return;
- }
- if (!$this->container->get('form.csrf_provider')->isCsrfTokenValid($intention, $this->get('request')->request->get('_sonata_csrf_token', false))) {
- throw new HttpException(400, "The csrf token is not valid, CSRF attack ?");
- }
- }
- /**
- * @param $intention
- *
- * @return string
- */
- protected function getCsrfToken($intention)
- {
- if (!$this->container->has('form.csrf_provider')) {
- return false;
- }
- return $this->container->get('form.csrf_provider')->generateCsrfToken($intention);
- }
- }
|