/** * Returns the Response object associated to the acl action. * * @param int|string|null $id * * @return Response|RedirectResponse * * @throws AccessDeniedException If access is not granted. * @throws NotFoundHttpException If the object does not exist or the ACL is not enabled */ 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())); }
/** * Redirect the user depend on this choice. * * @param object $object * @param Request $request * * @return RedirectResponse */ protected function redirectTo($object) { $request = $this->getRequest(); $url = false; if (null !== $request->get('btn_update_and_list')) { $url = $this->admin->generateUrl('list'); } if (null !== $request->get('btn_create_and_list')) { $url = $this->admin->generateUrl('list'); } if (null !== $request->get('btn_create_and_create')) { $params = array(); if ($this->admin->hasActiveSubClass()) { $params['subclass'] = $request->get('subclass'); } $url = $this->admin->generateUrl('create', $params); } if ($this->getRestMethod() === 'DELETE') { $url = $this->admin->generateUrl('list'); } if (!$url) { foreach (array('edit', 'show') as $route) { if ($this->admin->hasRoute($route) && $this->admin->isGranted(strtoupper($route), $object)) { $url = $this->admin->generateObjectUrl($route, $object); break; } } } if (!$url) { $url = $this->admin->generateUrl('list'); } return new RedirectResponse($url); }
/** * return the Response object associated to the view action * * @return \Symfony\Component\HttpFoundation\Response */ public function showAction($id) { if (false === $this->admin->isGranted('SHOW')) { throw new AccessDeniedException(); } $object = $this->admin->getObject($this->get('request')->get($this->admin->getIdParameter())); if (!$object) { throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id)); } $this->admin->setSubject($object); // build the show list $elements = $this->admin->getShow(); return $this->render($this->admin->getShowTemplate(), array( 'action' => 'show', 'object' => $object, 'elements' => $this->admin->getShow(), 'admin' => $this->admin, 'base_template' => $this->getBaseTemplate(), )); }
/** * @param \Symfony\Component\HttpFoundation\Request $request * @return \Symfony\Component\HttpFoundation\Response */ public function exportAction(Request $request) { if (false === $this->admin->isGranted('EXPORT')) { throw new AccessDeniedException(); } $format = $request->get('format'); $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()); }
/** * @param \Symfony\Component\HttpFoundation\Request $request * @return \Symfony\Component\HttpFoundation\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()); }
/** * Tests if the current user as the OWNER right. * * @return bool */ public function isOwner() { // Only a owner can set MASTER and OWNER ACL return $this->admin->isGranted('OWNER', $this->object); }