Session.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\Helper;
  17. use Amazon\Core\Api\Data\AmazonCustomerInterface;
  18. use Amazon\Login\Domain\ValidationCredentials;
  19. use Magento\Customer\Api\Data\CustomerInterface;
  20. use Magento\Customer\Model\Session as CustomerSession;
  21. use Magento\Checkout\Model\Session as CheckoutSession;
  22. use Magento\Framework\Event\ManagerInterface as EventManagerInterface;
  23. class Session
  24. {
  25. /**
  26. * @var CustomerSession
  27. */
  28. private $session;
  29. /**
  30. * @var CheckoutSession
  31. */
  32. private $checkoutSession;
  33. /**
  34. * @var EventManagerInterface
  35. */
  36. private $eventManager;
  37. /**
  38. * Session constructor.
  39. * @param CustomerSession $session
  40. * @param EventManagerInterface $eventManager
  41. * @param CheckoutSession $checkoutSession
  42. */
  43. public function __construct(
  44. CustomerSession $session,
  45. EventManagerInterface $eventManager,
  46. CheckoutSession $checkoutSession
  47. ) {
  48. $this->session = $session;
  49. $this->checkoutSession = $checkoutSession;
  50. $this->eventManager = $eventManager;
  51. }
  52. /**
  53. * Login customer by data
  54. *
  55. * @param CustomerInterface $customerData
  56. */
  57. public function login(CustomerInterface $customerData)
  58. {
  59. $this->dispatchAuthenticationEvent();
  60. if ($customerData->getId() != $this->session->getId() || !$this->session->isLoggedIn()) {
  61. $this->session->setCustomerDataAsLoggedIn($customerData);
  62. $this->session->regenerateId();
  63. $this->checkoutSession->loadCustomerQuote();
  64. }
  65. }
  66. /**
  67. * Login customer by id
  68. *
  69. * @param integer $customerId
  70. */
  71. public function loginById($customerId)
  72. {
  73. $this->dispatchAuthenticationEvent();
  74. $this->session->loginById($customerId);
  75. $this->session->regenerateId();
  76. }
  77. /**
  78. * For compatibility with customer_customer_authenticated event dispatched from standard login controller.
  79. * The observers are also attached to this with the exception of password related ones.
  80. */
  81. protected function dispatchAuthenticationEvent()
  82. {
  83. $this->eventManager->dispatch('amazon_customer_authenticated');
  84. }
  85. /**
  86. * Set validation credentials in session
  87. *
  88. * @param ValidationCredentials $credentials
  89. */
  90. public function setValidationCredentials(ValidationCredentials $credentials)
  91. {
  92. $this->session->setAmazonValidationCredentials($credentials);
  93. }
  94. /**
  95. * Get validation credentials from session
  96. *
  97. * @return ValidationCredentials|null
  98. */
  99. public function getValidationCredentials()
  100. {
  101. $credentials = $this->session->getAmazonValidationCredentials();
  102. return ($credentials) ?: null;
  103. }
  104. /**
  105. * Check if Magento account is logged in
  106. *
  107. * @return bool
  108. */
  109. public function isLoggedIn()
  110. {
  111. return $this->session->isLoggedIn();
  112. }
  113. /**
  114. * Check if user is logged in to Amazon
  115. *
  116. * @return bool
  117. */
  118. public function isAmazonLoggedIn()
  119. {
  120. return $this->session->getIsAmazonLoggedIn();
  121. }
  122. /**
  123. * @return void
  124. */
  125. public function setIsAmazonLoggedIn($isLoggedIn)
  126. {
  127. if ($isLoggedIn) {
  128. $this->session->setIsAmazonLoggedIn(true);
  129. } else {
  130. $this->session->unsIsAmazonLoggedIn();
  131. }
  132. }
  133. /**
  134. * @param AmazonCustomerInterface $amazonCustomer
  135. * @return void
  136. */
  137. public function setAmazonCustomer(AmazonCustomerInterface $amazonCustomer)
  138. {
  139. $this->session->setAmazonCustomer($amazonCustomer);
  140. }
  141. /**
  142. * @return void
  143. */
  144. public function clearAmazonCustomer()
  145. {
  146. $this->session->unsAmazonCustomer();
  147. }
  148. /**
  149. * @return AmazonCustomerInterface|null
  150. */
  151. public function getAmazonCustomer()
  152. {
  153. $amazonCustomer = $this->session->getAmazonCustomer();
  154. if ($amazonCustomer && (!$amazonCustomer instanceof AmazonCustomerInterface)) {
  155. $this->clearAmazonCustomer();
  156. $amazonCustomer = null;
  157. }
  158. return $amazonCustomer;
  159. }
  160. }