-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Willem Poortman
committed
Jul 4, 2023
1 parent
976c00b
commit 56d1aca
Showing
6 changed files
with
171 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
/** | ||
* Copyright © Willem Poortman 2021-present. All rights reserved. | ||
* | ||
* Please read the README and LICENSE files for more | ||
* details on copyrights and license information. | ||
*/ | ||
|
||
namespace Magewirephp\Magewire\Model\Request; | ||
|
||
/** | ||
* Marker for actions processing a subsequent Magewire POST requests. | ||
* | ||
* @api | ||
*/ | ||
interface MagewireSubsequentActionInterface | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Magewirephp\Magewire\Model\Request; | ||
|
||
use Magento\Framework\App\ActionInterface; | ||
use Magento\Framework\App\Request\InvalidRequestException; | ||
use Magento\Framework\App\Request\ValidatorInterface; | ||
use Magento\Framework\App\RequestInterface; | ||
use Magento\Framework\Controller\Result\JsonFactory; | ||
use Magento\Framework\Phrase; | ||
use Magento\Framework\Serialize\SerializerInterface; | ||
|
||
class MagewireValidator implements ValidatorInterface | ||
{ | ||
protected SerializerInterface $serializer; | ||
protected JsonFactory $resultJsonFactory; | ||
|
||
public function __construct( | ||
SerializerInterface $serializer, | ||
JsonFactory $resultJsonFactory | ||
) { | ||
$this->serializer = $serializer; | ||
$this->resultJsonFactory = $resultJsonFactory; | ||
} | ||
|
||
public function validate(RequestInterface $request, ActionInterface $action): void | ||
{ | ||
if (! $action instanceof MagewireSubsequentActionInterface) { | ||
return; | ||
} | ||
|
||
$input = $this->serializer->unserialize(file_get_contents('php://input')); | ||
$handle = $input['fingerprint']['handle'] ?? null; | ||
|
||
if (! $handle || preg_match('/^[a-zA-Z0-9][a-zA-Z\d\-_\.]*$/', $handle) !== 1) { | ||
$message = 'Bad Request'; | ||
|
||
$result = $this->resultJsonFactory->create(); | ||
$result->setStatusHeader(400); | ||
|
||
$result->setData([ | ||
'message'=> $message, | ||
'code' => 400 | ||
]); | ||
|
||
throw new InvalidRequestException($result, [new Phrase($message)]); | ||
} | ||
|
||
$request->setParams($input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters