dongtang6718 2014-01-17 12:08
浏览 54
已采纳

Symfony 2必须实现UserProviderInterface

I get this error 'Must implement UserProviderInterface' when trying to using the symfony login features. I'm referencing the repository * @ORM\Entity(repositoryClass="Login\LogBundle\Entity\Repository\UserRepository") . Does anyone have any ideas.

Entity

  <?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);
  }







  }

Repository

      // 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());
}
}
  • 写回答

2条回答 默认 最新

  • donglin7979 2014-01-17 12:14
    关注

    Check the app/config/security.yml file with something like this:

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

    And path to repositoryClass UserRepository is wrong, maybe you mean this:

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

    becouse you use namespace \Login\LogBundle\Entity\UserRepository.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题