Passed
Push — master ( ab3f5d...01e1f2 )
by Jan
02:56
created

PermissionsEmbed::writeBitPair()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 13
rs 10
c 0
b 0
f 0
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;
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
 * @package App\Entity
42
 */
43
class PermissionsEmbed
44
{
45
46
    /**
47
     * Permission values
48
     */
49
    public const INHERIT   = 0b00;
50
    public const ALLOW     = 0b01;
51
    public const DISALLOW  = 0b10;
52
53
54
    /**
55
     * Permission strings
56
     */
57
58
    const STORELOCATIONS    = 'storelocations';
59
    const FOOTRPINTS        = 'footprints';
60
    const CATEGORIES        = 'categories';
61
    const SUPPLIERS         = 'suppliers';
62
    const MANUFACTURERS     = 'manufacturers';
63
    const DEVICES           = 'devices';
64
    const ATTACHMENT_TYPES = 'attachment_types';
65
    const TOOLS             = 'tools';
66
    const PARTS             = 'parts';
67
    const PARTS_NAME        = 'parts_name';
68
    const PARTS_DESCRIPTION = 'parts_description';
69
    const PARTS_INSTOCK     = 'parts_instock';
70
    const PARTS_MININSTOCK  = 'parts_mininstock';
71
    const PARTS_FOOTPRINT   = 'parts_footprint';
72
    const PARTS_COMMENT     = 'parts_comment';
73
    const PARTS_STORELOCATION = 'parts_storelocation';
74
    const PARTS_MANUFACTURER = 'parts_manufacturer';
75
    const PARTS_ORDERDETAILS = 'parts_orderdetails';
76
    const PARTS_PRICES      = 'parts_prices';
77
    const PARTS_ATTACHMENTS = 'parts_attachments';
78
    const PARTS_ORDER        = 'parts_order';
79
    const GROUPS            = 'groups';
80
    const USERS             = 'users';
81
    const DATABASE          = 'system_database';
82
    const CONFIG            = 'system_config';
83
    const SYSTEM            = 'system';
84
    const DEVICE_PARTS      = 'devices_parts';
85
    const SELF              = 'self';
86
    const LABELS            = 'labels';
87
88
    /**
89
     * @var int
90
     * @ORM\Column(type="integer")
91
     */
92
    protected $system;
93
94
    /**
95
     * @var int
96
     * @ORM\Column(type="integer")
97
     */
98
    protected $groups;
99
100
    /**
101
     * @var int
102
     * @ORM\Column(type="integer")
103
     */
104
    protected $users;
105
106
    /**
107
     * @var int
108
     * @ORM\Column(type="integer")
109
     */
110
    protected $self;
111
112
    /**
113
     * @var int
114
     * @ORM\Column(type="integer")
115
     */
116
    protected $system_config;
117
118
    /**
119
     * @var int
120
     * @ORM\Column(type="integer")
121
     */
122
    protected $system_database;
123
124
    /**
125
     * @var int
126
     * @ORM\Column(type="bigint")
127
     */
128
    protected $parts;
129
130
    /**
131
     * @var int
132
     * @ORM\Column(type="smallint")
133
     */
134
    protected $parts_name;
135
136
    /**
137
     * @var int
138
     * @ORM\Column(type="smallint")
139
     */
140
    protected $parts_description;
141
142
    /**
143
     * @var int
144
     * @ORM\Column(type="smallint")
145
     */
146
    protected $parts_instock;
147
148
    /**
149
     * @var int
150
     * @ORM\Column(type="smallint")
151
     */
152
    protected $parts_mininstock;
153
154
    /**
155
     * @var int
156
     * @ORM\Column(type="smallint")
157
     */
158
    protected $parts_footprint;
159
160
    /**
161
     * @var int
162
     * @ORM\Column(type="smallint")
163
     */
164
    protected $parts_storelocation;
165
166
    /**
167
     * @var int
168
     * @ORM\Column(type="smallint")
169
     */
170
    protected $parts_manufacturer;
171
172
    /**
173
     * @var int
174
     * @ORM\Column(type="smallint")
175
     */
176
    protected $parts_comment;
177
178
    /**
179
     * @var int
180
     * @ORM\Column(type="smallint")
181
     */
182
    protected $parts_order;
183
184
    /**
185
     * @var int
186
     * @ORM\Column(type="smallint")
187
     */
188
    protected $parts_orderdetails;
189
190
    /**
191
     * @var int
192
     * @ORM\Column(type="smallint")
193
     */
194
    protected $parts_prices;
195
196
    /**
197
     * @var int
198
     * @ORM\Column(type="smallint", name="parts_attachements")
199
     */
200
    protected $parts_attachments;
201
202
    /**
203
     * @var int
204
     * @ORM\Column(type="integer")
205
     */
206
    protected $devices;
207
208
    /**
209
     * @var int
210
     * @ORM\Column(type="integer")
211
     */
212
    protected $devices_parts;
213
214
    /**
215
     * @var int
216
     * @ORM\Column(type="integer")
217
     */
218
    protected $storelocations;
219
220
    /**
221
     * @var int
222
     * @ORM\Column(type="integer")
223
     */
224
    protected $footprints;
225
226
    /**
227
     * @var int
228
     * @ORM\Column(type="integer")
229
     */
230
    protected $categories;
231
232
    /**
233
     * @var int
234
     * @ORM\Column(type="integer")
235
     */
236
    protected $suppliers;
237
238
    /**
239
     * @var int
240
     * @ORM\Column(type="integer")
241
     */
242
    protected $manufacturers;
243
244
    /**
245
     * @var int
246
     * @ORM\Column(type="integer", name="attachement_types")
247
     */
248
    protected $attachment_types;
249
250
    /**
251
     * @var int
252
     * @ORM\Column(type="integer")
253
     */
254
    protected $tools;
255
256
    /**
257
     * @var int
258
     * @ORM\Column(type="integer")
259
     */
260
    protected $labels;
261
262
263
    /**
264
     * Returns the bit pair value of the given permission.
265
     * @param string $permission_name The name of the permission, for which the bit pair should be returned.
266
     * @param int $bit_n The (lower) bit number of the bit pair, which should be read.
267
     * @return int The value of the bit pair. Compare to the INHERIT, ALLOW, and DISALLOW consts in this class.
268
     */
269
    public function getBitValue(string $permission_name, int $bit_n) : int
270
    {
271
        $perm_int = $this->$permission_name;
272
273
        return static::readBitPair($perm_int, $bit_n);
274
    }
275
276
    /**
277
     * Returns the value of the operation for the given permission.
278
     * @param string $permission_name The name of the permission, for which the operation should be returned.
279
     * @param int $bit_n The (lower) bit number of the bit pair for the operation.
280
     * @return bool|null The value of the operation. True, if the given operation is allowed, false if disallowed
281
     * and null if it should inherit from parent.
282
     */
283
    public function getPermissionValue(string $permission_name, int $bit_n) : ?bool
284
    {
285
        $value = $this->getBitValue($permission_name, $bit_n);
286
        if($value == self::ALLOW) {
287
            return true;
288
        } elseif($value == self::DISALLOW) {
289
            return false;
290
        } else {
291
            return null;
292
        }
293
    }
294
295
    /**
296
     * Reads a bit pair from $data.
297
     * @param $data int The data from where the bits should be extracted from.
298
     * @param $n int The number of the lower bit (of the pair) that should be read. Starting from zero.
299
     * @return int The value of the bit pair.
300
     */
301
    final protected static function readBitPair(int $data, int $n): int
302
    {
303
        Assert::lessThanEq($n,31, '$n must be smaller than 32, because only a 32bit int is used! Got %s.');
304
        if ($n % 2 !== 0) {
305
            throw new \InvalidArgumentException('$n must be dividable by 2, because we address bit pairs here!');
306
        }
307
308
        $mask = 0b11 << $n; //Create a mask for the data
309
        return ($data & $mask) >> $n; //Apply mask and shift back
310
    }
311
312
    /**
313
     * Writes a bit pair in the given $data and returns it.
314
     * @param $data int The data which should be modified.
315
     * @param $n int The number of the lower bit of the pair which should be written.
316
     * @param $new int The new value of the pair.
317
     * @return int The new data with the modified pair.
318
     */
319
    final protected static function writeBitPair(int $data, int $n, int $new) : int
320
    {
321
        Assert::lessThanEq($n,31, '$n must be smaller than 32, because only a 32bit int is used! Got %s.');
322
        Assert::lessThanEq($new, 3, '$new must be smaller than 3, because a bit pair is written! Got %s.');
323
324
        if ($n % 2 !== 0) {
325
            throw new \InvalidArgumentException('$n must be dividable by 2, because we address bit pairs here!');
326
        }
327
328
        $mask = 0b11 << $n; //Mask all bits that should be writen
329
        $newval = $new << $n; //The new value.
330
        $data = ($data & ~$mask) | ($newval & $mask);
331
        return $data;
332
    }
333
334
}