1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Circles - Bring cloud-users closer together. |
8
|
|
|
* |
9
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
10
|
|
|
* later. See the COPYING file. |
11
|
|
|
* |
12
|
|
|
* @author Maxence Lange <[email protected]> |
13
|
|
|
* @copyright 2021 |
14
|
|
|
* @license GNU AGPL version 3 or any later version |
15
|
|
|
* |
16
|
|
|
* This program is free software: you can redistribute it and/or modify |
17
|
|
|
* it under the terms of the GNU Affero General Public License as |
18
|
|
|
* published by the Free Software Foundation, either version 3 of the |
19
|
|
|
* License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU Affero General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU Affero General Public License |
27
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
namespace OCA\Circles\Model\Helpers; |
33
|
|
|
|
34
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools; |
35
|
|
|
use OCA\Circles\Exceptions\MemberHelperException; |
36
|
|
|
use OCA\Circles\Exceptions\MemberLevelException; |
37
|
|
|
use OCA\Circles\Exceptions\ParseMemberLevelException; |
38
|
|
|
use OCA\Circles\Model\Member; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Class MemberHelper |
43
|
|
|
* |
44
|
|
|
* @method void mustBeMember() @throws MemberHelperException, MemberLevelException |
45
|
|
|
* @method void mustBeModerator() @throws MemberHelperException, MemberLevelException |
46
|
|
|
* @method void mustBeAdmin() @throws MemberHelperException, MemberLevelException |
47
|
|
|
* @method void mustBeOwner() @throws MemberHelperException, MemberLevelException |
48
|
|
|
* @method void cannotBeMember() @throws MemberHelperException, MemberLevelException |
49
|
|
|
* @method void cannotBeModerator() @throws MemberHelperException, MemberLevelException |
50
|
|
|
* @method void cannotBeAdmin() @throws MemberHelperException, MemberLevelException |
51
|
|
|
* @method void cannotBeOwner() @throws MemberHelperException, MemberLevelException |
52
|
|
|
* |
53
|
|
|
* @package OCA\Circles\Model\Helpers |
54
|
|
|
*/ |
55
|
|
|
class MemberHelper { |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
use TArrayTools; |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** @var Member */ |
62
|
|
|
private $member; |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Member constructor. |
67
|
|
|
* |
68
|
|
|
* @param Member $member |
69
|
|
|
*/ |
70
|
|
|
public function __construct(Member $member) { |
71
|
|
|
$this->member = $member; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $name |
77
|
|
|
* @param array $arguments |
78
|
|
|
* |
79
|
|
|
* @throws MemberHelperException |
80
|
|
|
* @throws MemberLevelException |
81
|
|
|
*/ |
82
|
|
|
public function __call(string $name, array $arguments): void { |
83
|
|
|
if (substr(strtolower($name), 0, 8) === 'cannotbe') { |
84
|
|
|
$this->cannotBe(substr($name, 8), $arguments); |
85
|
|
|
|
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
if (substr(strtolower($name), 0, 6) === 'mustbe') { |
89
|
|
|
$this->mustBe(substr($name, 6), $arguments); |
90
|
|
|
|
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
throw new MemberHelperException('unknown method call'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $levelString |
100
|
|
|
* @param array $arguments |
101
|
|
|
* |
102
|
|
|
* @throws MemberHelperException |
103
|
|
|
* @throws MemberLevelException |
104
|
|
|
*/ |
105
|
|
|
private function mustBe(string $levelString, array $arguments): void { |
|
|
|
|
106
|
|
|
try { |
107
|
|
|
$level = Member::parseLevelString($levelString); |
108
|
|
|
} catch (ParseMemberLevelException $e) { |
109
|
|
|
throw new MemberHelperException('method ' . $levelString . ' not found'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->mustHaveLevelEqualOrAbove($level); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $levelString |
118
|
|
|
* @param array $arguments |
119
|
|
|
* |
120
|
|
|
* @throws MemberHelperException |
121
|
|
|
* @throws MemberLevelException |
122
|
|
|
*/ |
123
|
|
|
private function cannotBe(string $levelString, array $arguments): void { |
|
|
|
|
124
|
|
|
try { |
125
|
|
|
$level = Member::parseLevelString($levelString); |
126
|
|
|
} catch (ParseMemberLevelException $e) { |
127
|
|
|
throw new MemberHelperException('method ' . $levelString . ' not found'); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($this->member->getLevel() >= $level) { |
131
|
|
|
throw new MemberLevelException('Member cannot be ' . $levelString); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param int $level |
138
|
|
|
* |
139
|
|
|
* @throws MemberLevelException |
140
|
|
|
*/ |
141
|
|
|
public function mustHaveLevelAbove(int $level) { |
142
|
|
|
if ($this->member->getLevel() <= $level) { |
143
|
|
|
throw new MemberLevelException('Insufficient rights'); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param int $level |
150
|
|
|
* |
151
|
|
|
* @throws MemberLevelException |
152
|
|
|
*/ |
153
|
|
|
public function mustHaveLevelAboveOrEqual(int $level) { |
154
|
|
|
if ($this->member->getLevel() < $level) { |
155
|
|
|
throw new MemberLevelException('Insufficient rights'); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param int $level |
162
|
|
|
* |
163
|
|
|
* @throws MemberLevelException |
164
|
|
|
*/ |
165
|
|
|
public function mustHaveLevelEqualOrAbove(int $level) { |
166
|
|
|
if ($this->member->getLevel() < $level) { |
167
|
|
|
throw new MemberLevelException('Insufficient rights'); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param Member $compare |
174
|
|
|
* |
175
|
|
|
* @throws MemberLevelException |
176
|
|
|
*/ |
177
|
|
|
public function mustBeHigherLevelThan(Member $compare) { |
178
|
|
|
$this->mustHaveLevelAbove($compare->getLevel()); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param Member $compare |
183
|
|
|
* |
184
|
|
|
* @throws MemberLevelException |
185
|
|
|
*/ |
186
|
|
|
public function mustBeHigherOrSameLevelThan(Member $compare) { |
187
|
|
|
$this->mustHaveLevelEqualOrAbove($compare->getLevel()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.