UserProvider.php
4.63 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
namespace FootyRoom\Services\Auth;
use FootyRoom\User\User;
use FootyRoom\Repositories\IUserRepository;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider as UserProviderContract;
use Illuminate\Session\SessionManager;
class UserProvider implements UserProviderContract
{
public function __construct(SessionManager $session, IUserRepository $repo)
{
$this->session = $session;
$this->repo = $repo;
}
/**
* Retrieve a user by their unique identifier.
*
* @param mixed $identifier
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveById($identifier)
{
return $this->repo->findById($identifier);
}
public function retrieveBySession($name)
{
$raw = $this->session->get($name);
$user = new User($raw);
return $user;
}
/**
* Retrieve a user by by their unique identifier and "remember me" token.
*
* @param mixed $identifier
* @param string $token
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByToken($identifier, $token)
{
}
/**
* Update the "remember me" token for the given user in storage.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param string $token
* @return void
*/
public function updateRememberToken(Authenticatable $user, $token)
{
}
/**
* Retrieve a user by the given credentials.
*
* @param array $credentials
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function retrieveByCredentials(array $credentials)
{
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// User "model" that will be utilized by the Guard instances.
return $this->repo->findOne(function ($query) use ($credentials) {
foreach ($credentials as $key => $value) {
if (str_contains($key, 'password') || str_contains($key, 'passwordHash')) {
continue;
}
$query->where($key, $value);
}
});
}
/**
* Validate a user against the given credentials.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param array $credentials
* @return bool
*/
public function validateCredentials(Authenticatable $user, array $credentials)
{
if (isset($credentials['password'])) {
return $user->comparePassword($credentials['password']);
}
return $credentials['passwordHash'] === $user->getPassword();
}
/**
* Records failed login attempt.
*
* @param Authenticatable $user
* @param string $ip
* @return void
*/
public function recordFailedAttempt(Authenticatable $user, $ip)
{
$this->repo->recordFailedLogin($this->getId($user), $ip);
// $user->recordFailedLogin($ip);
}
/**
* Clears number of successive failed logins.
*
* @param Authenticatable $user
* @return void
*/
public function clearFailedAttempts(Authenticatable $user)
{
$this->repo->clearFailedLogins($this->getId($user));
// $user->clearFailedLogins();
}
/**
* Decides whether user's account is open.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
*
* @return bool
*/
public function accountOpen($user)
{
return $user->accountOpen();
}
/**
* Returns number of failed attempts performed in a row.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
*
* @return int|null
*/
public function failedAttempts($user)
{
return $this->repo->failedAttempts($this->getId($user));
}
/**
* Get user's password.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
*
* @return string
*/
public function getPasswordHash($user)
{
return $user->getPassword();
}
/**
* Get user's id.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
*
* @return int
*/
public function getId($user)
{
return $user->getAuthIdentifier();
}
/**
* Turns user data into array.
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
*
* @return array
*/
public function toArray($user)
{
return (array) $user;
return $user->toArray();
}
}