UserMapper.php 2.41 KB
Newer Older
1
2
3
4
<?php

namespace UnicaenAuth\Service;

5
use Doctrine\ORM\EntityManagerInterface;
6
use UnicaenAuth\Entity\Db\AbstractUser;
7
8
9
use UnicaenAuth\Options\ModuleOptions;
use Zend\Hydrator\HydratorInterface;
use ZfcUser\Entity\UserInterface;
10

11
class UserMapper extends \ZfcUser\Mapper\User
12
{
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    //========== repris du module zf-commons/zfc-user-doctrine-orm abandonné =========
    /**
     * @var \Doctrine\ORM\EntityManagerInterface
     */
    protected $em;

    /**
     * @var ModuleOptions
     */
    protected $options;

    public function __construct(EntityManagerInterface $em, ModuleOptions $options)
    {
        $this->em      = $em;
        $this->options = $options;
    }

    public function findByEmail($email)
    {
        $er = $this->em->getRepository($this->options->getUserEntityClass());

        return $er->findOneBy(array('email' => $email));
    }

    public function findByUsername($username)
    {
        $er = $this->em->getRepository($this->options->getUserEntityClass());

        return $er->findOneBy(array('username' => $username));
    }

    public function findById($id)
    {
        $er = $this->em->getRepository($this->options->getUserEntityClass());

        return $er->find($id);
    }

    public function insert(UserInterface $entity, $tableName = null, HydratorInterface $hydrator = null)
    {
        return $this->persist($entity);
    }

    public function update(UserInterface $entity, $where = null, $tableName = null, HydratorInterface $hydrator = null)
    {
        return $this->persist($entity);
    }

    protected function persist($entity)
    {
        $this->em->persist($entity);
        $this->em->flush();

        return $entity;
    }
    //===================



72
73
74
75
76
77
78
79
80
81
82
83
84
85
    /**
     * Recherche un utilisateur par son username (identifiant de connexion).
     *
     * @param string $username
     * @return AbstractUser|null
     */
    public function findOneByUsername($username)
    {
        /** @var AbstractUser $user */
        $user = $this->em->getRepository($this->options->getUserEntityClass())->findOneBy(['username' => $username]);

        return $user;
    }

86
87
    /**
     * @param string $token
88
     * @return AbstractUser
89
90
91
     */
    public function findOneByPasswordResetToken($token)
    {
92
        /** @var AbstractUser $user */
93
94
95
96
97
        $user = $this->em->getRepository($this->options->getUserEntityClass())->findOneBy(['passwordResetToken' => $token]);

        return $user;
    }
}