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