Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 OAuth2 provider error UserRepository

I'm new in Symfony framework and using tutorial http://blog.tankist.de/blog/2013/07/17/oauth2-explained-part-2-setting-up-oauth2-with-symfony2-using-fosoauthserverbundle/ getting error:

ClassNotFoundException: Attempted to load class "UserRepository" from namespace "Acme\DemoBundle\Repository" in C:\xampp\htdocs\*****\vendor\doctrine\orm\lib\Doctrine\ORM\Repository\DefaultRepositoryFactory.php line 75. Do you need to "use" it from another namespace?

I think that code can cause this error:

<!-- src/Acme/DemoBundle/Resources/config/services.xml -->
<parameters>
    <parameter key="platform.entity.user.class">Acme\DemoBundle\Entity\User</parameter>
    <parameter key="platform.user.provider.class">Acme\DemoBundle\Provider\UserProvider</parameter>
</parameters>

<services>
    <service id="platform.user.manager" class="Doctrine\ORM\EntityManager"
             factory-service="doctrine" factory-method="getManagerForClass">
        <argument>%platform.entity.user.class%</argument>
    </service>

    <service id="platform.user.repository"
             class="Acme\DemoBundle\Repository\UserRepository"
             factory-service="platform.user.manager" factory-method="getRepository">
        <argument>%platform.entity.user.class%</argument>
    </service>

    <service id="platform.user.provider" class="%platform.user.provider.class%">
        <argument type="service" id="platform.user.repository" />
    </service>
</services>

OR

<?php
  // src/Acme/DemoBundle/Provider/UserProvider.php

  namespace Acme\DemoBundle\Provider;

  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\Common\Persistence\ObjectRepository;
  use Doctrine\ORM\NoResultException;

  class UserProvider implements UserProviderInterface
  {
      protected $userRepository;

      public function __construct(ObjectRepository $userRepository){
          $this->userRepository = $userRepository;
      }

      public function loadUserByUsername($username)
      {
          $q = $this->userRepository
              ->createQueryBuilder('u')
              ->where('u.username = :username OR u.email = :email')
              ->setParameter('username', $username)
              ->setParameter('email', $username)
              ->getQuery();

          try {
              $user = $q->getSingleResult();
          } catch (NoResultException $e) {
              $message = sprintf(
                  'Unable to find an active admin AcmeDemoBundle: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->userRepository->find($user->getId());
      }

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

But I can not understand...

like image 246
altdovydas Avatar asked Feb 15 '23 08:02

altdovydas


1 Answers

I faced the same problem, here is what I did.

Create a file named UserRepository.PHP in Entity folder

Inside it, put this code

namespace Login\LoginBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * UserRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class UserRepository extends EntityRepository
{
}

Use your proper namespace, bundle name, class name etc. This worked for me, hope it will work for you too.

like image 125
AtanuCSE Avatar answered Feb 17 '23 11:02

AtanuCSE