Refresh.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Refreshes captcha and returns JSON encoded URL to image (AJAX action)
  4. * Example: {'imgSrc': 'http://example.com/media/captcha/67842gh187612ngf8s.png'}
  5. *
  6. * Copyright © Magento, Inc. All rights reserved.
  7. * See COPYING.txt for license details.
  8. */
  9. namespace Magento\Captcha\Controller\Adminhtml\Refresh;
  10. class Refresh extends \Magento\Backend\App\Action
  11. {
  12. /**
  13. * @var \Magento\Framework\Serialize\Serializer\Json
  14. */
  15. protected $serializer;
  16. /**
  17. * @var \Magento\Captcha\Helper\Data
  18. */
  19. protected $captchaHelper;
  20. /**
  21. * Refresh constructor.
  22. * @param \Magento\Backend\App\Action\Context $context
  23. * @param \Magento\Captcha\Helper\Data $captchaHelper
  24. * @param \Magento\Framework\Serialize\Serializer\Json $serializer
  25. */
  26. public function __construct(
  27. \Magento\Backend\App\Action\Context $context,
  28. \Magento\Captcha\Helper\Data $captchaHelper,
  29. \Magento\Framework\Serialize\Serializer\Json $serializer
  30. ) {
  31. parent::__construct($context);
  32. $this->serializer = $serializer;
  33. $this->captchaHelper = $captchaHelper;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function execute()
  39. {
  40. $formId = $this->getRequest()->getPost('formId');
  41. $captchaModel = $this->captchaHelper->getCaptcha($formId);
  42. $this->_view->getLayout()->createBlock(
  43. $captchaModel->getBlockName()
  44. )->setFormId(
  45. $formId
  46. )->setIsAjax(
  47. true
  48. )->toHtml();
  49. $this->getResponse()->representJson($this->serializer->serialize(['imgSrc' => $captchaModel->getImgSrc()]));
  50. $this->_actionFlag->set('', self::FLAG_NO_POST_DISPATCH, true);
  51. }
  52. /**
  53. * Check if user has permissions to access this controller
  54. *
  55. * @return bool
  56. */
  57. protected function _isAllowed()
  58. {
  59. return true;
  60. }
  61. }