2012-11-14 105 views
2

我已經搜索了this topic,但它沒有幫助我。Symfony2註冊後自動登錄

如何在註冊後驗證用戶? 我的錯誤在哪裏?

security.yml

security: 

    providers: 
     #chain_provider is used here to implement a multiple firewalls in future: admins, accounts ... 
     chain_provider: 
     chain: 
      providers: [admins,accounts] 
     admins: 
     entity: { class: FME\Bundle\_CoreBundle\Entity\Admin, property: username } 
     accounts: 
     entity: { class: FME\Bundle\_CoreBundle\Entity\Account, property: email } 

    encoders: 
     FME\Bundle\_CoreBundle\Entity\Admin: sha512 
     FME\Bundle\_CoreBundle\Entity\Account: sha512 

    firewalls: 
     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 

     #no firewall for the Login page 
     admin_area_login: 
      pattern: ^/admin/login$ 
      security: false 

     admin_area: 
      pattern: ^/admin/ 
      provider: admins 
      form_login: 
       check_path: fme_aa_login_handler 
       login_path: fme_aa_login 
      logout: 
       path: fme_aa_logout 
       target: fme_aa_login 
      #anonymous: ~ 
      #http_basic: 
      # realm: "Secured Demo Area" 

     #no firewall for the Login page 
     account_area_login: 
      pattern: ^/account/login$ 
      security: false 

     account_area: 
      pattern: ^/account/ 
      provider: accounts 
      form_login: 
       check_path: fme_aca_login_handler 
       login_path: fme_aca_login 
      logout: 
       path: fme_aca_logout 
       target: fme_aca_login 

登記控制器如下:

所有的
namespace FME\Bundle\FtdBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\HttpFoundation\Request; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; 
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; 

use FME\Bundle\_CoreBundle\Entity\Account; 
use FME\Bundle\FtdBundle\Form\RegistrationType; 

/** 
* @Route("/registration") 
*/ 
class RegistrationController extends Controller 
{  
    /** 
    * Account registration 
    * 
    * @Route("/",name="fme_ftd_registration") 
    * @Template() 
    */ 
    public function indexAction(Request $request) 
    { 
     $account = new Account(); 

     //set default role group 
     $account->setRoleGroup($this->getDoctrine()->getRepository('FMECoreBundle:AccountRoleGroup')->findDefault()); 

     //default company type from the FMECoreBundle is used 
     $form = $this->createForm(new RegistrationType(), $account); 

     if ($request->isMethod('POST')) 
     { 
      $form->bind($request); 

      if ($form->isValid()) 
      { 
       $encoder = $this->container->get('security.encoder_factory')->getEncoder($account); 

       //encode password using current encoder 
       $password = $encoder->encodePassword($form->get('password')->getData(), $account->getSalt()); 

       //set encrypted password 
       $account->setPassword($password); 

       //save an object in the DB 
       $em = $this->getDoctrine()->getEntityManager(); 
       $em->persist($account); 
       $em->flush(); 

       //send the token to account via email 
       if (! $this->_sendVerificationToken($account)) 
       { 
        $this->get('session')->setFlash('error', 
         $this->get('translator')->trans('Error sending the verification token.') 
        ); 
       } 

       $this->get('session')->setFlash('success', 
        $this->get('translator')->trans('Your account was created. Please check you inbox to verify the email.') 
       ); 

       //Automatic post-registration user authentication 
       $this->_authenticateAccount($account); 

       //redirect to home page in the account area 
       return $this->redirect($this->generateUrl('fme_aca_dashboard')); 
      } 
     } 

     return array('form' => $form->createView()); 
    } 

    /** 
    * Send the token to verify an account email 
    */ 
    protected function _sendVerificationToken(Account $account) 
    { 
     return TRUE; 
    } 

    /** 
    * Automatic post-registration user authentication 
    */ 
    protected function _authenticateAccount(Account $account) 
    { 
     $token = new UsernamePasswordToken($account, null, 'account_area', $account->getRoles()); 
     $this->get('security.context')->setToken($token); 
    } 
} 

回答

7

首先確保註冊頁面適合的防火牆之一。不是附加額外的參數,每個防火牆:

context: <string> 

像這樣:

account_area_login: 
     ... 
     context: administration 

    admin_area: 
     ... 
     context: administration 

上下文允許共享不同的防火牆之間的身份驗證cookie。所以爲了保證用戶在註冊後進行認證,註冊頁面的防火牆和其他防火牆應該具有相同的上下文。

+0

它的工作原理。 :)非常非常感謝 – Mikhail

+0

但我不明白爲什麼我需要指定一個'account_area'防火牆密鑰作爲第三個參數,如果這是不必要的,因爲setToken()隻影響當前使用的路由的'security.context'? – Mikhail

+0

我的意思是,我可以通過'blablabla'而不是'account_area',它也可以工作。你知道爲什麼嗎? – Mikhail