0

多くの開発者が FOSuser を使用するようにアドバイスしてくれた FOSuser のインストールと構成を 3 時間かけて試みています。実際には、FOS を使用せずに通常のログイン フォームを作成したかったのですが、多くの問題がありました。ドキュメントのすべての手順に従いました。 . インストールは問題ありませんでしたが、構成もログインしようとするたびに「Bad credentials」と表示されます。 . どういうわけか機能しますが、私が書いたものでのみ機能します。つまり、登録フォームにユーザーを登録してログインしようとすると、「Bad credentials」と表示されます。私のUsers.phpには、ログインするためのすべてのユーザー情報があります...

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,
        ));
    }
4

1 に答える 1

0

私は間違っているかもしれませんが、FOSUserBundle では、フォーム登録を使用する場合、作成後にユーザーをアクティブ化する必要があると思います。送信され、リンクが記載された電子メールが送信されると思います。app/console fos:user:activateメールが無ければアクティベートに使えると思います。

于 2013-07-20T10:50:09.223 に答える