I need to get:
for each users that tries to log in to my website. There is no problem to get that information if user logged successfully but I don't know how to get IP address for failure attempts.
Her's my code:
app.listener.interactive_login_listener:
class: AppBundle\EventListener\LogonListener
arguments: ['@security.token_storage', '@doctrine.orm.entity_manager', '@security.authorization_checker', '@router', '@manager.settings']
tags:
- { name: kernel.event_listener, event: security.interactive_login, method: onAuthenticationSuccess }
# authentication failure event listener
app.listener.authentication_failure_event_listener:
class: AppBundle\EventListener\LogonListener
arguments: ['@security.token_storage', '@doctrine.orm.entity_manager', '@security.authorization_checker', '@router', '@manager.settings']
tags:
- { name: kernel.event_listener, event: security.authentication.failure, method: onAuthenticationFailure }
and the listener:
class LogonListener implements EventSubscriberInterface
{
private $_tokenStorage;
private $_em;
private $_authorizationChecker;
private $_router;
private $_settings;
public function __construct(
TokenStorage $tokenStorage,
EntityManager $em,
AuthorizationCheckerInterface $authorizationChecker,
Router $router,
$settings)
{
$this->_tokenStorage = $tokenStorage;
$this->_em = $em;
$this->_authorizationChecker = $authorizationChecker;
$this->_router = $router;
$this->_settings = $settings;
}
/**
* getSubscribedEvents
*
* @return array
*/
public static function getSubscribedEvents()
{
return array(
AuthenticationEvents::AUTHENTICATION_SUCCESS => 'onAuthenticationSuccess',
AuthenticationEvents::AUTHENTICATION_FAILURE => 'onAuthenticationFailure',
);
}
/**
* onAuthenticationSuccess
*
* @param InteractiveLoginEvent $event
*/
public function onAuthenticationSuccess(InteractiveLoginEvent $event)
{
var_dump( $event->getAuthenticationToken()->getUser() );
var_dump( $event->getRequest()->getClientIp() );
die();
}
public function onAuthenticationFailure(AuthenticationFailureEvent $event)
{
$userName = $event->getAuthenticationToken()->getUsername();
$user = $this->_em->getRepository('AppBundle:User')->findOneByUsername($userName);
var_dump( $user );
// how to get IP?
die();
}
Inject RequestStack
into listener and get Request
from it. You can get IP from getClientIp()
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @return string
*/
protected function resolveClientIp()
{
return $this->requestStack->getMasterRequest()->getClientIp();
}
Have you tried to inject RequestStack
to your listener ?
//in your service
# authentication failure event listener
app.listener.authentication_failure_event_listener:
class: AppBundle\EventListener\LogonListener
arguments: ['@security.token_storage', '@doctrine.orm.entity_manager', '@security.authorization_checker', '@router', '@manager.settings', '@request_stack']
tags:
- { name: kernel.event_listener, event: security.authentication.failure, method: onAuthenticationFailure }
//in your listener
class LogonListener implements EventSubscriberInterface
{
private $_tokenStorage;
private $_em;
private $_authorizationChecker;
private $_router;
private $_settings;
private $_request
public function __construct(TokenStorage $tokenStorage, EntityManager $em, AuthorizationCheckerInterface $authorizationChecker, Router $router, $settings, $request)
{
$this->_tokenStorage = $tokenStorage;
$this->_em = $em;
$this->_authorizationChecker = $authorizationChecker;
$this->_router = $router;
$this->_settings = $settings;
$this->_request = $request
}
public function onAuthenticationFailure(AuthenticationFailureEvent $event)
{
$userName = $event->getAuthenticationToken()->getUsername();
$user = $this->_em->getRepository('AppBundle:User')->findOneByUsername($userName);
var_dump( $this->request->getCurrentRequest()->getClientIp() );
die();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With