Skip to content

Commit 0ec2a7b

Browse files
committed
Add FilterScope to every Filter
1 parent 7a892bc commit 0ec2a7b

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

Filter/Security.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Loki\AdminComponents\Filter;
4+
5+
use Loki\AdminComponents\Component\Form\FormRepository;
6+
use Loki\AdminComponents\Component\Form\FormViewModel;
7+
use Loki\AdminComponents\Form\Field\Field;
8+
use Loki\Components\Component\ComponentInterface;
9+
use Loki\Components\Filter\FilterInterface;
10+
use Loki\Components\Filter\FilterScope;
11+
12+
class Security implements FilterInterface
13+
{
14+
public function filter(mixed $value, FilterScope $scope): mixed
15+
{
16+
if (is_object($value)) {
17+
return $value;
18+
}
19+
20+
$value = (string)$value;
21+
if ($this->allowStripTags($value, $scope)) {
22+
$value = strip_tags($value);
23+
}
24+
25+
$value = htmlspecialchars_decode($value);
26+
return $value;
27+
}
28+
29+
private function allowStripTags(string $value, FilterScope $scope): bool
30+
{
31+
$component = $scope->getComponent();
32+
if (false === $component instanceof ComponentInterface) {
33+
return true;
34+
}
35+
36+
$repository = $component->getRepository();
37+
if (false === $repository instanceof FormRepository) {
38+
return true;
39+
}
40+
41+
$property = $scope->getProperty();
42+
if (empty($property)) {
43+
return true;
44+
}
45+
46+
$field = $this->getFieldByPropertyName($repository, $property);
47+
if (empty($field)) {
48+
return true;
49+
}
50+
51+
return !$field->allowHtml();
52+
}
53+
54+
private function getFieldByPropertyName(FormRepository $repository, string $propertyName): ?Field
55+
{
56+
/** @var FormViewModel $viewModel */
57+
$viewModel = $repository->getComponent()->getViewModel();
58+
$fields = $viewModel->getFields();
59+
foreach ($fields as $field) {
60+
if ($field->getCode() === $propertyName) {
61+
return $field;
62+
}
63+
}
64+
65+
return null;
66+
}
67+
}

Form/Field/Field.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace Loki\AdminComponents\Form\Field;
55

6+
use Loki\AdminComponents\Form\Field\FieldType\Editor;
67
use Magento\Framework\DataObject;
78
use Magento\Framework\View\Element\AbstractBlock;
89

@@ -30,6 +31,15 @@ public function isRequired(): bool
3031
return (bool)$this->getData('required');
3132
}
3233

34+
public function allowHtml(): bool
35+
{
36+
if ($this->getFieldType() instanceof Editor) {
37+
return true;
38+
}
39+
40+
return (bool)$this->getData('allow_html');
41+
}
42+
3343
public function getScope(): string
3444
{
3545
return (string)$this->getData('scope');

etc/adminhtml/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
<plugin name="Loki_AdminComponents::addCspInlineScripts" type="Loki\AdminComponents\Plugin\AddCspInlineScripts"/>
99
</type>
1010

11+
<type name="Loki\Components\Filter\FilterRegistry">
12+
<arguments>
13+
<argument name="filters" xsi:type="array">
14+
<item name="security" xsi:type="object">Loki\AdminComponents\Filter\Security</item>
15+
</argument>
16+
</arguments>
17+
</type>
18+
1119
<type name="Loki\AdminComponents\ViewModel\Form\Field\Editor">
1220
<arguments>
1321
<argument name="toolbar" xsi:type="array">

0 commit comments

Comments
 (0)