Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Session.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Customer\Model;
7 
13 
23 {
29  protected $_customer;
30 
34  protected $_customerResource;
35 
41  protected $_customerModel;
42 
48  protected $_isCustomerIdChecked = null;
49 
55  protected $_customerUrl;
56 
62  protected $_coreUrl = null;
63 
67  protected $_configShare;
68 
72  protected $_session;
73 
78 
82  protected $_customerFactory;
83 
87  protected $_urlFactory;
88 
92  protected $_eventManager;
93 
97  protected $_httpContext;
98 
102  protected $groupManagement;
103 
107  protected $response;
108 
134  public function __construct(
135  \Magento\Framework\App\Request\Http $request,
136  \Magento\Framework\Session\SidResolverInterface $sidResolver,
139  \Magento\Framework\Session\ValidatorInterface $validator,
140  \Magento\Framework\Session\StorageInterface $storage,
141  \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
142  \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
143  \Magento\Framework\App\State $appState,
144  Config\Share $configShare,
145  \Magento\Framework\Url\Helper\Data $coreUrl,
147  ResourceCustomer $customerResource,
148  CustomerFactory $customerFactory,
149  \Magento\Framework\UrlFactory $urlFactory,
150  \Magento\Framework\Session\Generic $session,
151  \Magento\Framework\Event\ManagerInterface $eventManager,
152  \Magento\Framework\App\Http\Context $httpContext,
155  \Magento\Framework\App\Response\Http $response
156  ) {
157  $this->_coreUrl = $coreUrl;
158  $this->_customerUrl = $customerUrl;
159  $this->_configShare = $configShare;
160  $this->_customerResource = $customerResource;
161  $this->_customerFactory = $customerFactory;
162  $this->_urlFactory = $urlFactory;
163  $this->_session = $session;
164  $this->customerRepository = $customerRepository;
165  $this->_eventManager = $eventManager;
166  $this->_httpContext = $httpContext;
167  parent::__construct(
168  $request,
169  $sidResolver,
171  $saveHandler,
172  $validator,
173  $storage,
176  $appState
177  );
178  $this->groupManagement = $groupManagement;
179  $this->response = $response;
180  $this->_eventManager->dispatch('customer_session_init', ['customer_session' => $this]);
181  }
182 
188  public function getCustomerConfigShare()
189  {
190  return $this->_configShare;
191  }
192 
199  public function setCustomerData(CustomerData $customer)
200  {
201  $this->_customer = $customer;
202  if ($customer === null) {
203  $this->setCustomerId(null);
204  } else {
205  $this->_httpContext->setValue(
207  $customer->getGroupId(),
209  );
210  $this->setCustomerId($customer->getId());
211  }
212  return $this;
213  }
214 
220  public function getCustomerData()
221  {
222  if (!$this->_customer instanceof CustomerData && $this->getCustomerId()) {
223  $this->_customer = $this->customerRepository->getById($this->getCustomerId());
224  }
225 
226  return $this->_customer;
227  }
228 
234  public function getCustomerDataObject()
235  {
236  /* TODO refactor this after all usages of the setCustomer is refactored */
237  return $this->getCustomer()->getDataModel();
238  }
239 
246  public function setCustomerDataObject(CustomerData $customerData)
247  {
248  $this->setId($customerData->getId());
249  $this->getCustomer()->updateData($customerData);
250  return $this;
251  }
252 
260  public function setCustomer(Customer $customerModel)
261  {
262  $this->_customerModel = $customerModel;
263  $this->_httpContext->setValue(
265  $customerModel->getGroupId(),
267  );
268  $this->setCustomerId($customerModel->getId());
269  if (!$customerModel->isConfirmationRequired() && $customerModel->getConfirmation()) {
270  $customerModel->setConfirmation(null)->save();
271  }
272 
277  $this->unsIsCustomerEmulated();
278 
279  return $this;
280  }
281 
288  public function getCustomer()
289  {
290  if ($this->_customerModel === null) {
291  $this->_customerModel = $this->_customerFactory->create()->load($this->getCustomerId());
292  }
293 
294  return $this->_customerModel;
295  }
296 
303  public function setCustomerId($id)
304  {
305  $this->storage->setData('customer_id', $id);
306  return $this;
307  }
308 
315  public function getCustomerId()
316  {
317  if ($this->storage->getData('customer_id')) {
318  return $this->storage->getData('customer_id');
319  }
320  return null;
321  }
322 
328  public function getId()
329  {
330  return $this->getCustomerId();
331  }
332 
339  public function setId($customerId)
340  {
341  return $this->setCustomerId($customerId);
342  }
343 
350  public function setCustomerGroupId($id)
351  {
352  $this->storage->setData('customer_group_id', $id);
353  return $this;
354  }
355 
362  public function getCustomerGroupId()
363  {
364  if ($this->storage->getData('customer_group_id')) {
365  return $this->storage->getData('customer_group_id');
366  }
367  if ($this->getCustomerData()) {
368  $customerGroupId = $this->getCustomerData()->getGroupId();
369  $this->setCustomerGroupId($customerGroupId);
370  return $customerGroupId;
371  }
373  }
374 
381  public function isLoggedIn()
382  {
383  return (bool)$this->getCustomerId()
384  && $this->checkCustomerId($this->getId())
385  && !$this->getIsCustomerEmulated();
386  }
387 
394  public function checkCustomerId($customerId)
395  {
396  if ($this->_isCustomerIdChecked === $customerId) {
397  return true;
398  }
399 
400  try {
401  $this->customerRepository->getById($customerId);
402  $this->_isCustomerIdChecked = $customerId;
403  return true;
404  } catch (\Exception $e) {
405  return false;
406  }
407  }
408 
414  {
415  $this->setCustomer($customer);
416  $this->_eventManager->dispatch('customer_login', ['customer' => $customer]);
417  $this->_eventManager->dispatch('customer_data_object_login', ['customer' => $this->getCustomerDataObject()]);
418  $this->regenerateId();
419  return $this;
420  }
421 
427  {
428  $this->_httpContext->setValue(Context::CONTEXT_AUTH, true, false);
429  $this->setCustomerData($customer);
430 
431  $customerModel = $this->_customerFactory->create()->updateData($customer);
432 
433  $this->setCustomer($customerModel);
434 
435  $this->_eventManager->dispatch('customer_login', ['customer' => $customerModel]);
436  $this->_eventManager->dispatch('customer_data_object_login', ['customer' => $customer]);
437  return $this;
438  }
439 
447  public function loginById($customerId)
448  {
449  try {
450  $customer = $this->customerRepository->getById($customerId);
452  return true;
453  } catch (\Exception $e) {
454  return false;
455  }
456  }
457 
464  public function logout()
465  {
466  if ($this->isLoggedIn()) {
467  $this->_eventManager->dispatch('customer_logout', ['customer' => $this->getCustomer()]);
468  $this->_logout();
469  }
470  $this->_httpContext->unsValue(Context::CONTEXT_AUTH);
471  return $this;
472  }
473 
480  public function authenticate($loginUrl = null)
481  {
482  if ($this->isLoggedIn()) {
483  return true;
484  }
485  $this->setBeforeAuthUrl($this->_createUrl()->getUrl('*/*/*', ['_current' => true]));
486  if (isset($loginUrl)) {
487  $this->response->setRedirect($loginUrl);
488  } else {
489  $arguments = $this->_customerUrl->getLoginUrlParams();
490  if ($this->_createUrl()->getUseSession()) {
491  $arguments += [
492  '_query' => [
493  $this->sidResolver->getSessionIdQueryParam($this->_session) => $this->_session->getSessionId(),
494  ]
495  ];
496  }
497  $this->response->setRedirect(
499  );
500  }
501 
502  return false;
503  }
504 
512  protected function _setAuthUrl($key, $url)
513  {
514  $url = $this->_coreUrl->removeRequestParam($url, $this->sidResolver->getSessionIdQueryParam($this));
515  // Add correct session ID to URL if needed
516  $url = $this->_createUrl()->getRebuiltUrl($url);
517  return $this->storage->setData($key, $url);
518  }
519 
525  protected function _logout()
526  {
527  $this->_customer = null;
528  $this->_customerModel = null;
529  $this->setCustomerId(null);
530  $this->setCustomerGroupId($this->groupManagement->getNotLoggedInGroup()->getId());
531  $this->destroy(['clear_storage' => false]);
532  return $this;
533  }
534 
541  public function setBeforeAuthUrl($url)
542  {
543  return $this->_setAuthUrl('before_auth_url', $url);
544  }
545 
552  public function setAfterAuthUrl($url)
553  {
554  return $this->_setAuthUrl('after_auth_url', $url);
555  }
556 
562  public function regenerateId()
563  {
564  parent::regenerateId();
565  $this->_cleanHosts();
566  return $this;
567  }
568 
572  protected function _createUrl()
573  {
574  return $this->_urlFactory->create();
575  }
576 }
$customerData
$customer
Definition: customers.php:11
setCustomerData(CustomerData $customer)
Definition: Session.php:199
$id
Definition: fieldset.phtml:14
$customerUrl
Definition: info.phtml:28
setCustomer(Customer $customerModel)
Definition: Session.php:260
$arguments
setCustomerDataAsLoggedIn($customer)
Definition: Session.php:426
setCustomerDataObject(CustomerData $customerData)
Definition: Session.php:246
authenticate($loginUrl=null)
Definition: Session.php:480
__construct(\Magento\Framework\App\Request\Http $request, \Magento\Framework\Session\SidResolverInterface $sidResolver, \Magento\Framework\Session\Config\ConfigInterface $sessionConfig, \Magento\Framework\Session\SaveHandlerInterface $saveHandler, \Magento\Framework\Session\ValidatorInterface $validator, \Magento\Framework\Session\StorageInterface $storage, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory, \Magento\Framework\App\State $appState, Config\Share $configShare, \Magento\Framework\Url\Helper\Data $coreUrl, \Magento\Customer\Model\Url $customerUrl, ResourceCustomer $customerResource, CustomerFactory $customerFactory, \Magento\Framework\UrlFactory $urlFactory, \Magento\Framework\Session\Generic $session, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Framework\App\Http\Context $httpContext, CustomerRepositoryInterface $customerRepository, GroupManagementInterface $groupManagement, \Magento\Framework\App\Response\Http $response)
Definition: Session.php:134