Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony][SF2][Symfony2]Przekierowanu po zalogowaniu
Forum PHP.pl > Forum > PHP > Frameworki
Michal_Sz
Witam,

Chciałbym zapytać w jaki sposób ustawić przekierowanie po zalogowaniu się w zależności czy to zwykły użytkownik czy admin.
Chciałbym abyw zależności od rodzaju użytkownika przekierowywało mnie na odpowiednią zakładkę.
Na tą chwilę mój plik security wygląda tak:
  1. # you can read more about security in the related section of the documentation
  2. # <a href="http://symfony.com/doc/current/book/security.html" target="_blank">http://symfony.com/doc/current/book/security.html</a>
  3. security:
  4. # <a href="http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password" target="_blank">http://symfony.com/doc/current/book/securi...user-s-password</a>
  5. encoders:
  6. User\ProfileBundle\Entity\User:
  7. algorithm: bcrypt
  8. cost: 15
  9.  
  10. # <a href="http://symfony.com/doc/current/book/security.html#hierarchical-roles" target="_blank">http://symfony.com/doc/current/book/securi...rarchical-roles</a>
  11. role_hierarchy:
  12. ROLE_ADMIN: ROLE_USER
  13. ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  14.  
  15. # <a href="http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers" target="_blank">http://symfony.com/doc/current/book/securi...-user-providers</a>
  16. providers:
  17. main:
  18. entity:
  19. class: User\ProfileBundle\Entity\User
  20. property: username
  21. in_memory:
  22. memory:
  23. users:
  24. user: { password: userpass, roles: [ 'ROLE_USER' ] }
  25. admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
  26.  
  27. # the main part of the security, where you can set up firewalls
  28. # for specific sections of your app
  29. firewalls:
  30. # disables authentication for assets and the profiler, adapt it according to your needs
  31. dev:
  32. pattern: ^/(_(profiler|wdt)|css|images|js)/
  33. security: false
  34. # the login page has to be accessible for everybody
  35. demo_login:
  36. pattern: ^/demo/secured/login$
  37. security: false
  38.  
  39. # secures part of the application
  40. demo_secured_area:
  41. pattern: ^/demo/secured/
  42. # it's important to notice that in this case _demo_security_check and _demo_login
  43. # are route names and that they are specified in the AcmeDemoBundle
  44. form_login:
  45. check_path: _demo_security_check
  46. login_path: _demo_login
  47. logout:
  48. path: _demo_logout
  49. target: _demo
  50. #anonymous: ~
  51. #http_basic:
  52. # realm: "Secured Demo Area"
  53. secured_area:
  54. pattern: ^/
  55. anonymous: ~
  56. form_login:
  57. always_use_default_target_path: true
  58. default_target_path: /index
  59. login_path: login
  60. check_path: login_check
  61. logout:
  62. path: /logout
  63. target: /login
  64. # with these settings you can restrict or allow access for different parts
  65. # of your application based on roles, ip, host or methods
  66. # <a href="http://symfony.com/doc/current/book/security.html#security-book-access-control-matching-options" target="_blank">http://symfony.com/doc/current/book/securi...atching-options</a>
  67. access_control:
  68. #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
  69. - { path: ^/after_login, roles: ROLE_USER }
  70. #- { path: ^/index, roles: ROLE_USER }
  71. - {path: ^/index,roles: ROLE_SUPER_ADMIN }
blahy
form_login w securty ma opcje success_handler
Mozna tam dodac serwis/handler implementujacy AuthenticationSuccessHandlerInterface.
jego metoda zostanie zzwolana w przypadku poprawnego logowania
  1. /**
  2.   * This is called when an interactive authentication attempt succeeds. This
  3.   * is called by authentication listeners inheriting from
  4.   * AbstractAuthenticationListener.
  5.   *
  6.   * @param Request $request
  7.   * @param TokenInterface $token
  8.   *
  9.   * @return Response never null
  10.   */
  11. public function onAuthenticationSuccess(Request $request, TokenInterface $token);


Jest tam dostep do requestu i tokena (usera), mozna wstrzyknac router (do wygenerowania routow) i security (do sprawdzenia rol) no i przekierowac w wybrane miesjce.

Np. tak:
https://gist.github.com/marydn/8061424

To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.