SecuredController.php 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Acme\DemoBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\Security\Core\SecurityContext;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  7. use JMS\SecurityExtraBundle\Annotation\Secure;
  8. /**
  9. * @Route("/demo/secured")
  10. */
  11. class SecuredController extends Controller
  12. {
  13. /**
  14. * @Route("/login", name="_demo_login")
  15. * @Template()
  16. */
  17. public function loginAction()
  18. {
  19. if ($this->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
  20. $error = $this->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
  21. } else {
  22. $error = $this->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
  23. }
  24. return array(
  25. 'last_username' => $this->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
  26. 'error' => $error,
  27. );
  28. }
  29. /**
  30. * @Route("/login_check", name="_security_check")
  31. */
  32. public function securityCheckAction()
  33. {
  34. // The security layer will intercept this request
  35. }
  36. /**
  37. * @Route("/logout", name="_demo_logout")
  38. */
  39. public function logoutAction()
  40. {
  41. // The security layer will intercept this request
  42. }
  43. /**
  44. * @Route("/hello", defaults={"name"="World"}),
  45. * @Route("/hello/{name}", name="_demo_secured_hello")
  46. * @Template()
  47. */
  48. public function helloAction($name)
  49. {
  50. return array('name' => $name);
  51. }
  52. /**
  53. * @Route("/hello/admin/{name}", name="_demo_secured_hello_admin")
  54. * @Secure(roles="ROLE_ADMIN")
  55. * @Template()
  56. */
  57. public function helloadminAction($name)
  58. {
  59. return array('name' => $name);
  60. }
  61. }