什么是从数据库中获取用户的正确security.yml文件

时间:2014-10-28 21:42:28

标签: php mysql symfony doctrine-orm

我的security.yml文件存在一些问题:

# you can read more about security in the related section of the documentation
# http://symfony.com/doc/current/book/security.html
security:
    # http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password
    encoders:
        #Symfony\Component\Security\Core\User\User: plaintext
        Login\Loginbundle\Entity\User: sha512

    # http://symfony.com/doc/current/book/security.html#hierarchical-roles
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
    providers:
        users:
            entity: { class: LoginLoginBundle:User, property: username }
        in_memory:
            memory:
                users:
                    user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                    admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }

    # the main part of the security, where you can set up firewalls
    # for specific sections of your app
    firewalls:
        secured_area:
            pattern:   ^/

            anonymous: ~
            form_login:
                provider: users
                login_path: login
                check_path: login_check
    access_control:
    - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }

我的网站有登录表单。用户位于数据库中。 我可以使用用户名:user和password:userpass登录,但是如何让它与数据库中的用户一起工作?

我已经阅读过有关UserInterfaces的内容,并且已经愚弄了它,没有成功。

也许用户实体是有用的,这里是:

<?php

namespace Login\LoginBundle\Entity;

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

/**
 * User
 */
class User implements UserInterface, \Serializable
{
    /**
     * @var string
     */
    private $username;

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

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

    /**
     * @var integer
     */
    private $money;

    /**
     * @var integer
     */
    private $userid;

    /**
     * @var \Login\LoginBundle\Entity\Team
     */
    private $teamTeamid;

    /**
     * @inheritDoc
     */
    public function getSalt()
    {
        // you *may* need a real salt depending on your encoder
        // see section on salt below
        return null;
    }

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

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

    /**
     * @see \Serializable::serialize()
     */
    public function serialize()
    {
        return serialize(array(
            $this->id,
            $this->username,
            $this->password,
            // see section on salt below
            // $this->salt,
        ));
    }

    /**
     * @see \Serializable::unserialize()
     */
    public function unserialize($serialized)
    {
        list (
            $this->id,
            $this->username,
            $this->password,
            // see section on salt below
            // $this->salt
        ) = unserialize($serialized);
    }

    /**
     * Set username
     *
     * @param string $username
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }



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

    /**
     * Set email
     *
     * @param string $email
     * @return User
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set money
     *
     * @param integer $money
     * @return User
     */
    public function setMoney($money)
    {
        $this->money = $money;

        return $this;
    }

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

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

    /**
     * Set teamTeamid
     *
     * @param \Login\LoginBundle\Entity\Team $teamTeamid
     * @return User
     */
    public function setTeamTeamid(\Login\LoginBundle\Entity\Team $teamTeamid = null)
    {
        $this->teamTeamid = $teamTeamid;

        return $this;
    }

    /**
     * Get teamTeamid
     *
     * @return \Login\LoginBundle\Entity\Team 
     */
    public function getTeamTeamid()
    {
        return $this->teamTeamid;
    }
}

编辑我的security.yml文件并访问数据库用户的正确方法是什么?

3 个答案:

答案 0 :(得分:0)

我无法理解这个问题。但是在symfony中处理用户的最简单方法是使用FOSUserBundle https://github.com/FriendsOfSymfony/FOSUserBundle

答案 1 :(得分:0)

您的函数serialize()unserialize($serialized)使用$this->id代替您之前在代码中使用的$this->useridSymfony2 docs解释了为什么序列化如此重要 - 请参阅&#34下的部分;序列化和反序列化的重要性是什么?&#34;

答案 2 :(得分:0)

您是否尝试使用数据库中的用户登录?

如果您尝试使用名称&#39; user&#39;并传递“用户通过”,这应该有效。

如果您想与数据库中的用户合作,您应该像这样编辑security.yml文件

providers:
        user:
            entity:
                class: Login\LoginBundle\Entity\User
                property: username