|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_auth_sources\Sources\Memory; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_auth_sources\AuthSourcesException; |
|
7
|
|
|
use kalanis\kw_auth_sources\Interfaces; |
|
8
|
|
|
use kalanis\kw_auth_sources\Traits\TLang; |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class Accounts |
|
13
|
|
|
* @package kalanis\kw_auth_sources\Sources\Memory |
|
14
|
|
|
* Authenticate class in memory |
|
15
|
|
|
*/ |
|
16
|
|
|
class Accounts implements Interfaces\IAuth, Interfaces\IWorkAccounts |
|
17
|
|
|
{ |
|
18
|
|
|
use TLang; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Interfaces\IHashes */ |
|
21
|
|
|
protected $mode = null; |
|
22
|
|
|
/** @var Interfaces\IUser[] */ |
|
23
|
|
|
protected $local = []; |
|
24
|
|
|
/** @var array<string, string> */ |
|
25
|
|
|
protected $pass = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param Interfaces\IHashes $mode |
|
29
|
|
|
* @param Interfaces\IUser[] $initial |
|
30
|
|
|
* @param Interfaces\IKAusTranslations|null $lang |
|
31
|
|
|
*/ |
|
32
|
13 |
|
public function __construct(Interfaces\IHashes $mode, array $initial = [], ?Interfaces\IKAusTranslations $lang = null) |
|
33
|
|
|
{ |
|
34
|
13 |
|
$this->setAusLang($lang); |
|
35
|
13 |
|
$this->mode = $mode; |
|
36
|
13 |
|
$this->local = $initial; |
|
37
|
13 |
|
foreach ($this->local as $item) { |
|
38
|
8 |
|
$this->pass[$item->getAuthId()] = 'undefined now'; |
|
39
|
|
|
} |
|
40
|
13 |
|
} |
|
41
|
|
|
|
|
42
|
4 |
|
public function authenticate(string $userName, array $params = []): ?Interfaces\IUser |
|
43
|
|
|
{ |
|
44
|
4 |
|
if (!isset($params['password'])) { |
|
45
|
1 |
|
throw new AuthSourcesException($this->getAusLang()->kauPassMustBeSet()); |
|
46
|
|
|
} |
|
47
|
3 |
|
$user = $this->getDataOnly($userName); |
|
48
|
3 |
|
if ($user && isset($params['password']) && isset($this->pass[$user->getAuthId()])) { |
|
49
|
2 |
|
if ($this->mode->checkHash(strval($params['password']), $this->pass[$user->getAuthId()])) { |
|
50
|
2 |
|
return clone $user; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
3 |
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
9 |
|
public function getDataOnly(string $userName): ?Interfaces\IUser |
|
57
|
|
|
{ |
|
58
|
9 |
|
foreach ($this->local as $local) { |
|
59
|
6 |
|
if ($local->getAuthName() == $userName) { |
|
60
|
6 |
|
return clone $local; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
6 |
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
3 |
|
public function createAccount(Interfaces\IUser $user, string $password): bool |
|
68
|
|
|
{ |
|
69
|
3 |
|
foreach ($this->local as $local) { |
|
70
|
2 |
|
if ($local->getAuthName() == $user->getAuthName()) { |
|
71
|
1 |
|
return false; |
|
72
|
|
|
} |
|
73
|
2 |
|
if ($local->getAuthId() == $user->getAuthId()) { |
|
74
|
1 |
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
$this->local[] = $user; |
|
79
|
2 |
|
$this->pass[$user->getAuthId()] = $this->mode->createHash($password); |
|
80
|
2 |
|
return true; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
1 |
|
public function readAccounts(): array |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->local; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
2 |
|
public function updateAccount(Interfaces\IUser $user): bool |
|
89
|
|
|
{ |
|
90
|
2 |
|
foreach ($this->local as $local) { |
|
91
|
1 |
|
if ($local->getAuthId() == $user->getAuthId()) { |
|
92
|
1 |
|
$local->setUserData( |
|
93
|
1 |
|
$local->getAuthId(), |
|
94
|
1 |
|
$user->getAuthName(), |
|
95
|
1 |
|
$user->getGroup(), |
|
96
|
1 |
|
$user->getClass(), |
|
97
|
1 |
|
$user->getStatus(), |
|
98
|
1 |
|
$user->getDisplayName(), |
|
99
|
1 |
|
$user->getDir(), |
|
100
|
1 |
|
$user->getExtra() |
|
101
|
|
|
); |
|
102
|
1 |
|
return true; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
1 |
|
return false; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
2 |
|
public function updatePassword(string $userName, string $passWord): bool |
|
109
|
|
|
{ |
|
110
|
2 |
|
$user = $this->getDataOnly($userName); |
|
111
|
2 |
|
if ($user && isset($this->pass[$user->getAuthId()])) { |
|
112
|
1 |
|
$this->pass[$user->getAuthId()] = $this->mode->createHash($passWord); |
|
113
|
|
|
} |
|
114
|
2 |
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
public function deleteAccount(string $userName): bool |
|
118
|
|
|
{ |
|
119
|
2 |
|
$willDelete = null; |
|
120
|
2 |
|
$use = []; |
|
121
|
2 |
|
foreach ($this->local as $local) { |
|
122
|
1 |
|
if ($local->getAuthName() == $userName) { |
|
123
|
1 |
|
$willDelete = $local; |
|
124
|
|
|
} else { |
|
125
|
1 |
|
$use[] = $local; |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
2 |
|
$this->local = $use; |
|
129
|
|
|
|
|
130
|
2 |
|
if ($willDelete && isset($this->pass[$willDelete->getAuthId()])) { |
|
131
|
1 |
|
unset($this->pass[$willDelete->getAuthId()]); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
2 |
|
return !empty($willDelete); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|