【问题标题】:Symfony 2 Must Implement UserProviderInterfaceSymfony 2 必须实现 UserProviderInterface
【发布时间】:2014-02-06 18:26:22
【问题描述】:

我收到此错误“必须实现 UserProviderInterface” 尝试使用 symfony 登录功能时。我正在引用存储库 * @ORM\Entity(repositoryClass="Login\LogBundle\Entity\Repository\UserRepository") 。有没有人有任何想法。

实体

  <?php
  namespace Login\LogBundle\Entity;

  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\ORM\Mapping as ORM;
  use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Component\Security\Core\User\UserProviderInterface;
  /**
  * @ORM\Table(name="user")
  * @ORM\Entity(repositoryClass="Login\LogBundle\Entity\Repository\UserRepository")
  * @ORM\HasLifecycleCallbacks()
  */
 class User implements UserInterface, \Serializable
 {
/**
 * @var integer
 */
private $id;

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

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

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

  /**
 * @ORM\Column(type="string", length=32)
 */
private $salt;

 /**
 * @ORM\Column(type="string", length=60, unique=true)
 */
private $email;

/**
 * @ORM\Column(name="is_active", type="boolean")
 */
private $isActive;

 /**
  /**
 * @OneToMany(targetEntity="userapidetails", mappedBy="user")
 */
public $userapidetails;

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

public function getUserapidetails() {
   return $this->userapidetails;
}

// public function setUserapidetails($userapidetails) {
//    $this->userapi=$userapidetails;

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

/**
 * 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 name
 *
 * @param string $name
 * @return User
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

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

/**
 * 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;
}
/**
 * @var string
 */
private $isactive;


/**
 * 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 salt
 *
 * @param string $salt
 * @return User
 */
public function setSalt($salt)
{
    $this->salt = $salt;

    return $this;
}

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

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

    return $this;
}

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




 /**
 * @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->salt,
        $this->password,
    ));
}

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







  }

存储库

      // src/Acme/UserBundle/Entity/UserRepository.php
    namespace Login\LogBundle\Entity;

   use Doctrine\ORM\EntityRepository;
   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\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());
}
}

【问题讨论】:

    标签: php symfony doctrine repository entity


    【解决方案1】:

    使用以下内容检查 app/config/security.yml 文件:

    # app/config/security.yml
    security:
        providers:
            main:
                entity:
                    class: Acme\UserBundle\Entity\User
                    property: username
    

    而repositoryClass UserRepository的路径是错误的,也许你的意思是这样的:

    @ORM\Entity(repositoryClass="Login\LogBundle\Entity\UserRepository")
    

    因为你使用命名空间\Login\LogBundle\Entity\UserRepository

    【讨论】:

    • @ORM\Entity(repositoryClass="Login\LogBundle\Entity\Repository\UserRepository")中的UserRepository命名空间修复为@ORM\Entity(repositoryClass="Login\LogBundle\Entity\UserRepository")
    • 这能解决您的问题吗?
    • 是的,谢谢,这是他们推荐提供者的方式: main: entity: { class: LoginLogBundle:User ,property: username}
    【解决方案2】:

    要从存储库加载用户,您应该从 security.yml 中省略 { property: username }。

    【讨论】:

    • 在 symfony >2.3 中是必需的,但在 symfony
    猜你喜欢
    • 1970-01-01
    • 2016-11-03
    • 2021-10-30
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多