3

I was implementing a login system in symfony2 which includes security.xml configuration and some custom stuffs...

At successful login the symfony sends user to a specific controller where i wrote few codes to populate some additional data to session. It was working fine so far as we started application from login page so far.

But in some cases when we access some page which requires authentication, symfony force us to login page. after success login it takes us directly to that page (where we came from) instead landing page. In this cases the extra code we wrote on landing page is not getting executed, hence the app is not behaving as expected further.

My Question is to overcome this... is there any hook / setting / function present in symfony2 by which i can execute a set of code on all successful login irrespective of which page user is getting redirected.

2 Answers2

1

hi Joseph i am currently working in symfony 1.4 project, maybe you should use the following code to check if the user is authenticate and then run your code in this block.

if ($user->isAuthenticated()) {
           #Run your code in this block and then redirect the user.
            $this->redirect('@your_page');
        }
Robin
  • 471
  • 6
  • 18
  • hi robin, i think you understood the question wrong. Checking user is authenticated or not is not my problem. I wanted to inject additional session values on user login. I wanted to know where to write it. Because of am having it on my success landing page currently, it fails as sometimes symfony redirect users to specific page where they come from., instead the default landing page :( – Joseph Ranjan S Ranjan S Oct 16 '14 at 07:26
  • i have a login page where the user fills in the user name and password and then clicks the submit button, then the code checks performs the $form->bind() and then it checks if the $form->isValid() if it is valid then $this->getUser()->signIn($values['user']); is used to signin the user and after this i call a function to save the user data in the session just before redirecting the user to other page – Robin Oct 16 '14 at 07:33
  • as soon as the user gets authenticated successfully then you can save the user data to the session. – Robin Oct 16 '14 at 07:34
  • hi robin thanks for your help... actually i use security.yml file configuration for login system. the answer by Mr.Matteo is what i looked for... anyways thank you for your time :) – Joseph Ranjan S Ranjan S Oct 16 '14 at 11:23
1

You can implement a Success Hander.

  1. Write a class that implement the AuthenticationSuccessHandlerInterface:

  2. Define it as a service (you can inject other services like doctine or the security context, in your case the Session)

  3. Add the handler service in the security config like:

    firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false
    
    secured_area:
        pattern:   ^/
        anonymous: ~
        form_login:
            login_path:  login
            check_path:  login_check
            success_handler: some.service.id
        logout:
            path: logout
            target: /
    

Check this for a complete example and the reference doc for all the symfony2 security configuration options (you can configure the failure_handler also).

Hope this help

Community
  • 1
  • 1
Matteo
  • 37,680
  • 11
  • 100
  • 115
  • hi matteo i was facing one more issue in this... i want to do some database queries in it so i need dbal connection in that page... so i modified the service config file like this: arguments: ["@security.http_utils", {}, @doctrine.dbal.default_connection] and recieved it through 'Doctrine\DBAL\Connection' variable. it is working on all pages fine. but throwing 'null given' error when i try to approach project root folder ... like http://localhost/project/web. The error is "AuthenticationSuccessHandler::__construct() must be an instance of Doctrine\DBAL\Connection, none given". any ideas ? – Joseph Ranjan S Ranjan S Oct 17 '14 at 05:18
  • The construct take 3 parameters? Why you are passing an empty array as second arguments? Can you provide the code (just the signature) of the construct method? – Matteo Oct 17 '14 at 05:48
  • hi matteo, here is the code... private $connection; public function __construct( HttpUtils $httpUtils, array $options, Connection $dbalConnection ) { $this->connection = $dbalConnection; parent::__construct( $httpUtils, $options ); } – Joseph Ranjan S Ranjan S Oct 17 '14 at 10:26
  • Hi matteo, any suggestions ? – Joseph Ranjan S Ranjan S Oct 18 '14 at 18:21
  • Hi @JosephRanjanSRanjanS, try open another Question which the service definition files, the code and describe when the problem occur. I hope to have time to investigate on your problem in the next 24 hours. – Matteo Oct 19 '14 at 13:57
  • hi matteo, i have posted this question here : http://stackoverflow.com/questions/26458747/getting-doctrine-dbal-is-null-on-app-initialization – Joseph Ranjan S Ranjan S Oct 20 '14 at 04:50