Symfony2, Security, registration and isEnable error

79 Views Asked by At

Guys there is a description about how to make 'isEnabled' (active\inactive account) on registration.

http://symfony.com/doc/current/cookbook/security/entity_provider.html#forbid-inactive-users

But there is NO description how to get this error on login action. For example I have working properly registration where user account default is inactive. After user login how can I get "inAcitve" if account is not activated by email link?

1

There are 1 best solutions below

0
DonCallisto On

Is pretty easy: you can create a service that will act as a event listener

# app/config/config.yml
services:
  your_bundle_name.login_listener:
    class: FQN\Of\Your\Bundle\Class\Listener
    tags: 
      - { kernel.event_listener, security.interactive_login}

tags part is there because, on bootstrap of your application, kernel will "attach" certain type of events to your service and will call a method on that service each time this event is raised. So kernel.event_listener is there because every event listener have to be tagged that way (follow it as a "rule") and security.interactive_login will be fired as a user succesfully log in.

Your class could be something like

//omitting class declaration on purpose
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event) {
  $user = $event->getAuthenticationToken()->getUser();
  if (!$user->isActive()) {
    //do the proper action to display the error
  }
}