1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Auth\Authorization\PolicyDecision; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Auth\Authorization\PolicyDecision\Algorithms\BasePolicyOrSetAlgorithm; |
22
|
|
|
use Limoncello\Auth\Authorization\PolicyDecision\Algorithms\DefaultTargetSerializeTrait; |
23
|
|
|
use Limoncello\Auth\Authorization\PolicyDecision\Algorithms\Encoder; |
24
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\PolicySetInterface; |
25
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyDecision\PolicyDecisionPointInterface; |
26
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyInformation\ContextInterface; |
27
|
|
|
use Psr\Log\LoggerAwareTrait; |
28
|
|
|
use Psr\Log\LoggerInterface; |
29
|
|
|
use function assert; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @package Limoncello\Auth |
33
|
|
|
*/ |
34
|
|
|
class PolicyDecisionPoint implements PolicyDecisionPointInterface |
35
|
|
|
{ |
36
|
|
|
use DefaultTargetSerializeTrait, LoggerAwareTrait; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $encodePolicySet; |
42
|
|
|
|
43
|
8 |
|
/** |
44
|
|
|
* @param PolicySetInterface|array $data |
45
|
8 |
|
*/ |
46
|
|
|
public function __construct($data) |
47
|
|
|
{ |
48
|
|
|
$data instanceof PolicySetInterface ? $this->initWithPolicySet($data) : $this->initWithData($data); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param ContextInterface $context |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
8 |
|
* |
56
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
57
|
8 |
|
*/ |
58
|
|
|
public function evaluate(ContextInterface $context): array |
59
|
8 |
|
{ |
60
|
|
|
$logger = $this->getLogger(); |
61
|
8 |
|
|
62
|
8 |
|
$set = $this->getEncodePolicySet(); |
63
|
|
|
|
64
|
8 |
|
$name = Encoder::policySetName($set); |
65
|
8 |
|
$logger === null ?: $logger->debug("Policy Decision Point evaluates '$name' policy set."); |
66
|
8 |
|
|
67
|
|
|
$target = Encoder::policySetTarget($set); |
68
|
8 |
|
$match = $this->evaluateTarget($context, $target, $logger); |
69
|
|
|
$result = BasePolicyOrSetAlgorithm::evaluateItem($context, $match, $this->getEncodePolicySet(), $logger); |
70
|
8 |
|
|
71
|
|
|
$logger === null ?: $logger->debug("Policy Decision Point evaluated '$name' policy set."); |
72
|
|
|
|
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
8 |
|
/** |
77
|
|
|
* @return array |
78
|
8 |
|
*/ |
79
|
|
|
public function getEncodePolicySet(): array |
80
|
|
|
{ |
81
|
|
|
return $this->encodePolicySet; |
82
|
|
|
} |
83
|
|
|
|
84
|
8 |
|
/** |
85
|
|
|
* @return LoggerInterface|null |
86
|
8 |
|
*/ |
87
|
|
|
protected function getLogger(): ?LoggerInterface |
88
|
|
|
{ |
89
|
|
|
return $this->logger; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param PolicySetInterface $policySet |
94
|
8 |
|
* |
95
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
96
|
8 |
|
*/ |
97
|
|
|
protected function initWithPolicySet(PolicySetInterface $policySet): void |
98
|
|
|
{ |
99
|
|
|
$this->initWithData(Encoder::encodePolicySet($policySet)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param array $encodePolicySet |
104
|
8 |
|
* |
105
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
106
|
8 |
|
*/ |
107
|
|
|
protected function initWithData(array $encodePolicySet): void |
108
|
8 |
|
{ |
109
|
|
|
assert(Encoder::isPolicySet($encodePolicySet)); |
110
|
|
|
|
111
|
|
|
$this->setEncodePolicySet($encodePolicySet); |
112
|
|
|
} |
113
|
|
|
|
114
|
8 |
|
/** |
115
|
|
|
* @param array $encodePolicySet |
116
|
8 |
|
*/ |
117
|
|
|
protected function setEncodePolicySet(array $encodePolicySet): void |
118
|
|
|
{ |
119
|
|
|
$this->encodePolicySet = $encodePolicySet; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|