Completed
Branch release/5.5.0 (ca3c12)
by Schlaefer
09:46
created

Resource::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
class Resource
16
{
17
    /** @var string resource name */
18
    protected $name;
19
20
    /** @var ResourceAC[] Allowed permissions */
21
    protected $allowed = [];
22
23
    /** @var ResourceAC[] Disallowed permissions */
24
    protected $disallowed = [];
25
26
    /**
27
     * Constructor
28
     *
29
     * @param string $name resource name
30
     */
31
    public function __construct(string $name)
32
    {
33
        $this->name = $name;
34
    }
35
36
    /**
37
     * Get resource name
38
     *
39
     * @return string
40
     */
41
    public function getName(): string
42
    {
43
        return $this->name;
44
    }
45
46
    /**
47
     * Allow the resource on a permission
48
     *
49
     * @param ResourceAC $permission permission
50
     * @return self
51
     */
52
    public function allow(ResourceAC $permission): self
53
    {
54
        $permission->lock();
55
        $this->allowed[] = $permission;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Disallow the resource on a permission
62
     *
63
     * @param ResourceAC $permission permission
64
     * @return self
65
     */
66
    public function disallow(ResourceAC $permission): self
67
    {
68
        $permission->lock();
69
        $this->disallowed[] = $permission;
70
71
        return $this;
72
    }
73
74
    /**
75
     * Check resource against identity
76
     *
77
     * @param ResourceAI $identity Identity
78
     * @return bool
79
     */
80
    public function check(ResourceAI $identity): bool
81
    {
82
        foreach ($this->disallowed as $permission) {
83
            if ($permission->check($identity)) {
84
                return false;
85
            }
86
        }
87
88
        foreach ($this->allowed as $permission) {
89
            if ($permission->check($identity)) {
90
                return true;
91
            }
92
        }
93
94
        return false;
95
    }
96
}
97