2013-07-19 61 views
0

我想從3小時開始安裝和配置FOSuser,許多開發人員建議我使用它。我實際上想做一個不使用FOS的正常登錄表單,但是我遇到了很多問題我遵循文檔中的所有步驟。安裝沒問題,配置也是每次當我嘗試登錄時,它顯示「Bad credentials」。所以我找到了我執行的這個命令:php app/console fos:user:create我給name-email-password 。它以某種方式工作,但只與我寫的東西有關,我的意思是當我在註冊表中註冊用戶並嘗試登錄時顯示「Bad credentials」。我希望我清楚了,請告訴我你需要知道什麼 Here是我Users.php在那裏我有我的所有用戶信息登錄...Fosuser錯誤憑據symfony2

namespace test\indexBundle\Document; 
use FOS\UserBundle\Model\User as BaseUser; 
use Symfony\Component\Security\Core\User\UserInterface; 

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; 

/** 
* 
* @MongoDB\Document 
*/ 

class Users extends BaseUser 
{ 


    /** 
    * @MongoDB\Id 
    */ 
    protected $id; 

    /** 
    * @MongoDB\String 
    */ 
    protected $userId; 

    /** 
    * @MongoDB\String 
    */ 
    protected $userEmail; 

    /** 
    * @MongoDB\String 
    */ 
    protected $userPassword; 


    /** 
    * @MongoDB\String 
    */ 
    protected $salt; 


    /** 
    * @MongoDB\Int 
    */ 
    protected $isActive; 


    public function __construct() 
    { 
     parent::__construct(); 
     $this->isActive = true; 
     $this->salt = md5(uniqid(null, true)); 
    } 


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

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


    /** 
    * Set userId 
    * 
    * @param string $userId 
    */ 
    public function setUserId() 
    { 
     $this->userId = $this->salt; 
    } 

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

    /** 
    * Set userName 
    * 
    * @param string $userName 
    */ 
    public function setUserName($userName) 
    { 
     $this->userName = $userName; 
    } 

    /** 
    * Get userName 
    * 
    * @return string $userName 
    */ 
    public function getUserName() 
    { 
     return $this->username; 
    } 


    /** 
    * Set userEmail 
    * 
    * @param string $userEmail 
    */ 
    public function setUserEmail($userEmail) 
    { 
     $this->userEmail = $userEmail; 
    } 

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


    /** 
    * Set userPassword 
    * 
    * @param string $userPassword 
    */ 
    public function setPassword($userPassword) 
    { 
     $this->userPassword = $userPassword; 
    } 

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

    /** 
    * @inheritDoc 
    */ 
    public function getSalt() 
    { 
     return ''; 
    } 



    /** 
    * @inheritDoc 
    */ 
    public function getRoles() 
    { 
     return array('ROLE_USER'); 
    } 

    /** 
    * @inheritDoc 
    */ 
    public function eraseCredentials() 
    { 
    } 

    /** 
    * @see \Serializable::serialize() 
    */ 
    public function serialize() 
    { 
     return serialize(array(
      $this->id 
     )); 
    } 

    /** 
    * @see \Serializable::unserialize() 
    */ 
    public function unserialize($serialized) 
    { 
     list (
      $this->id 
     ) = unserialize($serialized); 
    } 
} 

這裏我security.yml:

jms_security_extra: 
    secure_all_services: false 
    expressions: true 

security: 
    encoders: 

     FOS\UserBundle\Model\UserInterface: sha512 
     test\indexBundle\Document\Users: 
      algorithm:  sha1 
      encode_as_base64: false 
      iterations:  1 


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


    providers: 


     fos_userbundle: 
      id: fos_user.user_provider.username_email 

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

     main: 
      pattern: ^/ 
      anonymous: true 


      form_login: 
       check_path: /login_check 
       login_path: /login 
       provider: fos_userbundle 
       post_only: true 
       use_forward: false 
       username_parameter:    email 
       password_parameter:    password 
       failure_path:     null 
       failure_forward:    false 
       target_path_parameter: redirect_url 
      logout: 
       path: /logout 
       target: /blog 


    access_control: 
     - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY } 

和登錄功能:

public function loginAction() 
    { 

     $request = $this->getRequest(); 
     $session = $request->getSession(); 




     if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')) 
     { 
      return $this->redirect($this->generateUrl('index_homepage')); 
     } 



     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('indexBundle:index:logIn.html.twig', array(
       'last_username' => $session->get(SecurityContext::LAST_USERNAME), 
       'error'   => $error, 
     )); 
    } 

回答

0

我可能b e錯誤,但我認爲FOSUserBundle要求用戶在創建之後被激活,如果您使用表單註冊,它會發送出去並通過電子郵件發送給我相信的鏈接。我認爲如果沒有電子郵件,您可以使用app/console fos:user:activate激活。

+0

其實我是這樣做的:我有一個註冊表格,我做了,這個表格發送數據到我的Users.php(我的表在MongoDB:用戶),我必須登錄後,我也做了一個表單沒有FOSUser佈局。所以當我嘗試從我的Users表登錄時,這個登錄表單不會顯示任何內容,我不知道我的錯誤在哪裏? – Momo1987

+0

我不確定你的意思,但'__construct'中的asbtract'User'模型集啓用爲false,所以除非您在註冊時特別啓用用戶,否則將無法訪問。 – qooplmao