Completed
Branch feature/currentUserRefactoring (c13c1d)
by Schlaefer
04:13
created

PermissionConfig   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 6
lcom 3
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A allowAll() 0 6 1
A allowOwner() 0 6 1
A allowRole() 0 6 1
A getOwner() 0 4 1
A getRole() 0 4 1
A getForce() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Saito\User\Permission;
14
15
use Saito\User\Permission\Allowance\Force;
16
use Saito\User\Permission\Allowance\Owner;
17
use Saito\User\Permission\Allowance\Role;
18
19
class PermissionConfig
20
{
21
    protected $boolAllowances = [];
22
23
    protected $roleAllowances = [];
24
25
    protected $ownerAllowances = [];
26
27
    /**
28
     * Allows or forbids without any further check (use with care)
29
     *
30
     * @param string $resource Resource to allow
31
     * @param bool $allowed Force true or false
32
     * @return self
33
     */
34
    public function allowAll(string $resource, bool $allowed = true): self
35
    {
36
        $this->boolAllowances[$resource] = new Force($resource, $allowed);
37
38
        return $this;
39
    }
40
41
    /**
42
     * Allow the owner of the resource to access resource
43
     *
44
     * @param string $resource Resource to allow
45
     * @return self
46
     */
47
    public function allowOwner(string $resource): self
48
    {
49
        $this->ownerAllowances[$resource][] = new Owner($resource);
50
51
        return $this;
52
    }
53
54
    /**
55
     * Allow role for resource
56
     *
57
     * @param string $resource Resource to allow
58
     * @param array|string $role role
59
     * @param array|string|null $object object
60
     * @return self
61
     */
62
    public function allowRole(string $resource, $role, $object = null): self
63
    {
64
        $this->roleAllowances[$resource][] = new Role($resource, $role, $object);
65
66
        return $this;
67
    }
68
69
    /**
70
     * Get owner config
71
     *
72
     * @param string $resource Resource
73
     * @return array
74
     */
75
    public function getOwner(string $resource): array
76
    {
77
        return $this->ownerAllowances[$resource] ?? [];
78
    }
79
80
    /**
81
     * Get roles config
82
     *
83
     * @param string $resource Resource
84
     * @return array
85
     */
86
    public function getRole(string $resource): array
87
    {
88
        return $this->roleAllowances[$resource] ?? [];
89
    }
90
91
    /**
92
     * Get forced config
93
     *
94
     * @param string $resource Resource
95
     * @return Force|null
96
     */
97
    public function getForce(string $resource): ?Force
98
    {
99
        return $this->boolAllowances[$resource] ?? null;
100
    }
101
}
102