Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
71 / 71
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
PermissionAbstract
100.00% covered (success)
100.00%
71 / 71
100.00% covered (success)
100.00%
8 / 8
44
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 getPermission
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
7
 setPermission
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 addPermission
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
6
 hasPermissionFlags
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasPermission
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
20
 isEqual
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
7
 jsonSerialize
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Account
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Account;
16
17/**
18 * Permission class.
19 *
20 * This permission abstract is the basis for all permissions. Contrary to it's name it is not an
21 * abstract class and can be used directly if needed.
22 *
23 * @package phpOMS\Account
24 * @license OMS License 2.0
25 * @link    https://jingga.app
26 * @since   1.0.0
27 */
28class PermissionAbstract implements \JsonSerializable
29{
30    /**
31     * Permission id.
32     *
33     * @var int
34     * @since 1.0.0
35     */
36    public int $id = 0;
37
38    /**
39     * Unit id.
40     *
41     * @var null|int
42     * @since 1.0.0
43     */
44    public ?int $unit = null;
45
46    /**
47     * App name.
48     *
49     * @var null|int
50     * @since 1.0.0
51     */
52    public ?int $app = null;
53
54    /**
55     * Module id.
56     *
57     * @var null|string
58     * @since 1.0.0
59     */
60    public ?string $module = null;
61
62    /**
63     * Providing module id.
64     *
65     * @var string
66     * @since 1.0.0
67     */
68    public ?string $from = null;
69
70    /**
71     * Type.
72     *
73     * @var null|int
74     * @since 1.0.0
75     */
76    public ?int $category = null;
77
78    /**
79     * Element id.
80     *
81     * null === all
82     * int === specific
83     *
84     * @var null|int
85     * @since 1.0.0
86     */
87    public ?int $element = null;
88
89    /**
90     * Component id.
91     *
92     * null === all
93     * int === specific
94     * 0 === own data
95     *
96     * @var null|int
97     * @since 1.0.0
98     */
99    public ?int $component = null;
100
101    /**
102     * Permission.
103     *
104     * @var bool
105     * @since 1.0.0
106     */
107    public bool $hasRead = false;
108
109    /**
110     * Permission.
111     *
112     * @var bool
113     * @since 1.0.0
114     */
115    public bool $hasModify = false;
116
117    /**
118     * Permission.
119     *
120     * @var bool
121     * @since 1.0.0
122     */
123    public bool $hasCreate = false;
124
125    /**
126     * Default create permissions
127     *
128     * @var null|string
129     * @since 1.0.0
130     */
131    public ?string $defaultCPermissions = null;
132
133    /**
134     * Permission.
135     *
136     * @var bool
137     * @since 1.0.0
138     */
139    public bool $hasDelete = false;
140
141    /**
142     * Permission.
143     *
144     * @var bool
145     * @since 1.0.0
146     */
147    public bool $hasPermission = false;
148
149    /**
150     * Default permission permissions
151     *
152     * @var null|string
153     * @since 1.0.0
154     */
155    public ?string $defaultPPermissions = null;
156
157    /**
158     * Constructor.
159     *
160     * @param null|int    $unit       Unit to check (null if all are acceptable)
161     * @param null|int    $app        App to check  (null if all are acceptable)
162     * @param null|string $module     Module Module to check  (null if all are acceptable)
163     * @param null|string $from       Provided by which module
164     * @param null|int    $category   Category (e.g. customer) (null if all are acceptable)
165     * @param null|int    $element    (e.g. customer id) (null if all are acceptable)
166     * @param null|int    $component  (e.g. address) (null if all are acceptable)
167     * @param int         $permission Permission to check
168     *
169     * @since 1.0.0
170     */
171    public function __construct(
172        int $unit = null,
173        int $app = null,
174        string $module = null,
175        string $from = null,
176        int $category = null,
177        int $element = null,
178        int $component = null,
179        int $permission = PermissionType::NONE
180    ) {
181        $this->unit      = $unit;
182        $this->app       = $app;
183        $this->module    = $module;
184        $this->from      = $from;
185        $this->category  = $category;
186        $this->element   = $element;
187        $this->component = $component;
188
189        $this->hasRead       = ($permission & PermissionType::READ) === PermissionType::READ;
190        $this->hasCreate     = ($permission & PermissionType::CREATE) === PermissionType::CREATE;
191        $this->hasModify     = ($permission & PermissionType::MODIFY) === PermissionType::MODIFY;
192        $this->hasDelete     = ($permission & PermissionType::DELETE) === PermissionType::DELETE;
193        $this->hasPermission = ($permission & PermissionType::PERMISSION) === PermissionType::PERMISSION;
194    }
195
196    /**
197     * Get permission
198     *
199     * @return int Returns the permission (PermissionType)
200     *
201     * @since 1.0.0
202     */
203    public function getPermission() : int
204    {
205        $permission = 0;
206
207        if ($this->hasRead) {
208            $permission |= PermissionType::READ;
209        }
210
211        if ($this->hasCreate) {
212            $permission |= PermissionType::CREATE;
213        }
214
215        if ($this->hasModify) {
216            $permission |= PermissionType::MODIFY;
217        }
218
219        if ($this->hasDelete) {
220            $permission |= PermissionType::DELETE;
221        }
222
223        if ($this->hasPermission) {
224            $permission |= PermissionType::PERMISSION;
225        }
226
227        return $permission === 0 ? PermissionType::NONE : $permission;
228    }
229
230    /**
231     * Set permission.
232     *
233     * @param int $permission Permission
234     *
235     * @return void
236     *
237     * @since 1.0.0
238     */
239    public function setPermission(int $permission = 0) : void
240    {
241        $this->hasRead       = ($permission & PermissionType::READ) === PermissionType::READ;
242        $this->hasCreate     = ($permission & PermissionType::CREATE) === PermissionType::CREATE;
243        $this->hasModify     = ($permission & PermissionType::MODIFY) === PermissionType::MODIFY;
244        $this->hasDelete     = ($permission & PermissionType::DELETE) === PermissionType::DELETE;
245        $this->hasPermission = ($permission & PermissionType::PERMISSION) === PermissionType::PERMISSION;
246    }
247
248    /**
249     * Add permission.
250     *
251     * @param int $permission Permission
252     *
253     * @return void
254     *
255     * @since 1.0.0
256     */
257    public function addPermission(int $permission = 0) : void
258    {
259        switch($permission) {
260            case PermissionType::READ:
261                $this->hasRead = true;
262                break;
263            case PermissionType::CREATE:
264                $this->hasCreate = true;
265                break;
266            case PermissionType::MODIFY:
267                $this->hasModify = true;
268                break;
269            case PermissionType::DELETE:
270                $this->hasDelete = true;
271                break;
272            case PermissionType::PERMISSION:
273                $this->hasPermission = true;
274                break;
275        }
276    }
277
278    /**
279     * Has permission.
280     *
281     * @param int $permission Permission
282     *
283     * @return bool Returns true if the permission is set otherwise returns false
284     *
285     * @since 1.0.0
286     */
287    public function hasPermissionFlags(int $permission) : bool
288    {
289        return ($this->getPermission() & $permission) === $permission;
290    }
291
292    /**
293     * Has permissions.
294     *
295     * Checks if the permission is defined
296     *
297     * @param int         $permission Permission to check
298     * @param null|int    $unit       Unit Unit to check (null if all are acceptable)
299     * @param null|int    $app        App App to check  (null if all are acceptable)
300     * @param null|string $module     Module Module to check  (null if all are acceptable)
301     * @param null|int    $category   Category (e.g. customer) (null if all are acceptable)
302     * @param null|int    $element    (e.g. customer id) (null if all are acceptable)
303     * @param null|int    $component  (e.g. address) (null if all are acceptable)
304     *
305     * @return bool Returns true if the permission is set, false otherwise
306     *
307     * @since 1.0.0
308     */
309    public function hasPermission(
310        int $permission,
311        int $unit = null,
312        int $app = null,
313        string $module = null,
314        int $category = null,
315        int $element = null,
316        int $component = null
317    ) : bool
318    {
319        return $permission === PermissionType::NONE ||
320            (($unit === null || $this->unit === null || $this->unit === $unit)
321            && ($app === null || $this->app === null || $this->app === $app)
322            && ($module === null || $this->module === null || $this->module === $module)
323            && ($category === null || $this->category === null || $this->category === $category)
324            && ($element === null || $this->element === null || $this->element === $element)
325            && ($component === null || $this->component === null || $this->component === $component)
326            && ($this->getPermission() & $permission) === $permission);
327    }
328
329    /**
330     * Is equals.
331     *
332     * @param self $permission Permission
333     *
334     * @return bool Returns true if the permission is the same
335     *
336     * @since 1.0.0
337     */
338    public function isEqual(self $permission) : bool
339    {
340        return $this->unit === $permission->unit
341            && $this->app === $permission->app
342            && $this->module === $permission->module
343            && $this->category === $permission->category
344            && $this->element === $permission->element
345            && $this->component === $permission->component
346            && $this->getPermission() === $permission->getPermission();
347    }
348
349    /**
350     * {@inheritdoc}
351     */
352    public function jsonSerialize() : mixed
353    {
354        return [
355            'id'             => $this->id,
356            'unit'           => $this->unit,
357            'app'            => $this->app,
358            'module'         => $this->module,
359            'from'           => $this->from,
360            'category'       => $this->category,
361            'element'        => $this->element,
362            'component'      => $this->component,
363            'permission'     => $this->getPermission(),
364        ];
365    }
366}