UserRepository.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Entity\Repository;
  3. use Doctrine\ORM\EntityRepository;
  4. use Symfony\Component\Security\Core\User\UserInterface;
  5. use Symfony\Component\Security\Core\User\UserProviderInterface;
  6. use Doctrine\ORM\NoResultException;
  7. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  8. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  9. /**
  10. * Class UserRepository
  11. * @package Entity\Repository
  12. */
  13. class UserRepository extends EntityRepository implements UserProviderInterface
  14. {
  15. /**
  16. * @param string $keyword
  17. * @return mixed
  18. */
  19. public function searchUserByKeyword($keyword)
  20. {
  21. $qb = $this->createQueryBuilder('a');
  22. // Selecting user info
  23. $qb->select('DISTINCT u');
  24. $qb->from('Entity\User', 'u');
  25. // Selecting courses for users
  26. // $qb->innerJoin('u.courses', 'c');
  27. //@todo check app settings
  28. $qb->add('orderBy', 'u.firstname ASC');
  29. $qb->where('u.firstname LIKE :keyword OR u.lastname LIKE :keyword OR u.username LIKE :keyword ');
  30. $qb->setParameter('keyword', "%$keyword%");
  31. $q = $qb->getQuery();
  32. return $q->execute();
  33. }
  34. /**
  35. * @param string $keyword
  36. * @param string $role
  37. * @return mixed
  38. */
  39. public function searchUserByKeywordAndRole($keyword, $role)
  40. {
  41. $qb = $this->createQueryBuilder('a');
  42. // Selecting user info
  43. $qb->select('DISTINCT u');
  44. $qb->from('Entity\User', 'u');
  45. // Selecting courses for users
  46. $qb->innerJoin('u.roles', 'r');
  47. //@todo check app settings
  48. $qb->add('orderBy', 'u.firstname ASC');
  49. $qb->where('u.firstname LIKE :keyword OR u.lastname LIKE :keyword OR u.username LIKE :keyword ');
  50. $qb->andWhere('r.role = :role');
  51. $qb->setParameters(
  52. array(
  53. 'keyword' => "%$keyword%",
  54. 'role' => $role
  55. )
  56. );
  57. $q = $qb->getQuery();
  58. return $q->execute();
  59. }
  60. /**
  61. * @param string $username
  62. * @return \Entity\User
  63. * @throws UsernameNotFoundException
  64. */
  65. public function loadUserByUsername($username)
  66. {
  67. $q = $this
  68. ->createQueryBuilder('u')
  69. ->where('u.username = :username OR u.email = :email')
  70. ->setParameter('username', $username)
  71. ->setParameter('email', $username)
  72. ->getQuery();
  73. try {
  74. $user = $q->getSingleResult();
  75. } catch (NoResultException $e) {
  76. throw new UsernameNotFoundException(
  77. sprintf('Unable to find an active admin User identified by "%s".', $username),
  78. 0,
  79. $e
  80. );
  81. }
  82. return $user;
  83. }
  84. /**
  85. * Refreshes the user for the account interface.
  86. *
  87. * It is up to the implementation if it decides to reload the user data
  88. * from the database, or if it simply merges the passed User into the
  89. * identity map of an entity manager.
  90. *
  91. * @throws UnsupportedUserException if the account is not supported
  92. * @param UserInterface $user
  93. *
  94. * @return UserInterface
  95. */
  96. public function refreshUser(UserInterface $user)
  97. {
  98. return $user;
  99. /*
  100. $class = get_class($user);
  101. if (!$this->supportsClass($class)) {
  102. throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class));
  103. }
  104. return $this->loadUserByUsername($user->getUsername());*/
  105. }
  106. /**
  107. * @param string $class
  108. * @return bool
  109. */
  110. public function supportsClass($class)
  111. {
  112. return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
  113. }
  114. }