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