2014-05-07 57 views
2

使用Symfony2.4.3的Im。我已經嘗試了很多教程中的幾種方法,但仍無法使此登錄工作。我的登錄頁面有兩個不同的表格。 mst_pelajarROLE_USERnis字段作爲用戶名和mst_pegawaiROLE_ADMINna字段作爲用戶名。我可以使它適用於in_memory用戶名和密碼定義。我試圖通過首先在數據庫中插入純文本來使該登錄系統起作用。此設置出現bad credentials錯誤。使用Symfony2登錄2個不同的表格

這裏我security.yml:

security: 
    encoders: 
     #Symfony\Component\Security\Core\User\User: plaintext 
     Sifo\AdminBundle\Entity\MstPelajar: plaintext 
     Sifo\AdminBundle\Entity\MstPegawai: plaintext 

    role_hierarchy: 
     #ROLE_ADMIN:  ROLE_USER 
     #ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] 

    providers: 
    #in_memory: 
    # memory: 
    #  users: 
    #   user: { password: userpass, roles: [ 'ROLE_USER' ] } 
    #   admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] } 

     admin_area: 
      entity: { class: SifoAdminBundle:MstPegawai, property: na } 
     user_area: 
      entity: { class: SifoAdminBundle:MstPelajar, property: nis } 

    firewalls: 
     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 
      anonymous: true 
     alogin: 
      pattern: ^/admin/login$ 
      security: false 
      anonymous: true 
     ulogin: 
      pattern: ^/user/login$ 
      security: false 
      anonymous: true 
     admin_area: 
      pattern: ^/admin 
      anonymous: false 
      form_login: 
       check_path: /admin/login_check 
       login_path: /admin/login 
      logout: 
       path: /admin/logout 
       target: /admin 
     user_area: 
      pattern: ^/user 
      anonymous: false 
      form_login: 
       check_path: /user/login_check 
       login_path: /user/login 
      logout: 
       path: /user/logout 
       target: /user 

    access_control: 
     - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/admin/, roles: ROLE_ADMIN } 
     - { path: ^/user/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/user/, roles: ROLE_USER } 

實體管理:

<?php 

namespace Sifo\AdminBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Security\Core\User\UserInterface; 

/** 
* MstPelajar 
*/ 
class MstPelajar implements UserInterface, \Serializable 
{ 
    /** 
    * @var integer 
    */ 
    private $id; 

    * @var string 
    */ 
    private $nis; 


    /** 
    * @var string 
    */ 
    private $password; 

    /** 
    * @var string 
    */ 
    private $salt; 

    /** 
    * @var boolean 
    */ 
    private $aktif; 

    /** 
    * @var \DateTime 
    */ 
    private $timestamp; 

    /** 
    * @var string 
    */ 
    private $operator; 

    private $username; 

