Twinsen
Twinsen

Reputation: 893

Check User profile When change page Symfony2

I create User with FOSUserBundle, and when user created I create also a profile

User And Profile

I'd like that whenever the user changes the page, actually check that the user is navigating is authenticated and has also the profile

for now in my Controller i do

$user = $this->getUser();
if (!is_object($user)) { //i have to add || null !== $user->getProfile()
    throw new AccessDeniedException('This user does not have access to this section.');
}

But I would not repeat this code in all controllers, and I would also check if the user has a profile, otherwise redirect to the home page

Upvotes: 1

Views: 120

Answers (2)

Snroki
Snroki

Reputation: 2444

You can use the request listener : http://symfony.com/doc/current/cookbook/service_container/event_listener.html#request-events-checking-types

Just add your magic and redirect through the event to the homepage

Upvotes: 0

Amine
Amine

Reputation: 271

you can use JMSSecurityExtraBundle to add annotation for each action:

 /**
 * @Secure(roles="ROLE_USER, ROLE_FOO, ROLE_ADMIN")
 */

or without using of the bundle:

if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
    throw new AccessDeniedException();
}

its more simple. Maybe theres another solution to make it for all your controller but i don't know it.

Upvotes: 2

Related Questions