1
|
<?php
|
2
|
|
3
|
namespace Illuminate\Auth;
|
4
|
|
5
|
use Illuminate\Support\Str;
|
6
|
use Illuminate\Contracts\Auth\UserProvider;
|
7
|
use Illuminate\Database\ConnectionInterface;
|
8
|
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
|
9
|
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
|
10
|
|
11
|
class DatabaseUserProvider implements UserProvider
|
12
|
{
|
13
|
/**
|
14
|
* The active database connection.
|
15
|
*
|
16
|
* @var \Illuminate\Database\ConnectionInterface
|
17
|
*/
|
18
|
protected $conn;
|
19
|
|
20
|
/**
|
21
|
* The hasher implementation.
|
22
|
*
|
23
|
* @var \Illuminate\Contracts\Hashing\Hasher
|
24
|
*/
|
25
|
protected $hasher;
|
26
|
|
27
|
/**
|
28
|
* The table containing the users.
|
29
|
*
|
30
|
* @var string
|
31
|
*/
|
32
|
protected $table;
|
33
|
|
34
|
/**
|
35
|
* Create a new database user provider.
|
36
|
*
|
37
|
* @param \Illuminate\Database\ConnectionInterface $conn
|
38
|
* @param \Illuminate\Contracts\Hashing\Hasher $hasher
|
39
|
* @param string $table
|
40
|
* @return void
|
41
|
*/
|
42
|
public function __construct(ConnectionInterface $conn, HasherContract $hasher, $table)
|
43
|
{
|
44
|
$this->conn = $conn;
|
45
|
$this->table = $table;
|
46
|
$this->hasher = $hasher;
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* Retrieve a user by their unique identifier.
|
51
|
*
|
52
|
* @param mixed $identifier
|
53
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
54
|
*/
|
55
|
public function retrieveById($identifier)
|
56
|
{
|
57
|
$user = $this->conn->table($this->table)->find($identifier);
|
58
|
|
59
|
return $this->getGenericUser($user);
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* Retrieve a user by their unique identifier and "remember me" token.
|
64
|
*
|
65
|
* @param mixed $identifier
|
66
|
* @param string $token
|
67
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
68
|
*/
|
69
|
public function retrieveByToken($identifier, $token)
|
70
|
{
|
71
|
$user = $this->conn->table($this->table)
|
72
|
->where('id', $identifier)
|
73
|
->where('remember_token', $token)
|
74
|
->first();
|
75
|
|
76
|
return $this->getGenericUser($user);
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* Update the "remember me" token for the given user in storage.
|
81
|
*
|
82
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
83
|
* @param string $token
|
84
|
* @return void
|
85
|
*/
|
86
|
public function updateRememberToken(UserContract $user, $token)
|
87
|
{
|
88
|
$this->conn->table($this->table)
|
89
|
->where('id', $user->getAuthIdentifier())
|
90
|
->update(['remember_token' => $token]);
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Retrieve a user by the given credentials.
|
95
|
*
|
96
|
* @param array $credentials
|
97
|
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
98
|
*/
|
99
|
public function retrieveByCredentials(array $credentials)
|
100
|
{
|
101
|
// First we will add each credential element to the query as a where clause.
|
102
|
// Then we can execute the query and, if we found a user, return it in a
|
103
|
// generic "user" object that will be utilized by the Guard instances.
|
104
|
$query = $this->conn->table($this->table);
|
105
|
|
106
|
foreach ($credentials as $key => $value) {
|
107
|
if (! Str::contains($key, 'password')) {
|
108
|
$query->where($key, $value);
|
109
|
}
|
110
|
}
|
111
|
|
112
|
// Now we are ready to execute the query to see if we have an user matching
|
113
|
// the given credentials. If not, we will just return nulls and indicate
|
114
|
// that there are no matching users for these given credential arrays.
|
115
|
$user = $query->first();
|
116
|
|
117
|
return $this->getGenericUser($user);
|
118
|
}
|
119
|
|
120
|
/**
|
121
|
* Get the generic user.
|
122
|
*
|
123
|
* @param mixed $user
|
124
|
* @return \Illuminate\Auth\GenericUser|null
|
125
|
*/
|
126
|
protected function getGenericUser($user)
|
127
|
{
|
128
|
if ($user !== null) {
|
129
|
return new GenericUser((array) $user);
|
130
|
}
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Validate a user against the given credentials.
|
135
|
*
|
136
|
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
137
|
* @param array $credentials
|
138
|
* @return bool
|
139
|
*/
|
140
|
public function validateCredentials(UserContract $user, array $credentials)
|
141
|
{
|
142
|
$plain = $credentials['password'];
|
143
|
|
144
|
return $this->hasher->check($plain, $user->getAuthPassword());
|
145
|
}
|
146
|
}
|