Skip to content

Commit 8a0fb85

Browse files
committed
AccessPolicy refactoring
1 parent 3a6024f commit 8a0fb85

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/Application/UI/AccessPolicy.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,27 @@ final class AccessPolicy
2525

2626

2727
public function __construct(
28-
private readonly Component $component,
2928
private readonly \ReflectionClass|\ReflectionMethod $element,
3029
) {
3130
}
3231

3332

34-
public function checkAccess(): void
33+
public function checkAccess(Component $component): void
3534
{
35+
$this->presenter ??= $component->getPresenterIfExists() ??
36+
throw new Nette\InvalidStateException('Presenter is required for checking requirements of ' . Reflection::toString($this->element));
37+
3638
$attrs = $this->getAttributes();
37-
$attrs = self::applyInternalRules($attrs);
39+
$attrs = self::applyInternalRules($attrs, $component);
3840
foreach ($attrs as $attribute) {
3941
$this->checkAttribute($attribute);
4042
}
4143
}
4244

4345

46+
/**
47+
* @return Attributes\Requires[]
48+
*/
4449
private function getAttributes(): array
4550
{
4651
return array_map(
@@ -50,11 +55,11 @@ private function getAttributes(): array
5055
}
5156

5257

53-
private function applyInternalRules(array $attrs): array
58+
private function applyInternalRules(array $attrs, Component $component): array
5459
{
5560
if (
5661
$this->element instanceof \ReflectionMethod
57-
&& str_starts_with($this->element->getName(), $this->component::formatSignalMethod(''))
62+
&& str_starts_with($this->element->getName(), $component::formatSignalMethod(''))
5863
&& !ComponentReflection::parseAnnotation($this->element, 'crossOrigin')
5964
&& !Nette\Utils\Arrays::some($attrs, fn($attr) => $attr->sameOrigin === false)
6065
) {
@@ -66,9 +71,6 @@ private function applyInternalRules(array $attrs): array
6671

6772
private function checkAttribute(Attributes\Requires $attribute): void
6873
{
69-
$this->presenter ??= $this->component->getPresenterIfExists() ??
70-
throw new Nette\InvalidStateException('Presenter is required for checking requirements of ' . Reflection::toString($this->element));
71-
7274
if ($attribute->methods !== null) {
7375
$this->checkHttpMethod($attribute);
7476
}

src/Application/UI/Component.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getUniqueId(): string
7575
protected function createComponent(string $name): ?Nette\ComponentModel\IComponent
7676
{
7777
if (method_exists($this, $method = 'createComponent' . $name)) {
78-
(new AccessPolicy($this, new \ReflectionMethod($this, $method)))->checkAccess();
78+
(new AccessPolicy(new \ReflectionMethod($this, $method)))->checkAccess($this);
7979
}
8080
$res = parent::createComponent($name);
8181
if ($res && !$res instanceof SignalReceiver && !$res instanceof StatePersistent) {
@@ -110,7 +110,7 @@ protected function tryCall(string $method, array $params): bool
110110
}
111111

112112
$rm = $rc->getMethod($method);
113-
(new AccessPolicy($this, $rm))->checkAccess();
113+
(new AccessPolicy($rm))->checkAccess($this);
114114
$this->checkRequirements($rm);
115115
try {
116116
$args = ParameterConverter::toArguments($rm, $params);

src/Application/UI/Presenter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public function run(Application\Request $request): Application\Response
187187

188188
try {
189189
// CHECK REQUIREMENTS
190-
(new AccessPolicy($this, static::getReflection()))->checkAccess();
190+
(new AccessPolicy(static::getReflection()))->checkAccess($this);
191191
$this->checkRequirements(static::getReflection());
192192
$this->checkHttpMethod();
193193

0 commit comments

Comments
 (0)