Skip to content

Commit ac1a866

Browse files
committed
Merge remote-tracking branch 'origin/MC-17320' into borg-security-2.2
2 parents 7e7defa + d1a8240 commit ac1a866

File tree

10 files changed

+597
-58
lines changed

10 files changed

+597
-58
lines changed

app/code/Magento/Cms/Controller/Adminhtml/Page/InlineEdit.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public function __construct(
5656
}
5757

5858
/**
59+
* Process the request
60+
*
5961
* @return \Magento\Framework\Controller\ResultInterface
6062
* @throws \Magento\Framework\Exception\LocalizedException
6163
*/
@@ -68,10 +70,12 @@ public function execute()
6870

6971
$postItems = $this->getRequest()->getParam('items', []);
7072
if (!($this->getRequest()->getParam('isAjax') && count($postItems))) {
71-
return $resultJson->setData([
72-
'messages' => [__('Please correct the data sent.')],
73-
'error' => true,
74-
]);
73+
return $resultJson->setData(
74+
[
75+
'messages' => [__('Please correct the data sent.')],
76+
'error' => true,
77+
]
78+
);
7579
}
7680

7781
foreach (array_keys($postItems) as $pageId) {
@@ -98,10 +102,12 @@ public function execute()
98102
}
99103
}
100104

101-
return $resultJson->setData([
102-
'messages' => $messages,
103-
'error' => $error
104-
]);
105+
return $resultJson->setData(
106+
[
107+
'messages' => $messages,
108+
'error' => $error
109+
]
110+
);
105111
}
106112

