1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* part-db version 0.1 |
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
6
|
|
|
* http://www.cl-projects.de/ |
7
|
|
|
* |
8
|
|
|
* part-db version 0.2+ |
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
10
|
|
|
* http://code.google.com/p/part-db/ |
11
|
|
|
* |
12
|
|
|
* Part-DB Version 0.4+ |
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
14
|
|
|
* https://github.com/jbtronics |
15
|
|
|
* |
16
|
|
|
* This program is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU General Public License |
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
19
|
|
|
* of the 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 General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU General Public License |
27
|
|
|
* along with this program; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace App\Entity\UserSystem; |
33
|
|
|
|
34
|
|
|
use Doctrine\ORM\Mapping as ORM; |
35
|
|
|
use Webmozart\Assert\Assert; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* This entity represents the permission fields a user or group can have. |
39
|
|
|
* |
40
|
|
|
* @ORM\Embeddable() |
41
|
|
|
*/ |
42
|
|
|
class PermissionsEmbed |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* Permission values. |
46
|
|
|
*/ |
47
|
|
|
public const INHERIT = 0b00; |
48
|
|
|
public const ALLOW = 0b01; |
49
|
|
|
public const DISALLOW = 0b10; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Permission strings. |
53
|
|
|
*/ |
54
|
|
|
public const STORELOCATIONS = 'storelocations'; |
55
|
|
|
public const FOOTRPINTS = 'footprints'; |
56
|
|
|
public const CATEGORIES = 'categories'; |
57
|
|
|
public const SUPPLIERS = 'suppliers'; |
58
|
|
|
public const MANUFACTURERS = 'manufacturers'; |
59
|
|
|
public const DEVICES = 'devices'; |
60
|
|
|
public const ATTACHMENT_TYPES = 'attachment_types'; |
61
|
|
|
public const TOOLS = 'tools'; |
62
|
|
|
public const PARTS = 'parts'; |
63
|
|
|
public const PARTS_NAME = 'parts_name'; |
64
|
|
|
public const PARTS_DESCRIPTION = 'parts_description'; |
65
|
|
|
public const PARTS_INSTOCK = 'parts_instock'; |
66
|
|
|
public const PARTS_MININSTOCK = 'parts_mininstock'; |
67
|
|
|
public const PARTS_FOOTPRINT = 'parts_footprint'; |
68
|
|
|
public const PARTS_COMMENT = 'parts_comment'; |
69
|
|
|
public const PARTS_STORELOCATION = 'parts_storelocation'; |
70
|
|
|
public const PARTS_MANUFACTURER = 'parts_manufacturer'; |
71
|
|
|
public const PARTS_ORDERDETAILS = 'parts_orderdetails'; |
72
|
|
|
public const PARTS_PRICES = 'parts_prices'; |
73
|
|
|
public const PARTS_ATTACHMENTS = 'parts_attachments'; |
74
|
|
|
public const PARTS_ORDER = 'parts_order'; |
75
|
|
|
public const GROUPS = 'groups'; |
76
|
|
|
public const USERS = 'users'; |
77
|
|
|
public const DATABASE = 'database'; |
78
|
|
|
public const CONFIG = 'config'; |
79
|
|
|
public const SYSTEM = 'system'; |
80
|
|
|
public const DEVICE_PARTS = 'devices_parts'; |
81
|
|
|
public const SELF = 'self'; |
82
|
|
|
public const LABELS = 'labels'; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var int |
86
|
|
|
* @ORM\Column(type="integer") |
87
|
|
|
*/ |
88
|
|
|
protected $system = 0; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var int |
92
|
|
|
* @ORM\Column(type="integer") |
93
|
|
|
*/ |
94
|
|
|
protected $groups = 0; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var int |
98
|
|
|
* @ORM\Column(type="integer") |
99
|
|
|
*/ |
100
|
|
|
protected $users = 0; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var int |
104
|
|
|
* @ORM\Column(type="integer") |
105
|
|
|
*/ |
106
|
|
|
protected $self = 0; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @var int |
110
|
|
|
* @ORM\Column(type="integer", name="system_config") |
111
|
|
|
*/ |
112
|
|
|
protected $config = 0; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var int |
116
|
|
|
* @ORM\Column(type="integer", name="system_database") |
117
|
|
|
*/ |
118
|
|
|
protected $database = 0; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @var int |
122
|
|
|
* @ORM\Column(type="bigint") |
123
|
|
|
*/ |
124
|
|
|
protected $parts = 0; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @var int |
128
|
|
|
* @ORM\Column(type="smallint") |
129
|
|
|
*/ |
130
|
|
|
protected $parts_name = 0; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @var int |
134
|
|
|
* @ORM\Column(type="smallint") |
135
|
|
|
*/ |
136
|
|
|
protected $parts_description = 0; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @var int |
140
|
|
|
* @ORM\Column(type="smallint") |
141
|
|
|
*/ |
142
|
|
|
protected $parts_instock = 0; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @var int |
146
|
|
|
* @ORM\Column(type="smallint") |
147
|
|
|
*/ |
148
|
|
|
protected $parts_mininstock = 0; |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @var int |
152
|
|
|
* @ORM\Column(type="smallint") |
153
|
|
|
*/ |
154
|
|
|
protected $parts_footprint = 0; |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @var int |
158
|
|
|
* @ORM\Column(type="smallint") |
159
|
|
|
*/ |
160
|
|
|
protected $parts_storelocation = 0; |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @var int |
164
|
|
|
* @ORM\Column(type="smallint") |
165
|
|
|
*/ |
166
|
|
|
protected $parts_manufacturer = 0; |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @var int |
170
|
|
|
* @ORM\Column(type="smallint") |
171
|
|
|
*/ |
172
|
|
|
protected $parts_comment = 0; |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @var int |
176
|
|
|
* @ORM\Column(type="smallint") |
177
|
|
|
*/ |
178
|
|
|
protected $parts_order = 0; |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @var int |
182
|
|
|
* @ORM\Column(type="smallint") |
183
|
|
|
*/ |
184
|
|
|
protected $parts_orderdetails = 0; |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @var int |
188
|
|
|
* @ORM\Column(type="smallint") |
189
|
|
|
*/ |
190
|
|
|
protected $parts_prices = 0; |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @var int |
194
|
|
|
* @ORM\Column(type="smallint", name="parts_attachements") |
195
|
|
|
*/ |
196
|
|
|
protected $parts_attachments = 0; |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @var int |
200
|
|
|
* @ORM\Column(type="integer") |
201
|
|
|
*/ |
202
|
|
|
protected $devices = 0; |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @var int |
206
|
|
|
* @ORM\Column(type="integer") |
207
|
|
|
*/ |
208
|
|
|
protected $devices_parts = 0; |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @var int |
212
|
|
|
* @ORM\Column(type="integer") |
213
|
|
|
*/ |
214
|
|
|
protected $storelocations = 0; |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @var int |
218
|
|
|
* @ORM\Column(type="integer") |
219
|
|
|
*/ |
220
|
|
|
protected $footprints = 0; |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @var int |
224
|
|
|
* @ORM\Column(type="integer") |
225
|
|
|
*/ |
226
|
|
|
protected $categories = 0; |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @var int |
230
|
|
|
* @ORM\Column(type="integer") |
231
|
|
|
*/ |
232
|
|
|
protected $suppliers = 0; |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @var int |
236
|
|
|
* @ORM\Column(type="integer") |
237
|
|
|
*/ |
238
|
|
|
protected $manufacturers = 0; |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @var int |
242
|
|
|
* @ORM\Column(type="integer", name="attachement_types") |
243
|
|
|
*/ |
244
|
|
|
protected $attachment_types = 0; |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @var int |
248
|
|
|
* @ORM\Column(type="integer") |
249
|
|
|
*/ |
250
|
|
|
protected $tools = 0; |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @var int |
254
|
|
|
* @ORM\Column(type="integer") |
255
|
|
|
*/ |
256
|
|
|
protected $labels = 0; |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Checks whether a permission with the given name is valid for this object. |
260
|
|
|
* @param string $permission_name The name of the permission which should be checked for. |
261
|
|
|
* @return bool True if the permission is existing on this object. |
262
|
|
|
*/ |
263
|
|
|
public function isValidPermissionName(string $permission_name) : bool |
264
|
|
|
{ |
265
|
|
|
return isset($this->$permission_name); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Returns the bit pair value of the given permission. |
270
|
|
|
* |
271
|
|
|
* @param string $permission_name The name of the permission, for which the bit pair should be returned. |
272
|
|
|
* @param int $bit_n The (lower) bit number of the bit pair, which should be read. |
273
|
|
|
* |
274
|
|
|
* @return int The value of the bit pair. Compare to the INHERIT, ALLOW, and DISALLOW consts in this class. |
275
|
|
|
*/ |
276
|
|
|
public function getBitValue(string $permission_name, int $bit_n): int |
277
|
|
|
{ |
278
|
|
|
if(!$this->isValidPermissionName($permission_name)) { |
279
|
|
|
throw new \InvalidArgumentException('No permission with the given name is existing!'); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
$perm_int = $this->$permission_name; |
283
|
|
|
|
284
|
|
|
return static::readBitPair($perm_int, $bit_n); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Returns the value of the operation for the given permission. |
289
|
|
|
* |
290
|
|
|
* @param string $permission_name The name of the permission, for which the operation should be returned. |
291
|
|
|
* @param int $bit_n The (lower) bit number of the bit pair for the operation. |
292
|
|
|
* |
293
|
|
|
* @return bool|null The value of the operation. True, if the given operation is allowed, false if disallowed |
294
|
|
|
* and null if it should inherit from parent. |
295
|
|
|
*/ |
296
|
|
|
public function getPermissionValue(string $permission_name, int $bit_n): ?bool |
297
|
|
|
{ |
298
|
|
|
$value = $this->getBitValue($permission_name, $bit_n); |
299
|
|
|
if (self::ALLOW === $value) { |
300
|
|
|
return true; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
if (self::DISALLOW === $value) { |
304
|
|
|
return false; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return null; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Sets the value of the given permission and operation. |
312
|
|
|
* @param string $permission_name The name of the permission, for which the bit pair should be written. |
313
|
|
|
* @param int $bit_n The (lower) bit number of the bit pair, which should be written. |
314
|
|
|
* @param bool|null $new_value The new value for the operation: |
315
|
|
|
* True, if the given operation is allowed, false if disallowed |
316
|
|
|
* and null if it should inherit from parent. |
317
|
|
|
* @return PermissionsEmbed The instance itself. |
318
|
|
|
*/ |
319
|
|
|
public function setPermissionValue(string $permission_name, int $bit_n, ?bool $new_value) : self |
320
|
|
|
{ |
321
|
|
|
//Determine which bit value the given value is. |
322
|
|
|
if($new_value === true) { |
323
|
|
|
$bit_value = static::ALLOW; |
324
|
|
|
} elseif($new_value === false) { |
325
|
|
|
$bit_value = static::DISALLOW; |
326
|
|
|
} else { |
327
|
|
|
$bit_value = static::INHERIT; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
$this->setBitValue($permission_name, $bit_n, $bit_value); |
331
|
|
|
|
332
|
|
|
return $this; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Sets the bit value of the given permission and operation. |
337
|
|
|
* @param string $permission_name The name of the permission, for which the bit pair should be written. |
338
|
|
|
* @param int $bit_n The (lower) bit number of the bit pair, which should be written. |
339
|
|
|
* @param int $new_value The new (bit) value of the bit pair, which should be written. |
340
|
|
|
* @return PermissionsEmbed The instance itself. |
341
|
|
|
*/ |
342
|
|
|
public function setBitValue(string $permission_name, int $bit_n, int $new_value) : self |
343
|
|
|
{ |
344
|
|
|
if(!$this->isValidPermissionName($permission_name)) { |
345
|
|
|
throw new \InvalidArgumentException('No permission with the given name is existing!'); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
$this->$permission_name = static::writeBitPair($this->$permission_name, $bit_n, $new_value); |
349
|
|
|
|
350
|
|
|
return $this; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Reads a bit pair from $data. |
355
|
|
|
* |
356
|
|
|
* @param $data int The data from where the bits should be extracted from. |
357
|
|
|
* @param $n int The number of the lower bit (of the pair) that should be read. Starting from zero. |
358
|
|
|
* |
359
|
|
|
* @return int The value of the bit pair. |
360
|
|
|
*/ |
361
|
|
|
final protected static function readBitPair(int $data, int $n): int |
362
|
|
|
{ |
363
|
|
|
Assert::lessThanEq($n, 31, '$n must be smaller than 32, because only a 32bit int is used! Got %s.'); |
364
|
|
|
if (0 !== $n % 2) { |
365
|
|
|
throw new \InvalidArgumentException('$n must be dividable by 2, because we address bit pairs here!'); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
$mask = 0b11 << $n; //Create a mask for the data |
369
|
|
|
return ($data & $mask) >> $n; //Apply mask and shift back |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Writes a bit pair in the given $data and returns it. |
374
|
|
|
* |
375
|
|
|
* @param $data int The data which should be modified. |
376
|
|
|
* @param $n int The number of the lower bit of the pair which should be written. |
377
|
|
|
* @param $new int The new value of the pair. |
378
|
|
|
* |
379
|
|
|
* @return int The new data with the modified pair. |
380
|
|
|
*/ |
381
|
|
|
final protected static function writeBitPair(int $data, int $n, int $new): int |
382
|
|
|
{ |
383
|
|
|
Assert::lessThanEq($n, 31, '$n must be smaller than 32, because only a 32bit int is used! Got %s.'); |
384
|
|
|
Assert::lessThanEq($new, 3, '$new must be smaller than 3, because a bit pair is written! Got %s.'); |
385
|
|
|
Assert::greaterThanEq($new, 0, '$new must not be negative, because a bit pair is written! Got %s.'); |
386
|
|
|
|
387
|
|
|
if (0 !== $n % 2) { |
388
|
|
|
throw new \InvalidArgumentException('$n must be dividable by 2, because we address bit pairs here!'); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
$mask = 0b11 << $n; //Mask all bits that should be writen |
392
|
|
|
$newval = $new << $n; //The new value. |
393
|
|
|
$data = ($data & ~$mask) | ($newval & $mask); |
394
|
|
|
|
395
|
|
|
return $data; |
396
|
|
|
} |
397
|
|
|
} |
398
|
|
|
|