-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from JaroslawZielinski/bugfix/csrf-verse-edit-…
…fix-ajax-check CSRF Verse Edit fix ajax check
- Loading branch information
Showing
6 changed files
with
152 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JaroslawZielinski\TorahVerse\Controller\Adminhtml; | ||
|
||
use Magento\Backend\App\Action; | ||
use Magento\Backend\App\Action\Context; | ||
use Magento\Backend\Block\Admin\Formkey; | ||
use Magento\Framework\App\Request\Http; | ||
use Magento\Framework\App\RequestInterface; | ||
use Magento\Framework\Controller\Result\Json; | ||
use Magento\Framework\Controller\Result\JsonFactory; | ||
|
||
abstract class Ajax extends Action | ||
{ | ||
/** | ||
* @var FormKey | ||
*/ | ||
private $formKey; | ||
/** | ||
* @var JsonFactory | ||
*/ | ||
protected $resultJsonFactory; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function __construct( | ||
FormKey $formKey, | ||
JsonFactory $resultJsonFactory, | ||
Context $context | ||
) { | ||
$this->formKey = $formKey; | ||
$this->resultJsonFactory = $resultJsonFactory; | ||
parent::__construct($context); | ||
} | ||
|
||
public function ajax(array $data): Json | ||
{ | ||
$result = $this->resultJsonFactory->create(); | ||
/** @var RequestInterface|Http $request */ | ||
$request = $this->getRequest(); | ||
if (!$request->isAjax()) { | ||
return $result->setData([ | ||
'status' => 'ERROR', | ||
'message' => __('It is not an ajax.'), | ||
'result' => __('It is not an ajax.') | ||
]); | ||
} | ||
if ($request->getParam('form_key', '') !== $this->formKey->getFormKey()) { | ||
return $result->setData([ | ||
'status' => 'ERROR', | ||
'message' => __('CSRF attack possible.'), | ||
'result' => __('Form is broken.') | ||
]); | ||
} | ||
return $result->setData($data); | ||
} | ||
} |
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,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JaroslawZielinski\TorahVerse\Controller\Adminhtml\Siglum; | ||
|
||
use JaroslawZielinski\TorahVerse\Controller\Adminhtml\Ajax; | ||
use Magento\Backend\App\Action\Context; | ||
use Magento\Framework\Controller\Result\JsonFactory; | ||
use GuzzleHttp\Client; | ||
use JaroslawZielinski\Torah\Bible\Service; | ||
use JaroslawZielinski\Torah\Bible\Torah; | ||
use JaroslawZielinski\Torah\Bible\Torah\SiglumFactory; | ||
use JaroslawZielinski\Torah\Bible\TorahValidator; | ||
use Psr\Log\LoggerInterface; | ||
use JaroslawZielinski\TorahVerse\Model\Config; | ||
use Magento\Backend\Block\Admin\Formkey; | ||
|
||
class Preview extends Ajax | ||
{ | ||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
/** | ||
* @var LoggerInterface | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function __construct( | ||
Config $config, | ||
LoggerInterface $logger, | ||
FormKey $formKey, | ||
JsonFactory $resultJsonFactory, | ||
Context $context | ||
) { | ||
$this->config = $config; | ||
$this->logger = $logger; | ||
parent::__construct($formKey, $resultJsonFactory, $context); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function execute() | ||
{ | ||
try { | ||
$client = new Service\Client($this->logger, new Client()); | ||
$torah = new Torah(new TorahValidator(), new Service($client)); | ||
$request = $this->getRequest(); | ||
$translationParameter = $request->getParam('translation'); | ||
if (empty($translationParameter)) { | ||
throw new \Exception('Translation is not set!'); | ||
} | ||
$siglumParameter = $request->getParam('siglum'); | ||
if (empty(trim($siglumParameter))) { | ||
throw new \Exception('String is empty!'); | ||
} | ||
$siglum = SiglumFactory::createFromTranslationAndString($translationParameter, $siglumParameter); | ||
$language = $this->config->getInternalizationLanguage(); | ||
$text = $torah->getTextBySiglum($siglum, $language); | ||
if (!empty($text)) { | ||
$content = $text->getOrdered(); | ||
$description = $text->getDescription(); | ||
$previewText = <<<EOT | ||
{$content} | ||
{$description} | ||
EOT; | ||
} else { | ||
$previewText = implode(<<<EOT | ||
EOT, $torah->getErrors()); | ||
} | ||
$data['status'] = 'ok'; | ||
} catch (\Exception $e) { | ||
$previewText = $e->getMessage(); | ||
$data['status'] = 'error'; | ||
} | ||
$data['result'] = $previewText; | ||
return $this->ajax($data); | ||
} | ||
} |
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
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