Authorize.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. namespace Amazon\Login\Controller\Login;
  17. use Amazon\Core\Api\Data\AmazonCustomerInterface;
  18. use Amazon\Login\Domain\ValidationCredentials;
  19. use Magento\Framework\Exception\ValidatorException;
  20. use Magento\Framework\Exception\NotFoundException;
  21. use Zend_Validate;
  22. class Authorize extends \Amazon\Login\Controller\Login
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function execute()
  28. {
  29. if (!$this->amazonCoreHelper->isLwaEnabled()) {
  30. throw new NotFoundException(__('Action is not available'));
  31. }
  32. if (!$this->isValidToken()) {
  33. return $this->getRedirectLogin();
  34. }
  35. try {
  36. $amazonCustomer = $this->getAmazonCustomer();
  37. if ($amazonCustomer) {
  38. $processed = $this->processAmazonCustomer($amazonCustomer);
  39. if ($processed instanceof ValidationCredentials) {
  40. $this->session->setValidationCredentials($processed);
  41. $this->session->setAmazonCustomer($amazonCustomer);
  42. return $this->_redirect($this->_url->getRouteUrl('*/*/validate'));
  43. } else {
  44. $this->session->login($processed);
  45. }
  46. }
  47. } catch (ValidatorException $e) {
  48. $this->logger->error($e);
  49. $this->messageManager->addErrorMessage($e->getMessage());
  50. $this->_eventManager->dispatch('amazon_login_authorize_validation_error', ['exception' => $e]);
  51. $this->_eventManager->dispatch('amazon_login_authorize_error', ['exception' => $e]);
  52. } catch (\Exception $e) {
  53. $this->logger->error($e);
  54. $this->messageManager->addErrorMessage(__('Error processing Amazon Login'));
  55. $this->_eventManager->dispatch('amazon_login_authorize_error', ['exception' => $e]);
  56. }
  57. return $this->getRedirectAccount();
  58. }
  59. protected function processAmazonCustomer(AmazonCustomerInterface $amazonCustomer)
  60. {
  61. $customerData = $this->matcher->match($amazonCustomer);
  62. if (null === $customerData) {
  63. return $this->createCustomer($amazonCustomer);
  64. }
  65. if ($amazonCustomer->getId() != $customerData->getExtensionAttributes()->getAmazonId()) {
  66. if (! $this->session->isLoggedIn()) {
  67. return new ValidationCredentials($customerData->getId(), $amazonCustomer->getId());
  68. }
  69. $this->customerLinkManagement->updateLink($customerData->getId(), $amazonCustomer->getId());
  70. }
  71. return $customerData;
  72. }
  73. protected function createCustomer(AmazonCustomerInterface $amazonCustomer)
  74. {
  75. if (! Zend_Validate::is($amazonCustomer->getEmail(), 'EmailAddress')) {
  76. throw new ValidatorException(__('the email address for your Amazon account is invalid'));
  77. }
  78. $customerData = $this->customerLinkManagement->create($amazonCustomer);
  79. $this->customerLinkManagement->updateLink($customerData->getId(), $amazonCustomer->getId());
  80. return $customerData;
  81. }
  82. }