1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_auth\Sources\Files; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_auth\AuthException; |
7
|
|
|
use kalanis\kw_auth\Data\FileGroup; |
8
|
|
|
use kalanis\kw_auth\Interfaces\IAccessGroups; |
9
|
|
|
use kalanis\kw_auth\Interfaces\IGroup; |
10
|
|
|
use kalanis\kw_auth\Sources\TAuthLock; |
11
|
|
|
use kalanis\kw_locks\LockException; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait TGroups |
16
|
|
|
* @package kalanis\kw_auth\Sources\Files |
17
|
|
|
* Work with groups |
18
|
|
|
*/ |
19
|
|
|
trait TGroups |
20
|
|
|
{ |
21
|
|
|
use TAuthLock; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param IGroup $group |
25
|
|
|
* @throws AuthException |
26
|
|
|
* @throws LockException |
27
|
|
|
*/ |
28
|
9 |
|
public function createGroup(IGroup $group): void |
29
|
|
|
{ |
30
|
9 |
|
$userId = $group->getGroupAuthorId(); |
31
|
9 |
|
$groupName = $this->stripChars($group->getGroupName()); |
|
|
|
|
32
|
9 |
|
$groupDesc = $this->stripChars($group->getGroupDesc()); |
33
|
|
|
|
34
|
|
|
// not everything necessary is set |
35
|
9 |
|
if (empty($userId) || empty($groupName)) { |
36
|
4 |
|
throw new AuthException($this->getLang()->kauGroupMissParam()); |
37
|
|
|
} |
38
|
5 |
|
$this->checkLock(); |
39
|
|
|
|
40
|
5 |
|
$gid = 0; |
41
|
5 |
|
$this->getLock()->create(); |
42
|
|
|
|
43
|
|
|
// read groups |
44
|
|
|
try { |
45
|
5 |
|
$groupLines = $this->openGroups(); |
46
|
1 |
|
} catch (AuthException $ex) { |
47
|
|
|
// silence the problems on storage |
48
|
1 |
|
$groupLines = []; |
49
|
|
|
} |
50
|
5 |
|
foreach ($groupLines as &$line) { |
51
|
4 |
|
$gid = max($gid, $line[IAccessGroups::GRP_ID]); |
52
|
|
|
} |
53
|
5 |
|
$gid++; |
54
|
|
|
|
55
|
|
|
$newGroup = [ |
56
|
|
|
IAccessGroups::GRP_ID => $gid, |
57
|
|
|
IAccessGroups::GRP_NAME => $groupName, |
58
|
|
|
IAccessGroups::GRP_AUTHOR => $userId, |
59
|
5 |
|
IAccessGroups::GRP_DESC => !empty($groupDesc) ? $groupDesc : $groupName, |
60
|
|
|
IAccessGroups::GRP_FEED => '', |
61
|
|
|
]; |
62
|
5 |
|
ksort($newGroup); |
63
|
5 |
|
$groupLines[] = $newGroup; |
64
|
|
|
|
65
|
|
|
// now save it |
66
|
5 |
|
$this->saveGroups($groupLines); |
67
|
|
|
|
68
|
5 |
|
$this->getLock()->delete(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $groupId |
73
|
|
|
* @throws AuthException |
74
|
|
|
* @throws LockException |
75
|
|
|
* @return IGroup|null |
76
|
|
|
*/ |
77
|
7 |
|
public function getGroupDataOnly(int $groupId): ?IGroup |
78
|
|
|
{ |
79
|
7 |
|
$this->checkLock(); |
80
|
|
|
try { |
81
|
7 |
|
$groupLines = $this->openGroups(); |
82
|
2 |
|
} catch (AuthException $ex) { |
83
|
|
|
// silence the problems on storage |
84
|
2 |
|
return null; |
85
|
|
|
} |
86
|
5 |
|
foreach ($groupLines as &$line) { |
87
|
5 |
|
if ($line[IAccessGroups::GRP_ID] == $groupId) { |
88
|
5 |
|
return $this->getGroupClass($line); |
89
|
|
|
} |
90
|
|
|
} |
91
|
4 |
|
return null; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @throws AuthException |
96
|
|
|
* @throws LockException |
97
|
|
|
* @return IGroup[] |
98
|
|
|
*/ |
99
|
4 |
|
public function readGroup(): array |
100
|
|
|
{ |
101
|
4 |
|
$this->checkLock(); |
102
|
|
|
|
103
|
4 |
|
$groupLines = $this->openGroups(); |
104
|
4 |
|
$result = []; |
105
|
4 |
|
foreach ($groupLines as &$line) { |
106
|
4 |
|
$result[] = $this->getGroupClass($line); |
107
|
|
|
} |
108
|
|
|
|
109
|
4 |
|
return $result; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param array<int, string> $line |
114
|
|
|
* @return IGroup |
115
|
|
|
*/ |
116
|
9 |
|
protected function getGroupClass(array &$line): IGroup |
117
|
|
|
{ |
118
|
9 |
|
$group = new FileGroup(); |
119
|
9 |
|
$group->setData( |
120
|
9 |
|
intval($line[IAccessGroups::GRP_ID]), |
121
|
9 |
|
strval($line[IAccessGroups::GRP_NAME]), |
122
|
9 |
|
intval($line[IAccessGroups::GRP_AUTHOR]), |
123
|
9 |
|
strval($line[IAccessGroups::GRP_DESC]) |
124
|
|
|
); |
125
|
9 |
|
return $group; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param IGroup $group |
130
|
|
|
* @throws AuthException |
131
|
|
|
* @throws LockException |
132
|
|
|
*/ |
133
|
5 |
|
public function updateGroup(IGroup $group): void |
134
|
|
|
{ |
135
|
5 |
|
$groupName = $this->stripChars($group->getGroupName()); |
136
|
5 |
|
$groupDesc = $this->stripChars($group->getGroupDesc()); |
137
|
|
|
|
138
|
5 |
|
$this->checkLock(); |
139
|
|
|
|
140
|
5 |
|
$this->getLock()->create(); |
141
|
|
|
try { |
142
|
5 |
|
$groupLines = $this->openGroups(); |
143
|
1 |
|
} catch (AuthException $ex) { |
144
|
1 |
|
$this->getLock()->delete(); |
145
|
1 |
|
throw $ex; |
146
|
|
|
} |
147
|
4 |
|
foreach ($groupLines as &$line) { |
148
|
4 |
|
if ($line[IAccessGroups::GRP_ID] == $group->getGroupId()) { |
149
|
|
|
// REFILL |
150
|
4 |
|
$line[IAccessGroups::GRP_NAME] = !empty($groupName) ? $groupName : $line[IAccessGroups::GRP_NAME] ; |
151
|
4 |
|
$line[IAccessGroups::GRP_DESC] = !empty($groupDesc) ? $groupDesc : $line[IAccessGroups::GRP_DESC] ; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
4 |
|
$this->saveGroups($groupLines); |
156
|
4 |
|
$this->getLock()->delete(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param int $groupId |
161
|
|
|
* @throws AuthException |
162
|
|
|
* @throws LockException |
163
|
|
|
*/ |
164
|
7 |
|
public function deleteGroup(int $groupId): void |
165
|
|
|
{ |
166
|
7 |
|
$this->checkLock(); |
167
|
7 |
|
$this->checkRest($groupId); |
168
|
|
|
|
169
|
5 |
|
$changed = false; |
170
|
5 |
|
$this->getLock()->create(); |
171
|
|
|
|
172
|
|
|
// update groups |
173
|
|
|
try { |
174
|
5 |
|
$openGroups = $this->openGroups(); |
175
|
1 |
|
} catch (AuthException $ex) { |
176
|
|
|
// silence the problems on storage |
177
|
1 |
|
$this->getLock()->delete(); |
178
|
1 |
|
return; |
179
|
|
|
} |
180
|
4 |
|
foreach ($openGroups as $index => &$line) { |
181
|
4 |
|
if ($line[IAccessGroups::GRP_ID] == $groupId) { |
182
|
4 |
|
unset($openGroups[$index]); |
183
|
4 |
|
$changed = true; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// now save it |
188
|
4 |
|
if ($changed) { |
189
|
4 |
|
$this->saveGroups($openGroups); |
190
|
|
|
} |
191
|
4 |
|
$this->getLock()->delete(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Check the rest of source for existence of group |
196
|
|
|
* @param int $groupId |
197
|
|
|
* @throws AuthException |
198
|
|
|
*/ |
199
|
|
|
abstract protected function checkRest(int $groupId): void; |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @throws AuthException |
203
|
|
|
* @return array<int, array<int, string|int>> |
204
|
|
|
*/ |
205
|
|
|
abstract protected function openGroups(): array; |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param array<int, array<int, string|int>> $lines |
209
|
|
|
* @throws AuthException |
210
|
|
|
*/ |
211
|
|
|
abstract protected function saveGroups(array $lines): void; |
212
|
|
|
} |
213
|
|
|
|