1 | <?php namespace Limoncello\Tests\Auth\Authorization\PolicyEnforcement\Data\Policies; |
||
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 |