1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Auth;
|
4
|
|
5
|
use Illuminate\Support\Str;
|
6
|
use Illuminate\Contracts\Auth\UserProvider;
|
7
|
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
|
8
|
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
|
9
|
|
10
|
class EloquentUserProvider implements UserProvider
|
11
|
{
|
12
|
/**
|
13
|
* The hasher implementation.
|
14
|
*
|
15
|
* @var \Illuminate\Contracts\Hashing\Hasher
|
16
|
*/
|
17
|
protected $hasher;
|
18
|
|
19
|
/**
|
20
|
* The Eloquent user model.
|
21
|
*
|
22
|
* @var string
|
23
|
*/
|
24
|
protected $model;
|
25
|
|
26
|
/**
|
27
|
* Create a new database user provider.
|
28
|
*
|
29
|
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
|
30
|
* @param string $model
|
31
|
* @return void
|
32
|
*/
|
33
|
public function __construct(HasherContract $hasher, $model)
|
34
|
{
|
35
|
$this->model = $model;
|
36
|
$this->hasher = $hasher;
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Retrieve a user by their unique identifier.
|
41
|
*
|
42
|
* @param mixed $identifier
|
43
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
44
|
*/
|
45
|
public function retrieveById($identifier)
|
46
|
{
|
47
|
return $this->createModel()->newQuery()->find($identifier);
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Retrieve a user by their unique identifier and "remember me" token.
|
52
|
*
|
53
|
* @param mixed $identifier
|
54
|
* @param string $token
|
55
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
56
|
*/
|
57
|
public function retrieveByToken($identifier, $token)
|
58
|
{
|
59
|
$model = $this->createModel();
|
60
|
|
61
|
return $model->newQuery()
|
62
|
->where($model->getAuthIdentifierName(), $identifier)
|
63
|
->where($model->getRememberTokenName(), $token)
|
64
|
->first();
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* Update the "remember me" token for the given user in storage.
|
69
|
*
|
70
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
71
|
* @param string $token
|
72
|
* @return void
|
73
|
*/
|
74
|
public function updateRememberToken(UserContract $user, $token)
|
75
|
{
|
76
|
$user->setRememberToken($token);
|
77
|
|
78
|
$user->save();
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Retrieve a user by the given credentials.
|
83
|
*
|
84
|
* @param array $credentials
|
85
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
86
|
*/
|
87
|
public function retrieveByCredentials(array $credentials)
|
88
|
{
|
89
|
if (empty($credentials)) {
|
90
|
return;
|
91
|
}
|
92
|
|
93
|
// First we will add each credential element to the query as a where clause.
|
94
|
// Then we can execute the query and, if we found a user, return it in a
|
95
|
// Eloquent User "model" that will be utilized by the Guard instances.
|
96
|
$query = $this->createModel()->newQuery();
|
97
|
|
98
|
foreach ($credentials as $key => $value) {
|
99
|
if (! Str::contains($key, 'password')) {
|
100
|
$query->where($key, $value);
|
101
|
}
|
102
|
}
|
103
|
|
104
|
return $query->first();
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* Validate a user against the given credentials.
|
109
|
*
|
110
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
111
|
* @param array $credentials
|
112
|
* @return bool
|
113
|
*/
|
114
|
public function validateCredentials(UserContract $user, array $credentials)
|
115
|
{
|
116
|
$plain = $credentials['password'];
|
117
|
|
118
|
return $this->hasher->check($plain, $user->getAuthPassword());
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* Create a new instance of the model.
|
123
|
*
|
124
|
* @return \Illuminate\Database\Eloquent\Model
|
125
|
*/
|
126
|
public function createModel()
|
127
|
{
|
128
|
$class = '\\'.ltrim($this->model, '\\');
|
129
|
|
130
|
return new $class;
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Gets the hasher implementation.
|
135
|
*
|
136
|
* @return \Illuminate\Contracts\Hashing\Hasher
|
137
|
*/
|
138
|
public function getHasher()
|
139
|
{
|
140
|
return $this->hasher;
|
141
|
}
|
142
|
|
143
|
/**
|
144
|
* Sets the hasher implementation.
|
145
|
*
|
146
|
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
|
147
|
* @return $this
|
148
|
*/
|
149
|
public function setHasher(HasherContract $hasher)
|
150
|
{
|
151
|
$this->hasher = $hasher;
|
152
|
|
153
|
return $this;
|
154
|
}
|
155
|
|
156
|
/**
|
157
|
* Gets the name of the Eloquent user model.
|
158
|
*
|
159
|
* @return string
|
160
|
*/
|
161
|
public function getModel()
|
162
|
{
|
163
|
return $this->model;
|
164
|
}
|
165
|
|
166
|
/**
|
167
|
* Sets the name of the Eloquent user model.
|
168
|
*
|
169
|
* @param string $model
|
170
|
* @return $this
|
171
|
*/
|
172
|
public function setModel($model)
|
173
|
{
|
174
|
$this->model = $model;
|
175
|
|
176
|
return $this;
|
177
|
}
|
178
|
}
|