UserRepository.php 1.41 KB
Newer Older
1
2
3
<?php

namespace PotageBundle\Repository;
4
5
use Symfony\Bridge\Doctrine\Security\User\UserLoaderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
6
7
8
9
10
11
12

/**
 * UserRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
13
class UserRepository extends \Doctrine\ORM\EntityRepository implements UserLoaderInterface
14
{
Mat's avatar
Mat committed
15
16
17
18
19
20
21
22
23
24
25
26
27
    /**
     * @param $id_utilisateur
     * @return mixed
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function findOneByUtilisateur($id_utilisateur)
    {
        $qb = $this->createQueryBuilder('fu')
            ->join('fu.utilisateur', 'u')
            ->where('u.id = :id')
            ->setParameter(':id', $id_utilisateur);
        return $qb->getQuery()->getOneOrNullResult();
    }
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    
    /**
     * Dans security, je ne peux me référer à une propriété, puisque token_key est un array sérialisé
     * cfr. https://symfony.com/doc/3.4/security/entity_provider.html#using-a-custom-query-to-load-the-user
     *
     * @param $tokenkey
     * @return UserInterface|null
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function loadUserByUsername($tokenkey)
    {
        return $this->createQueryBuilder('u')
            ->where('u.tokenKey LIKE :tokenkey')
            ->setParameter('tokenkey', '%'.$tokenkey.'%')
            ->getQuery()
            ->getOneOrNullResult();
    }
    
46
}