ApiValidate.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Dotdigitalgroup\Email\Observer\Adminhtml;
  3. /**
  4. * Validate api when saving creds in admin.
  5. */
  6. class ApiValidate implements \Magento\Framework\Event\ObserverInterface
  7. {
  8. /**
  9. * @var \Dotdigitalgroup\Email\Helper\Data
  10. */
  11. private $helper;
  12. /**
  13. * @var \Magento\Backend\App\Action\Context
  14. */
  15. private $context;
  16. /**
  17. * @var \Magento\Framework\Message\ManagerInterface
  18. */
  19. private $messageManager;
  20. /**
  21. * @var \Magento\Framework\App\Config\Storage\Writer
  22. */
  23. private $writer;
  24. /**
  25. * @var \Dotdigitalgroup\Email\Model\Apiconnector\Test
  26. */
  27. private $test;
  28. /**
  29. * ApiValidate constructor.
  30. *
  31. * @param \Dotdigitalgroup\Email\Helper\Data $data
  32. * @param \Dotdigitalgroup\Email\Model\Apiconnector\Test $test
  33. * @param \Magento\Backend\App\Action\Context $context
  34. * @param \Magento\Framework\App\Config\Storage\Writer $writer
  35. */
  36. public function __construct(
  37. \Dotdigitalgroup\Email\Helper\Data $data,
  38. \Dotdigitalgroup\Email\Model\Apiconnector\Test $test,
  39. \Magento\Backend\App\Action\Context $context,
  40. \Magento\Framework\App\Config\Storage\Writer $writer
  41. ) {
  42. $this->test = $test;
  43. $this->helper = $data;
  44. $this->writer = $writer;
  45. $this->context = $context;
  46. $this->messageManager = $context->getMessageManager();
  47. }
  48. /**
  49. * Execute method.
  50. *
  51. * @param \Magento\Framework\Event\Observer $observer
  52. *
  53. * @return $this
  54. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  55. */
  56. public function execute(\Magento\Framework\Event\Observer $observer)
  57. {
  58. $groups = $this->context->getRequest()->getPost('groups');
  59. if (isset($groups['api']['fields']['username']['inherit'])
  60. || isset($groups['api']['fields']['password']['inherit'])
  61. ) {
  62. return $this;
  63. }
  64. $apiUsername = isset($groups['api']['fields']['username']['value'])
  65. ? $groups['api']['fields']['username']['value'] : false;
  66. $apiPassword = isset($groups['api']['fields']['password']['value'])
  67. ? $groups['api']['fields']['password']['value'] : false;
  68. $this->validateAccount($apiUsername, $apiPassword);
  69. return $this;
  70. }
  71. /**
  72. * Validate account
  73. *
  74. * @param string|boolean $apiUsername
  75. * @param string|boolean $apiPassword
  76. * @return void
  77. */
  78. private function validateAccount($apiUsername, $apiPassword)
  79. {
  80. //skip if the inherit option is selected
  81. if ($apiUsername && $apiPassword) {
  82. $this->helper->log('----VALIDATING ACCOUNT---');
  83. $isValid = $this->test->validate($apiUsername, $apiPassword);
  84. if ($isValid) {
  85. $this->messageManager->addSuccessMessage(__('API Credentials Valid.'));
  86. } else {
  87. $this->messageManager->addWarningMessage(__('Authorization has been denied for this request.'));
  88. }
  89. }
  90. }
  91. }