Passed
Push — master ( 1c7155...943d23 )
by Jan
03:08
created

PermissionsEmbed::isValidPermissionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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