Bonjour,
Je suis en train de suivre un tutoriel sur le net (fosuserbundle ) et à un moment donné je n'obtient pas le résultat escompté.
Dans mon contrôleur je fait un test sur le fait que l’utilisateur est connecté. Si il est connecté en tant que client il doit être redirigé sur la page client si il est connecté en tant qu'admin il est dirigé
vers admin..hors quant je me connecte que ce soit en client ou admin ça me redirige vers la page admin, si j'ai le rôle client dans la barre d'info de symfony ça me dirige quant même sur la page admin
par contre si je vais sur la page client en tant que client c 'est ok
si je vais sur la page admin en tant que client il me fait un access denied ( donc c'est ok)
si je me connecte en tant que admin c'est ok , j'ai bien accès aux page admin et client.
c'est la méthode "showUserAction" dans mon contrôleur
de me diriger vers tel ou tel page selon le rôle qui ne marche pas
mon fichier security.yml
mon controlleur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 security: encoders: FOS\UserBundle\Model\UserInterface: bcrypt role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: ROLE_ADMIN providers: fos_userbundle: id: fos_user.user_provider.username firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: pattern: ^/ form_login: provider: fos_userbundle csrf_token_generator: security.csrf.token_manager # if you are using Symfony < 2.8, use the following config instead: # csrf_provider: form.csrf_provider logout: true anonymous: true access_control: - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/admin/, role: ROLE_ADMIN } - { path: ^/client/, role: ROLE_USER }
un exemple d'une page twig ( rien de particulier )
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 namespace AppBundle\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; class DefaultController extends Controller { /** * @Route("/", name="homepage") * il n'y a aucune condition d'acces à cette page */ public function indexAction() { return $this->render('homepage.html.twig'); } /** * @return \Symfony\Component\HttpFoundation\Response * * @Route("/admin/", name="admin_page") */ public function adminPageAction() { return $this->render('admin.html.twig'); } /** * @return \Symfony\Component\HttpFoundation\Response * * @Route("/client/", name="client_page") */ public function clientPageAction() { return $this->render('client.html.twig'); } /** * @return \Symfony\Component\HttpFoundation\Response * * @Route("/login_ok", name="login_ok") * * @Security("has_role('ROLE_USER')") */ public function showInfoUserAction() { return $this->render('login_success.html.twig'); } /** * @Route("/user/", name="user_info") * * @Security("is_granted('IS_AUTHENTICATED_FULLY')") * */ public function showUserAction() { if($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN'));{ return $this->render('admin.html.twig'); } if($this->get('security.authorization_checker')->isGranted('ROLE_USER'));{ return $this->render('client.html.twig'); } }
merci pour les pistes..
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 {% extends "base.html.twig" %} {% block body %} <h1>Page Client</h1> <p><a href="{{ path('fos_user_security_logout') }}">Se déconnecter</a></p> {% endblock body %}
Partager