ValidatePost.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Login\Api\CustomerLinkManagementInterface;
  18. use Amazon\Login\Domain\ValidationCredentials;
  19. use Amazon\Login\Helper\Session;
  20. use Amazon\Login\Model\Customer\Account\Redirect as AccountRedirect;
  21. use Magento\Customer\Model\CustomerRegistry;
  22. use Magento\Framework\App\Action\Action;
  23. use Magento\Framework\App\Action\Context;
  24. use Magento\Framework\Encryption\Encryptor;
  25. class ValidatePost extends Action
  26. {
  27. /**
  28. * @var Session
  29. */
  30. private $session;
  31. /**
  32. * @var AccountRedirect
  33. */
  34. private $accountRedirect;
  35. /**
  36. * @var CustomerRegistry
  37. */
  38. private $customerRegistry;
  39. /**
  40. * @var Encryptor
  41. */
  42. private $encryptor;
  43. /**
  44. * @var CustomerLinkManagement
  45. */
  46. private $customerLinkManagement;
  47. /**
  48. * ValidatePost constructor.
  49. *
  50. * @param Context $context
  51. * @param Session $session
  52. * @param AccountRedirect $accountRedirect
  53. * @param CustomerRegistry $customerRegistry
  54. * @param Encryptor $encryptor
  55. * @param customerLinkManagement $customerLinkManagement
  56. */
  57. public function __construct(
  58. Context $context,
  59. Session $session,
  60. AccountRedirect $accountRedirect,
  61. CustomerRegistry $customerRegistry,
  62. Encryptor $encryptor,
  63. CustomerLinkManagementInterface $customerLinkManagement
  64. ) {
  65. parent::__construct($context);
  66. $this->session = $session;
  67. $this->accountRedirect = $accountRedirect;
  68. $this->customerRegistry = $customerRegistry;
  69. $this->encryptor = $encryptor;
  70. $this->customerLinkManagement = $customerLinkManagement;
  71. }
  72. public function execute()
  73. {
  74. $credentials = $this->session->getValidationCredentials();
  75. if (null !== $credentials && $credentials instanceof ValidationCredentials) {
  76. $password = $this->getRequest()->getParam('password');
  77. $hash = $this->customerRegistry->retrieveSecureData($credentials->getCustomerId())->getPasswordHash();
  78. if ($this->encryptor->validateHash($password, $hash)) {
  79. $this->customerLinkManagement->updateLink($credentials->getCustomerId(), $credentials->getAmazonId());
  80. $this->session->loginById($credentials->getCustomerId());
  81. } else {
  82. $this->messageManager->addErrorMessage('The password supplied was incorrect');
  83. return $this->_redirect($this->_url->getRouteUrl('*/*/validate'));
  84. }
  85. }
  86. return $this->accountRedirect->getRedirect();
  87. }
  88. }