You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.7 KiB
132 lines
3.7 KiB
<?php
|
|
|
|
namespace Entity\Repository;
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
|
use Symfony\Component\Security\Core\User\UserInterface;
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
|
use Doctrine\ORM\NoResultException;
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
|
|
use \Entity\User;
|
|
|
|
/**
|
|
* Class UserRepository
|
|
* @package Entity\Repository
|
|
*/
|
|
class UserRepository extends EntityRepository implements UserProviderInterface
|
|
{
|
|
/**
|
|
* @param string $keyword
|
|
* @return mixed
|
|
*/
|
|
public function searchUserByKeyword($keyword)
|
|
{
|
|
$qb = $this->createQueryBuilder('a');
|
|
|
|
// Selecting user info
|
|
$qb->select('DISTINCT b');
|
|
|
|
$qb->from('Entity\User', 'b');
|
|
|
|
// Selecting courses for users
|
|
//$qb->innerJoin('u.courses', 'c');
|
|
|
|
//@todo check app settings
|
|
$qb->add('orderBy', 'b.firstname ASC');
|
|
$qb->where('b.firstname LIKE :keyword OR b.lastname LIKE :keyword ');
|
|
$qb->setParameter('keyword', "%$keyword%");
|
|
$query = $qb->getQuery();
|
|
|
|
return $query->execute();
|
|
}
|
|
|
|
/**
|
|
* @param string $username
|
|
* @return \Entity\User
|
|
* @throws UsernameNotFoundException
|
|
*/
|
|
public function loadUserByUsername($username)
|
|
{
|
|
$query = $this
|
|
->createQueryBuilder('u')
|
|
->where('u.username = :username OR u.email = :email')
|
|
->leftJoin('u.roles', 'r')
|
|
->setParameter('username', $username)
|
|
->setParameter('email', $username)
|
|
->getQuery();
|
|
|
|
try {
|
|
$user = $query->getSingleResult();
|
|
} catch (NoResultException $e) {
|
|
throw new UsernameNotFoundException(
|
|
sprintf('Unable to find an active admin User identified by "%s".', $username),
|
|
0,
|
|
$e
|
|
);
|
|
}
|
|
return $user;
|
|
}
|
|
|
|
/**
|
|
* Refreshes the user for the account interface.
|
|
*
|
|
* It is up to the implementation if it decides to reload the user data
|
|
* from the database, or if it simply merges the passed User into the
|
|
* identity map of an entity manager.
|
|
*
|
|
* @throws UnsupportedUserException if the account is not supported
|
|
* @param UserInterface $user
|
|
*
|
|
* @return UserInterface
|
|
*/
|
|
public function refreshUser(UserInterface $user)
|
|
{
|
|
//return $user;
|
|
$class = get_class($user);
|
|
if (!$this->supportsClass($class)) {
|
|
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class));
|
|
}
|
|
return $this->loadUserByUsername($user->getUsername());
|
|
}
|
|
|
|
/**
|
|
* @param string $class
|
|
* @return bool
|
|
*/
|
|
public function supportsClass($class)
|
|
{
|
|
return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
|
|
}
|
|
|
|
/**
|
|
* Get course user relationship based in the course_rel_user table.
|
|
* @return array
|
|
*/
|
|
public function getCourses(User $user)
|
|
{
|
|
$queryBuilder = $this->createQueryBuilder('user');
|
|
|
|
// Selecting course info.
|
|
$queryBuilder->select('c');
|
|
|
|
// Loading User.
|
|
//$qb->from('Entity\User', 'u');
|
|
|
|
// Selecting course
|
|
$queryBuilder->innerJoin('Entity\Course', 'c');
|
|
|
|
//@todo check app settings
|
|
//$qb->add('orderBy', 'u.lastname ASC');
|
|
|
|
$wherePart = $queryBuilder->expr()->andx();
|
|
|
|
// Get only users subscribed to this course
|
|
$wherePart->add($queryBuilder->expr()->eq('user.userId', $user->getUserId()));
|
|
|
|
$queryBuilder->where($wherePart);
|
|
$query = $queryBuilder->getQuery();
|
|
|
|
return $query->execute();
|
|
}
|
|
}
|
|
|