簡體   English   中英

Symfony 2登錄表單無法登錄

[英]Symfony 2 login form cannot login

我正在創建一個連接到用戶表的登錄表單。 但是,當我提交登錄表單時,它會生成此消息“ 用戶提供程序必須返回UserInterface對象”。

我從user.orm.yml創建了用戶實體我錯過了什么?

Security.yml

安全:

encoders:
    ESS\UserBundle\Entity\User: 
        algorithm: plaintext
        encode-as-base64: true
        iterations: 1

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

providers:
    administrators:
        entity: { class: ESSUserBundle:User}

UserRepository類

namespace ESS\UserBundle\Repository;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;

class UserRepository extends EntityRepository implements UserProviderInterface
{
    public function loadUserByUsername($username)
    {
        $q = $this
            ->createQueryBuilder('u')
            ->where('u.username = :username OR u.email = :email')
            ->setParameter('username', $username)
            ->setParameter('email', $username)
            ->getQuery();

        try {
            // The Query::getSingleResult() method throws an exception
            // if there is no record matching the criteria.
            $user = $q->getSingleResult();
        } catch (NoResultException $e) {
            $message = sprintf(
                'Unable to find an active admin AcmeUserBundle:User object identified by "%s".',
                $username
            );
            throw new UsernameNotFoundException($message, 0, $e);
        }

        return $user;
    }

    public function refreshUser(UserInterface $user)
    {
        $class = get_class($user);
        if (!$this->supportsClass($class)) {
            throw new UnsupportedUserException(
                sprintf(
                    'Instances of "%s" are not supported.',
                    $class
                )
            );
        }

        return $this->find($user->getId());
    }

    public function supportsClass($class)
    {
        return $this->getEntityName() === $class
            || is_subclass_of($class, $this->getEntityName());
    }
}

用戶實體類

namespace ESS\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * User
 * 
 * 
 */
class User
    /**
     * @var integer
     */
    private $id;

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

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

問題是您的用戶實體本身需要實現Symfony2的用戶界面。 http://api.symfony.com/2.0/Symfony/Component/Security/Core/User/UserInterface.html

將其添加到實體文件的頂部。

use Symfony\Component\Security\Core\User\UserInterface; 



class User implements UserInterface {

然后在界面中實現方法。

Symfony的安全組件要求用戶實現此接口以便能夠對其進行身份驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM