1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AloiaCms\Auth; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
6
|
|
|
use Illuminate\Contracts\Auth\UserProvider; |
7
|
|
|
use Illuminate\Contracts\Hashing\Hasher as HasherContract; |
8
|
|
|
|
9
|
|
|
class AloiaCmsUserProvider implements UserProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The hasher implementation. |
13
|
|
|
* |
14
|
|
|
* @var \Illuminate\Contracts\Hashing\Hasher |
15
|
|
|
*/ |
16
|
|
|
protected $hasher; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The user model. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $model; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new Aloia CMS user provider. |
27
|
|
|
* |
28
|
|
|
* @param \Illuminate\Contracts\Hashing\Hasher $hasher |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function __construct(HasherContract $hasher, string $model) |
32
|
|
|
{ |
33
|
|
|
$this->hasher = $hasher; |
34
|
|
|
$this->model = $model; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function retrieveById($identifier) |
38
|
|
|
{ |
39
|
|
|
$user = $this |
40
|
|
|
->createModel() |
41
|
|
|
->findById($identifier); |
42
|
|
|
|
43
|
|
|
if ($user->exists()) { |
44
|
|
|
return $user; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function retrieveByToken($identifier, $token) |
51
|
|
|
{ |
52
|
|
|
$user = $this |
53
|
|
|
->createModel() |
54
|
|
|
->findById($identifier); |
55
|
|
|
|
56
|
|
|
if (!$user->exists()) { |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$rememberToken = $user->getRememberToken(); |
|
|
|
|
61
|
|
|
|
62
|
|
|
return $rememberToken && hash_equals($rememberToken, $token) |
|
|
|
|
63
|
|
|
? $user : null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function updateRememberToken(Authenticatable $user, $token) |
67
|
|
|
{ |
68
|
|
|
$user->setRememberToken($token); |
69
|
|
|
$user->save(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function retrieveByCredentials(array $credentials) |
73
|
|
|
{ |
74
|
|
|
$user = $this |
75
|
|
|
->createModel() |
76
|
|
|
->findById($credentials['email']); |
77
|
|
|
|
78
|
|
|
if ($user->exists()) { |
79
|
|
|
return $user; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function validateCredentials(Authenticatable $user, array $credentials) |
86
|
|
|
{ |
87
|
|
|
$plain = $credentials['password']; |
88
|
|
|
|
89
|
|
|
return $this->hasher->check($plain, $user->getAuthPassword()); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Create a new instance of the model. |
94
|
|
|
* |
95
|
|
|
* @return \AloiaCms\Models\Model |
96
|
|
|
*/ |
97
|
|
|
public function createModel() |
98
|
|
|
{ |
99
|
|
|
$class = '\\'.ltrim($this->model, '\\'); |
100
|
|
|
|
101
|
|
|
return new $class; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: