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 MEASUREMENT_UNITS = 'measurement_units'; |
62
|
|
|
public const CURRENCIES = 'currencies'; |
63
|
|
|
public const TOOLS = 'tools'; |
64
|
|
|
public const PARTS = 'parts'; |
65
|
|
|
public const PARTS_NAME = 'parts_name'; |
66
|
|
|
public const PARTS_DESCRIPTION = 'parts_description'; |
67
|
|
|
public const PARTS_MINAMOUNT = 'parts_minamount'; |
68
|
|
|
public const PARTS_FOOTPRINT = 'parts_footprint'; |
69
|
|
|
public const PARTS_MPN = 'parts_mpn'; |
70
|
|
|
public const PARTS_STATUS = 'parts_status'; |
71
|
|
|
public const PARTS_TAGS = 'parts_tags'; |
72
|
|
|
public const PARTS_UNIT = 'parts_unit'; |
73
|
|
|
public const PARTS_MASS = 'parts_mass'; |
74
|
|
|
public const PARTS_LOTS = 'parts_lots'; |
75
|
|
|
public const PARTS_COMMENT = 'parts_comment'; |
76
|
|
|
public const PARTS_MANUFACTURER = 'parts_manufacturer'; |
77
|
|
|
public const PARTS_ORDERDETAILS = 'parts_orderdetails'; |
78
|
|
|
public const PARTS_PRICES = 'parts_prices'; |
79
|
|
|
public const PARTS_ATTACHMENTS = 'parts_attachments'; |
80
|
|
|
public const PARTS_ORDER = 'parts_order'; |
81
|
|
|
public const GROUPS = 'groups'; |
82
|
|
|
public const USERS = 'users'; |
83
|
|
|
public const DATABASE = 'database'; |
84
|
|
|
public const CONFIG = 'config'; |
85
|
|
|
public const SYSTEM = 'system'; |
86
|
|
|
public const DEVICE_PARTS = 'devices_parts'; |
87
|
|
|
public const SELF = 'self'; |
88
|
|
|
public const LABELS = 'labels'; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var int |
92
|
|
|
* @ORM\Column(type="integer") |
93
|
|
|
*/ |
94
|
|
|
protected $system = 0; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @var int |
98
|
|
|
* @ORM\Column(type="integer") |
99
|
|
|
*/ |
100
|
|
|
protected $groups = 0; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var int |
104
|
|
|
* @ORM\Column(type="integer") |
105
|
|
|
*/ |
106
|
|
|
protected $users = 0; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @var int |
110
|
|
|
* @ORM\Column(type="integer") |
111
|
|
|
*/ |
112
|
|
|
protected $self = 0; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var int |
116
|
|
|
* @ORM\Column(type="integer", name="system_config") |
117
|
|
|
*/ |
118
|
|
|
protected $config = 0; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @var int |
122
|
|
|
* @ORM\Column(type="integer", name="system_database") |
123
|
|
|
*/ |
124
|
|
|
protected $database = 0; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @var int |
128
|
|
|
* @ORM\Column(type="bigint") |
129
|
|
|
*/ |
130
|
|
|
protected $parts = 0; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @var int |
134
|
|
|
* @ORM\Column(type="smallint") |
135
|
|
|
*/ |
136
|
|
|
protected $parts_name = 0; |
137
|
|
|
|
138
|
|
|
/** @var int |
139
|
|
|
* @ORM\Column(type="smallint") |
140
|
|
|
*/ |
141
|
|
|
protected $parts_category = 0; |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @var int |
145
|
|
|
* @ORM\Column(type="smallint") |
146
|
|
|
*/ |
147
|
|
|
protected $parts_description = 0; |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @var int |
151
|
|
|
* @ORM\Column(type="smallint") |
152
|
|
|
*/ |
153
|
|
|
protected $parts_minamount = 0; |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @var int |
157
|
|
|
* @ORM\Column(type="smallint") |
158
|
|
|
*/ |
159
|
|
|
protected $parts_footprint = 0; |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @var int |
163
|
|
|
* @ORM\Column(type="smallint") |
164
|
|
|
*/ |
165
|
|
|
protected $parts_lots = 0; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @var int |
169
|
|
|
* @ORM\Column(type="smallint") |
170
|
|
|
*/ |
171
|
|
|
protected $parts_tags = 0; |
172
|
|
|
|
173
|
|
|
/** @var int |
174
|
|
|
* @ORM\Column(type="smallint") |
175
|
|
|
*/ |
176
|
|
|
protected $parts_unit = 0; |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @var int |
180
|
|
|
* @ORM\Column(type="smallint") |
181
|
|
|
*/ |
182
|
|
|
protected $parts_mass = 0; |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @var int |
186
|
|
|
* @ORM\Column(type="smallint") |
187
|
|
|
*/ |
188
|
|
|
protected $parts_manufacturer = 0; |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @var int |
192
|
|
|
* @ORM\Column(type="smallint") |
193
|
|
|
*/ |
194
|
|
|
protected $parts_status = 0; |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @var int |
198
|
|
|
* @ORM\Column(type="smallint") |
199
|
|
|
*/ |
200
|
|
|
protected $parts_mpn = 0; |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @var int |
204
|
|
|
* @ORM\Column(type="smallint") |
205
|
|
|
*/ |
206
|
|
|
protected $parts_comment = 0; |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @var int |
210
|
|
|
* @ORM\Column(type="smallint") |
211
|
|
|
*/ |
212
|
|
|
protected $parts_order = 0; |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @var int |
216
|
|
|
* @ORM\Column(type="smallint") |
217
|
|
|
*/ |
218
|
|
|
protected $parts_orderdetails = 0; |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @var int |
222
|
|
|
* @ORM\Column(type="smallint") |
223
|
|
|
*/ |
224
|
|
|
protected $parts_prices = 0; |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @var int |
228
|
|
|
* @ORM\Column(type="smallint", name="parts_attachements") |
229
|
|
|
*/ |
230
|
|
|
protected $parts_attachments = 0; |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @var int |
234
|
|
|
* @ORM\Column(type="integer") |
235
|
|
|
*/ |
236
|
|
|
protected $devices = 0; |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @var int |
240
|
|
|
* @ORM\Column(type="integer") |
241
|
|
|
*/ |
242
|
|
|
protected $devices_parts = 0; |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @var int |
246
|
|
|
* @ORM\Column(type="integer") |
247
|
|
|
*/ |
248
|
|
|
protected $storelocations = 0; |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @var int |
252
|
|
|
* @ORM\Column(type="integer") |
253
|
|
|
*/ |
254
|
|
|
protected $footprints = 0; |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @var int |
258
|
|
|
* @ORM\Column(type="integer") |
259
|
|
|
*/ |
260
|
|
|
protected $categories = 0; |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @var int |
264
|
|
|
* @ORM\Column(type="integer") |
265
|
|
|
*/ |
266
|
|
|
protected $suppliers = 0; |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @var int |
270
|
|
|
* @ORM\Column(type="integer") |
271
|
|
|
*/ |
272
|
|
|
protected $manufacturers = 0; |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @var int |
276
|
|
|
* @ORM\Column(type="integer", name="attachement_types") |
277
|
|
|
*/ |
278
|
|
|
protected $attachment_types = 0; |
279
|
|
|
|
280
|
|
|
/** @var int |
281
|
|
|
* @ORM\Column(type="integer") |
282
|
|
|
*/ |
283
|
|
|
protected $currencies = 0; |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @var int |
287
|
|
|
* @ORM\Column(type="integer") |
288
|
|
|
*/ |
289
|
|
|
protected $measurement_units = 0; |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @var int |
293
|
|
|
* @ORM\Column(type="integer") |
294
|
|
|
*/ |
295
|
|
|
protected $tools = 0; |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @var int |
299
|
|
|
* @ORM\Column(type="integer") |
300
|
|
|
*/ |
301
|
|
|
protected $labels = 0; |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Checks whether a permission with the given name is valid for this object. |
305
|
|
|
* @param string $permission_name The name of the permission which should be checked for. |
306
|
|
|
* @return bool True if the permission is existing on this object. |
307
|
|
|
*/ |
308
|
|
|
public function isValidPermissionName(string $permission_name) : bool |
309
|
|
|
{ |
310
|
|
|
return isset($this->$permission_name); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Returns the bit pair value of the given permission. |
315
|
|
|
* |
316
|
|
|
* @param string $permission_name The name of the permission, for which the bit pair should be returned. |
317
|
|
|
* @param int $bit_n The (lower) bit number of the bit pair, which should be read. |
318
|
|
|
* |
319
|
|
|
* @return int The value of the bit pair. Compare to the INHERIT, ALLOW, and DISALLOW consts in this class. |
320
|
|
|
*/ |
321
|
|
|
public function getBitValue(string $permission_name, int $bit_n): int |
322
|
|
|
{ |
323
|
|
|
if (!$this->isValidPermissionName($permission_name)) { |
324
|
|
|
throw new \InvalidArgumentException(sprintf('No permission with the name "%s" is existing!', $permission_name)); |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$perm_int = $this->$permission_name; |
328
|
|
|
|
329
|
|
|
return static::readBitPair($perm_int, $bit_n); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Returns the value of the operation for the given permission. |
334
|
|
|
* |
335
|
|
|
* @param string $permission_name The name of the permission, for which the operation should be returned. |
336
|
|
|
* @param int $bit_n The (lower) bit number of the bit pair for the operation. |
337
|
|
|
* |
338
|
|
|
* @return bool|null The value of the operation. True, if the given operation is allowed, false if disallowed |
339
|
|
|
* and null if it should inherit from parent. |
340
|
|
|
*/ |
341
|
|
|
public function getPermissionValue(string $permission_name, int $bit_n): ?bool |
342
|
|
|
{ |
343
|
|
|
$value = $this->getBitValue($permission_name, $bit_n); |
344
|
|
|
if (self::ALLOW === $value) { |
345
|
|
|
return true; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
if (self::DISALLOW === $value) { |
349
|
|
|
return false; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
return null; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Sets the value of the given permission and operation. |
357
|
|
|
* @param string $permission_name The name of the permission, for which the bit pair should be written. |
358
|
|
|
* @param int $bit_n The (lower) bit number of the bit pair, which should be written. |
359
|
|
|
* @param bool|null $new_value The new value for the operation: |
360
|
|
|
* True, if the given operation is allowed, false if disallowed |
361
|
|
|
* and null if it should inherit from parent. |
362
|
|
|
* @return PermissionsEmbed The instance itself. |
363
|
|
|
*/ |
364
|
|
|
public function setPermissionValue(string $permission_name, int $bit_n, ?bool $new_value) : self |
365
|
|
|
{ |
366
|
|
|
//Determine which bit value the given value is. |
367
|
|
|
if ($new_value === true) { |
368
|
|
|
$bit_value = static::ALLOW; |
369
|
|
|
} elseif ($new_value === false) { |
370
|
|
|
$bit_value = static::DISALLOW; |
371
|
|
|
} else { |
372
|
|
|
$bit_value = static::INHERIT; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
$this->setBitValue($permission_name, $bit_n, $bit_value); |
376
|
|
|
|
377
|
|
|
return $this; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Sets the bit value of the given permission and operation. |
382
|
|
|
* @param string $permission_name The name of the permission, for which the bit pair should be written. |
383
|
|
|
* @param int $bit_n The (lower) bit number of the bit pair, which should be written. |
384
|
|
|
* @param int $new_value The new (bit) value of the bit pair, which should be written. |
385
|
|
|
* @return PermissionsEmbed The instance itself. |
386
|
|
|
*/ |
387
|
|
|
public function setBitValue(string $permission_name, int $bit_n, int $new_value) : self |
388
|
|
|
{ |
389
|
|
|
if (!$this->isValidPermissionName($permission_name)) { |
390
|
|
|
throw new \InvalidArgumentException('No permission with the given name is existing!'); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
$this->$permission_name = static::writeBitPair($this->$permission_name, $bit_n, $new_value); |
394
|
|
|
|
395
|
|
|
return $this; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Returns the given permission as raw int (all bit at once) |
400
|
|
|
* @param string $permission_name The name of the permission, which should be retrieved. |
401
|
|
|
* If this is not existing an exception is thrown. |
402
|
|
|
* @return int The raw permission value. |
403
|
|
|
*/ |
404
|
|
|
public function getRawPermissionValue(string $permission_name) : int |
405
|
|
|
{ |
406
|
|
|
if (!$this->isValidPermissionName($permission_name)) { |
407
|
|
|
throw new \InvalidArgumentException('No permission with the given name is existing!'); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
return $this->$permission_name; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Sets the given permission to the value. |
415
|
|
|
* @param string $permission_name The name of the permission to that should be set. |
416
|
|
|
* @param int $value The new value of the permsission |
417
|
|
|
* @return $this |
418
|
|
|
*/ |
419
|
|
|
public function setRawPermissionValue(string $permission_name, int $value) : self |
420
|
|
|
{ |
421
|
|
|
if (!$this->isValidPermissionName($permission_name)) { |
422
|
|
|
throw new \InvalidArgumentException( |
423
|
|
|
sprintf('No permission with the given name %s is existing!', $permission_name) |
424
|
|
|
); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
$this->$permission_name = $value; |
428
|
|
|
return $this; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Sets multiple permissions at once. |
433
|
|
|
* @param array $values An array in the form ['perm_name' => $value], containing the new data |
434
|
|
|
* @param array|null $values2 If this array is not null, the first array will treated of list of perm names, |
435
|
|
|
* and this array as an array of new values. |
436
|
|
|
* @return $this |
437
|
|
|
*/ |
438
|
|
|
public function setRawPermissionValues(array $values, array $values2 = null) : self |
439
|
|
|
{ |
440
|
|
|
if (!empty($values2)) { |
441
|
|
|
$values = array_combine($values, $values2); |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
foreach ($values as $key => $value) { |
445
|
|
|
$this->setRawPermissionValue($key, $value); |
446
|
|
|
} |
447
|
|
|
return $this; |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Reads a bit pair from $data. |
452
|
|
|
* |
453
|
|
|
* @param $data int The data from where the bits should be extracted from. |
454
|
|
|
* @param $n int The number of the lower bit (of the pair) that should be read. Starting from zero. |
455
|
|
|
* |
456
|
|
|
* @return int The value of the bit pair. |
457
|
|
|
*/ |
458
|
|
|
final protected static function readBitPair(int $data, int $n): int |
459
|
|
|
{ |
460
|
|
|
Assert::lessThanEq($n, 31, '$n must be smaller than 32, because only a 32bit int is used! Got %s.'); |
461
|
|
|
if (0 !== $n % 2) { |
462
|
|
|
throw new \InvalidArgumentException('$n must be dividable by 2, because we address bit pairs here!'); |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
$mask = 0b11 << $n; //Create a mask for the data |
466
|
|
|
return ($data & $mask) >> $n; //Apply mask and shift back |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* Writes a bit pair in the given $data and returns it. |
471
|
|
|
* |
472
|
|
|
* @param $data int The data which should be modified. |
473
|
|
|
* @param $n int The number of the lower bit of the pair which should be written. |
474
|
|
|
* @param $new int The new value of the pair. |
475
|
|
|
* |
476
|
|
|
* @return int The new data with the modified pair. |
477
|
|
|
*/ |
478
|
|
|
final protected static function writeBitPair(int $data, int $n, int $new): int |
479
|
|
|
{ |
480
|
|
|
Assert::lessThanEq($n, 31, '$n must be smaller than 32, because only a 32bit int is used! Got %s.'); |
481
|
|
|
Assert::lessThanEq($new, 3, '$new must be smaller than 3, because a bit pair is written! Got %s.'); |
482
|
|
|
Assert::greaterThanEq($new, 0, '$new must not be negative, because a bit pair is written! Got %s.'); |
483
|
|
|
|
484
|
|
|
if (0 !== $n % 2) { |
485
|
|
|
throw new \InvalidArgumentException('$n must be dividable by 2, because we address bit pairs here!'); |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
$mask = 0b11 << $n; //Mask all bits that should be writen |
489
|
|
|
$newval = $new << $n; //The new value. |
490
|
|
|
$data = ($data & ~$mask) | ($newval & $mask); |
491
|
|
|
|
492
|
|
|
return $data; |
493
|
|
|
} |
494
|
|
|
} |
495
|
|
|
|