|
1
|
|
|
<?php namespace Limoncello\Tests\Auth\Authorization\PolicyEnforcement\Data\Policies; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2017 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Auth\Authorization\PolicyAdministration\AllOf; |
|
20
|
|
|
use Limoncello\Auth\Authorization\PolicyAdministration\AnyOf; |
|
21
|
|
|
use Limoncello\Auth\Authorization\PolicyAdministration\Rule; |
|
22
|
|
|
use Limoncello\Auth\Authorization\PolicyAdministration\Target; |
|
23
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\RuleInterface; |
|
24
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\TargetInterface; |
|
25
|
|
|
use Limoncello\Auth\Contracts\Authorization\PolicyInformation\ContextInterface; |
|
26
|
|
|
use Limoncello\Tests\Auth\Authorization\PolicyEnforcement\Data\ContextProperties; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @package Limoncello\Tests\Auth |
|
30
|
|
|
*/ |
|
31
|
|
|
abstract class General |
|
32
|
|
|
{ |
|
33
|
|
|
/** Operation identity */ |
|
34
|
|
|
const OPERATION_CREATE = 'create'; |
|
35
|
|
|
|
|
36
|
|
|
/** Operation identity */ |
|
37
|
|
|
const OPERATION_READ = 'read'; |
|
38
|
|
|
|
|
39
|
|
|
/** Operation identity */ |
|
40
|
|
|
const OPERATION_UPDATE = 'update'; |
|
41
|
|
|
|
|
42
|
|
|
/** Operation identity */ |
|
43
|
|
|
const OPERATION_DELETE = 'delete'; |
|
44
|
|
|
|
|
45
|
|
|
/** Operation identity */ |
|
46
|
|
|
const OPERATION_INDEX = 'index'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param ContextInterface $context |
|
50
|
|
|
* |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function isAdmin(ContextInterface $context) |
|
54
|
|
|
{ |
|
55
|
|
|
$curUserRole = $context->get(ContextProperties::PARAM_CURRENT_USER_ROLE); |
|
56
|
|
|
$result = $curUserRole === 'admin'; |
|
57
|
|
|
|
|
58
|
|
|
return $result; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param string|int $key |
|
63
|
|
|
* @param string|int|float $value (any scalar) |
|
64
|
|
|
* |
|
65
|
|
|
* @return TargetInterface |
|
66
|
|
|
*/ |
|
67
|
|
|
protected static function target($key, $value) |
|
68
|
|
|
{ |
|
69
|
|
|
return static::targetMulti([$key => $value]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $properties |
|
75
|
|
|
* |
|
76
|
|
|
* @return TargetInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
protected static function targetMulti(array $properties) |
|
79
|
|
|
{ |
|
80
|
|
|
$target = new Target(new AnyOf([new AllOf($properties)])); |
|
81
|
|
|
|
|
82
|
|
|
$stringPairs = []; |
|
83
|
|
|
foreach ($properties as $key => $value) { |
|
84
|
|
|
$stringPairs[] = "$key=$value"; |
|
85
|
|
|
} |
|
86
|
|
|
$target->setName(implode(',', $stringPairs)); |
|
87
|
|
|
|
|
88
|
|
|
return $target; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return TargetInterface |
|
93
|
|
|
*/ |
|
94
|
|
|
protected static function targetOperationRead() |
|
95
|
|
|
{ |
|
96
|
|
|
return static::target(ContextProperties::PARAM_OPERATION, static::OPERATION_READ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return TargetInterface |
|
101
|
|
|
*/ |
|
102
|
|
|
protected static function targetOperationUpdate() |
|
103
|
|
|
{ |
|
104
|
|
|
return static::target(ContextProperties::PARAM_OPERATION, static::OPERATION_UPDATE); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return TargetInterface |
|
109
|
|
|
*/ |
|
110
|
|
|
protected static function targetOperationDelete() |
|
111
|
|
|
{ |
|
112
|
|
|
return static::target(ContextProperties::PARAM_OPERATION, static::OPERATION_DELETE); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return TargetInterface |
|
117
|
|
|
*/ |
|
118
|
|
|
protected static function targetOperationIndex() |
|
119
|
|
|
{ |
|
120
|
|
|
return static::target(ContextProperties::PARAM_OPERATION, static::OPERATION_INDEX); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return RuleInterface |
|
125
|
|
|
*/ |
|
126
|
|
|
protected static function rulePermit() |
|
127
|
|
|
{ |
|
128
|
|
|
return (new Rule())->setName('permit'); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|