107113
/**
@@ -131,7 +137,7 @@ protected function filterPost($postData = [])
131137
*/
132138
protected function validatePost(array $pageData, \Magento\Cms\Model\Page $page, &$error, array &$messages)
133139
{
134-
if (!($this->dataProcessor->validate($pageData) && $this->dataProcessor->validateRequireEntry($pageData))) {
140+
if (!$this->dataProcessor->validateRequireEntry($pageData)) {
135141
$error = true;
136142
foreach ($this->messageManager->getMessages(true)->getItems() as $error) {
137143
$messages[] = $this->getErrorWithPageId($page, $error->getText());

app/code/Magento/Cms/Controller/Adminhtml/Page/PostDataProcessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
use Magento\Framework\Config\Dom\ValidationSchemaException;
1313

1414
/**
15-
* Class PostDataProcessor
16-
* @package Magento\Cms\Controller\Adminhtml\Page
15+
* Processes form data
1716
*/
1817
class PostDataProcessor
1918
{
@@ -80,6 +79,7 @@ public function filter($data)
8079
*
8180
* @param array $data
8281
* @return bool Return FALSE if some item is invalid
82+
* @deprecated
8383
*/
8484
public function validate($data)
8585
{

app/code/Magento/Cms/Controller/Adminhtml/Page/Save.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ public function execute()
104104
['page' => $model, 'request' => $this->getRequest()]
105105
);
106106

107-
if (!$this->dataProcessor->validate($data)) {
108-
return $resultRedirect->setPath('*/*/edit', ['page_id' => $model->getId(), '_current' => true]);
109-
}
110-
111107
try {
112108
$this->pageRepository->save($model);
113109
$this->messageManager->addSuccessMessage(__('You saved the page.'));
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Cms\Model\PageRepository;
10+
11+
use Magento\Cms\Api\Data\PageInterface;
12+
use Magento\Cms\Api\PageRepositoryInterface;
13+
use Magento\Framework\Api\SearchCriteriaInterface;
14+
15+
/**
16+
* Validates and saves a page
17+
*/
18+
class ValidationComposite implements PageRepositoryInterface
19+
{
20+
/**
21+
* @var PageRepositoryInterface
22+
*/
23+
private $repository;
24+
25+
/**
26+
* @var array
27+
*/
28+
private $validators;
29+
30+
/**
31+
* @param PageRepositoryInterface $repository
32+
* @param ValidatorInterface[] $validators
33+
*/
34+
public function __construct(
35+
PageRepositoryInterface $repository,
36+
array $validators = []
37+
) {
38+
foreach ($validators as $validator) {
39+
if (!$validator instanceof ValidatorInterface) {
40+
throw new \InvalidArgumentException(
41+
sprintf('Supplied validator does not implement %s', ValidatorInterface::class)
42+
);
43+
}
44+
}
45+
$this->repository = $repository;
46+
$this->validators = $validators;
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function save(PageInterface $page)
53+
{
54+
foreach ($this->validators as $validator) {
55+
$validator->validate($page);
56+
}
57+
58+
return $this->repository->save($page);
59+
}
60+
61+
/**
62+
* @inheritdoc
63+
*/
64+
public function getById($pageId)
65+
{
66+
return $this->repository->getById($pageId);
67+
}
68+
69+
/**
70+
* @inheritdoc
71+
*/
72+
public function getList(SearchCriteriaInterface $searchCriteria)
73+
{
74+
return $this->repository->getList($searchCriteria);
75+
}
76+
77+
/**
78+
* @inheritdoc
79+
*/
80+
public function delete(PageInterface $page)
81+
{
82+
return $this->repository->delete($page);
83+
}
84+
85+
/**
86+
* @inheritdoc
87+
*/
88+
public function deleteById($pageId)
89+
{
90+
return $this->repository->deleteById($pageId);
91+
}
92+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Cms\Model\PageRepository\Validator;
10+
11+
use Magento\Cms\Api\Data\PageInterface;
12+
use Magento\Cms\Model\PageRepository\ValidatorInterface;
13+
use Magento\Framework\Config\Dom\ValidationException;
14+
use Magento\Framework\Config\Dom\ValidationSchemaException;
15+
use Magento\Framework\Config\ValidationStateInterface;
16+
use Magento\Framework\Exception\LocalizedException;
17+
use Magento\Framework\View\Model\Layout\Update\Validator;
18+
use Magento\Framework\View\Model\Layout\Update\ValidatorFactory;
19+
20+
/**
21+
* Validate a given page
22+
*/
23+
class LayoutUpdateValidator implements ValidatorInterface
24+
{
25+
/**
26+
* @var ValidatorFactory
27+
*/
28+
private $validatorFactory;
29+
30+
/**
31+
* @var ValidationStateInterface
32+
*/
33+
private $validationState;
34+
35+
/**
36+
* @param ValidatorFactory $validatorFactory
37+
* @param ValidationStateInterface $validationState
38+
*/
39+
public function __construct(
40+
ValidatorFactory $validatorFactory,
41+
ValidationStateInterface $validationState
42+
) {
43+
$this->validatorFactory = $validatorFactory;
44+
$this->validationState = $validationState;
45+
}
46+
47+
/**
48+
* Validate the data before saving
49+
*
50+
* @param PageInterface $page
51+
* @throws LocalizedException
52+
*/
53+
public function validate(PageInterface $page)
54+
{
55+
$this->validateRequiredFields($page);
56+
$this->validateLayoutUpdate($page);
57+
$this->validateCustomLayoutUpdate($page);
58+
}
59+
60+
/**
61+
* Validate required fields
62+
*
63+
* @param PageInterface $page
64+
* @throws LocalizedException
65+
*/
66+
private function validateRequiredFields(PageInterface $page)
67+
{
68+
if (empty($page->getTitle())) {
69+
throw new LocalizedException(__('Required field "%1" is empty.', 'title'));
70+
}
71+
}
72+
73+
/**
74+
* Validate layout update
75+
*
76+
* @param PageInterface $page
77+
* @throws LocalizedException
78+
*/
79+
private function validateLayoutUpdate(PageInterface $page)
80+
{
81+
$layoutXmlValidator = $this->getLayoutValidator();
82+
83+
try {
84+
if (!empty($page->getLayoutUpdateXml())
85+
&& !$layoutXmlValidator->isValid($page->getLayoutUpdateXml())
86+
) {
87+
throw new LocalizedException(__('Layout update is invalid'));
88+
}
89+
} catch (ValidationException $e) {
90+
throw new LocalizedException(__('Layout update is invalid'));
91+
} catch (ValidationSchemaException $e) {
92+
throw new LocalizedException(__('Layout update is invalid'));
93+
}
94+
}
95+
96+
/**
97+
* Validate custom layout update
98+
*
99+
* @param PageInterface $page
100+
* @throws LocalizedException
101+
*/
102+
private function validateCustomLayoutUpdate(PageInterface $page)
103+
{
104+
$layoutXmlValidator = $this->getLayoutValidator();
105+
106+
try {
107+
if (!empty($page->getCustomLayoutUpdateXml())
108+
&& !$layoutXmlValidator->isValid($page->getCustomLayoutUpdateXml())
109+
) {
110+
throw new LocalizedException(__('Custom layout update is invalid'));
111+
}
112+
} catch (ValidationException $e) {
113+
throw new LocalizedException(__('Custom layout update is invalid'));
114+
} catch (ValidationSchemaException $e) {
115+
throw new LocalizedException(__('Custom layout update is invalid'));
116+
}
117+
}
118+
119+
/**
120+
* Return a new validator
121+
*
122+
* @return Validator
123+
*/
124+
private function getLayoutValidator(): Validator
125+
{
126+
return $this->validatorFactory->create(
127+
[
128+
'validationState' => $this->validationState,
129+
]
130+
);
131+
}
132+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Cms\Model\PageRepository;
10+
11+
use Magento\Cms\Api\Data\PageInterface;
12+
use Magento\Framework\Exception\LocalizedException;
13+
14+
/**
15+
* Validate a page repository
16+
*/
17+
interface ValidatorInterface
18+
{
19+
/**
20+
* Assert the given page valid
21+
*
22+
* @param PageInterface $page
23+
* @return void
24+
* @throws LocalizedException
25+
*/
26+
public function validate(PageInterface $page);
27+
}

0 commit comments

Comments
 (0)