    /** 
    * Set id 
    * 
    * @param integer $id 
    * @return MstPelajar 
    */ 
    public function setId($id) 
    { 
     $this->id = $id; 

     return $this; 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 


    /** 
    * Set nis 
    * 
    * @param string $nis 
    * @return MstPelajar 
    */ 
    public function setNis($nis) 
    { 
     $this->nis = $nis; 

     return $this; 
    } 

    /** 
    * Get nis 
    * 
    * @return string 
    */ 
    public function getNis() 
    { 
     return $this->nis; 
    } 

    /** 
    * Set password 
    * 
    * @param string $password 
    * @return MstPelajar 
    */ 
    public function setPassword($password) 
    { 
     $this->password = $password; 

     return $this; 
    } 

    /** 
    * Get password 
    * 
    * @return string 
    */ 
    public function getPassword() 
    { 
     return $this->password; 
    } 

    /** 
    * Set salt 
    * 
    * @param string $salt 
    * @return MstPelajar 
    */ 
    public function setSalt($salt) 
    { 
     $this->salt = $salt; 

     return $this; 
    } 

    /** 
    * Get salt 
    * 
    * @return string 
    */ 
    public function getSalt() 
    { 
     return $this->salt; 
    } 

    public function __construct() 
    { 
     $this->aktif = true; 
     // may not be needed, see section on salt below 
     // $this->salt = md5(uniqid(null, true)); 
    } 

    public function getUsername() 
    { 
     return $this->nis; 
    } 

    public function getRoles() 
    { 
     return array('ROLE_USER'); 
    } 

    public function eraseCredentials() 
    { 
    } 

    public function serialize() 
    { 
     return serialize(array(
      $this->id, 
      $this->nis, 
      $this->password, 
      // see section on salt below 
      // $this->salt, 
     )); 
    } 

    public function unserialize($serialized) 
    { 
     list (
      $this->id, 
      $this->nis, 
      $this->password, 
      // see section on salt below 
      // $this->salt 
     ) = unserialize($serialized); 
    } 
} 

用戶實體幾乎相同的管理員,我會讓管理員登錄工作的首位拿到後問題。我只是在頂部顯示重要的領域。只是評論如果需要另一個領域。

這裏我對管理DefaultController:

<?php 

namespace Sifo\AdminBundle\Controller; 

use Symfony\Component\HttpFoundation\Request; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Symfony\Component\Security\Core\SecurityContext; 
use Symfony\Component\Security\Core\Exception\AccessDeniedException; 

use Sifo\AdminBundle\Form\DefaultType; 

class DefaultController extends Controller 
{ 

public function indexAction() 
    { 
     return $this->render('SifoAdminBundle:Default:index.html.twig'); 
    } 

public function loginAction() 
    { 
     $request = $this->getRequest(); 
     $session = $request->getSession(); 

     // get the login error if there is one 
     if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) { 
      $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR); 
     } else { 
      $error = $session->get(SecurityContext::AUTHENTICATION_ERROR); 
      $session->remove(SecurityContext::AUTHENTICATION_ERROR); 
     } 

     return $this->render('SifoAdminBundle:Default:login.html.twig', array(
      // last username entered by the user 
      'last_username' => $session->get(SecurityContext::LAST_USERNAME), 
      'error'   => $error, 
     )); 
    } 
} 

謝謝你,如果需要其他信息只是發表評論或留言給我。我非常感謝你的幫助。

回答

2

我只是解決了我的問題。我需要在我的防火牆中添加提供程序。它會看起來像這樣:

security: 
    encoders: 
     #Symfony\Component\Security\Core\User\User: plaintext 
     Sifo\AdminBundle\Entity\MstPelajar: plaintext 
     Sifo\AdminBundle\Entity\MstPegawai: plaintext 

    role_hierarchy: 
     #ROLE_ADMIN:  ROLE_USER 
     #ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH] 

    providers: 
     #in_memory: 
     # memory: 
     #  users: 
     #   user: { password: userpass, roles: [ 'ROLE_USER' ] } 
     #   admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] } 
     admin_area: 
      entity: { class: SifoAdminBundle:MstPegawai, property: na } 
     user_area: 
      entity: { class: SifoAdminBundle:MstPelajar, property: nis } 

    firewalls: 
     dev: 
      pattern: ^/(_(profiler|wdt)|css|images|js)/ 
      security: false 
      anonymous: true 
     alogin: 
      pattern: ^/admin/login$ 
      security: false 
      anonymous: true 
     ulogin: 
      pattern: ^/user/login$ 
      security: false 
      anonymous: true 
     admin_area: 
      pattern: ^/admin 
      provider: admin_area 
      anonymous: false 
      form_login: 
       check_path: /admin/login_check 
       login_path: /admin/login 
      logout: 
       path: /admin/logout 
       target: /admin 
     user_area: 
      pattern: ^/user 
      provider: user_area 
      anonymous: false 
      form_login: 
       check_path: /user/login_check 
       login_path: /user/login 
      logout: 
       path: /user/logout 
       target: /user 

    access_control: 
     - { path: ^/admin/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/admin/, roles: ROLE_ADMIN } 
     - { path: ^/user/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } 
     - { path: ^/user/, roles: ROLE_USER } 

希望可以幫助有同樣問題的人。謝謝。