forked from mitchellvanw/laravel-doctrine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoctrineUserProvider.php
123 lines (108 loc) · 3 KB
/
DoctrineUserProvider.php
1
2
3
4
5
6
7
8
9
10
11
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php namespace Mitch\LaravelDoctrine;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\UserProviderInterface;
use Illuminate\Hashing\HasherInterface;
class DoctrineUserProvider implements UserProviderInterface
{
/**
* @var HasherInterface
*/
private $hasher;
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var string
*/
private $entity;
/**
* @param HasherInterface $hasher
* @param EntityManager $entityManager
* @param $entity
*/
public function __construct(HasherInterface $hasher, EntityManager $entityManager, $entity)
{
$this->hasher = $hasher;
$this->entityManager = $entityManager;
$this->entity = $entity;
}
/**
* Retrieve a user by their unique identifier.
* @param mixed $identifier
* @return UserInterface|null
*/
public function retrieveById($identifier)
{
return $this->getRepository()->find($identifier);
}
/**
* Retrieve a user by by their unique identifier and "remember me" token.
* @param mixed $identifier
* @param string $token
* @return UserInterface|null
*/
public function retrieveByToken($identifier, $token)
{
$entity = $this->getEntity();
return $this->getRepository()->findOneBy([
$entity->getKeyName() => $identifier,
$entity->getRememberTokenName() => $token
]);
}
/**
* Update the "remember me" token for the given user in storage.
* @param UserInterface $user
* @param string $token
* @return void
*/
public function updateRememberToken(UserInterface $user, $token)
{
$user->setRememberToken($token);
$this->entityManager->persist($user);
$this->entityManager->flush();
}
/**
* Retrieve a user by the given credentials.
* @param array $credentials
* @return UserInterface|null
*/
public function retrieveByCredentials(array $credentials)
{
$criteria = [];
foreach ($credentials as $key => $value)
if ( ! str_contains($key, 'password'))
$criteria[$key] = $value;
return $this->getRepository()->findOneBy($criteria);
}
/**
* Validate a user against the given credentials.
* @param UserInterface $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(UserInterface $user, array $credentials)
{
return $this->hasher->check($credentials['password'], $user->getAuthPassword());
}
/**
* Returns repository for the entity.
*
* @return EntityRepository
*/
private function getRepository()
{
return $this->entityManager->getRepository($this->entity);
}
/**
* Returns instantiated entity.
*
* @return mixed
*/
private function getEntity()
{
return new $this->entity;
}
}