Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Session Class Reference
Inheritance diagram for Session:
SessionManager SessionManagerInterface

Public Member Functions

 __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)
 
 getCustomerConfigShare ()
 
 setCustomerData (CustomerData $customer)
 
 getCustomerData ()
 
 getCustomerDataObject ()
 
 setCustomerDataObject (CustomerData $customerData)
 
 setCustomer (Customer $customerModel)
 
 getCustomer ()
 
 setCustomerId ($id)
 
 getCustomerId ()
 
 getId ()
 
 setId ($customerId)
 
 setCustomerGroupId ($id)
 
 getCustomerGroupId ()
 
 isLoggedIn ()
 
 checkCustomerId ($customerId)
 
 setCustomerAsLoggedIn ($customer)
 
 setCustomerDataAsLoggedIn ($customer)
 
 loginById ($customerId)
 
 logout ()
 
 authenticate ($loginUrl=null)
 
 setBeforeAuthUrl ($url)
 
 setAfterAuthUrl ($url)
 
 regenerateId ()
 
- Public Member Functions inherited from SessionManager
 __construct (\Magento\Framework\App\Request\Http $request, SidResolverInterface $sidResolver, ConfigInterface $sessionConfig, SaveHandlerInterface $saveHandler, ValidatorInterface $validator, StorageInterface $storage, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory, \Magento\Framework\App\State $appState)
 
 writeClose ()
 
 __call ($method, $args)
 
 start ()
 
 isSessionExists ()
 
 getData ($key='', $clear=false)
 
 getSessionId ()
 
 getName ()
 
 setName ($name)
 
 destroy (array $options=null)
 
 clearStorage ()
 
 getCookieDomain ()
 
 getCookiePath ()
 
 getCookieLifetime ()
 
 setSessionId ($sessionId)
 
 getSessionIdForHost ($urlHost)
 
 isValidForHost ($host)
 
 isValidForPath ($path)
 
 regenerateId ()
 
 expireSessionCookie ()
 

Protected Member Functions

 _setAuthUrl ($key, $url)
 
 _logout ()
 
 _createUrl ()
 
- Protected Member Functions inherited from SessionManager
 registerSaveHandler ()
 
 _addHost ()
 
 _getHosts ()
 
 _cleanHosts ()
 
 clearSubDomainSessionCookie ()
 

Protected Attributes

 $_customer
 
 $_customerResource
 
 $_customerModel
 
 $_isCustomerIdChecked = null
 
 $_customerUrl
 
 $_coreUrl = null
 
 $_configShare
 
 $_session
 
 $customerRepository
 
 $_customerFactory
 
 $_urlFactory
 
 $_eventManager
 
 $_httpContext
 
 $groupManagement
 
 $response
 
- Protected Attributes inherited from SessionManager
 $defaultDestroyOptions = ['send_expire_cookie' => true, 'clear_storage' => true]
 
 $validator
 
 $request
 
 $sidResolver
 
 $sessionConfig
 
 $saveHandler
 
 $storage
 
 $cookieManager
 
 $cookieMetadataFactory
 

Additional Inherited Members

- Data Fields inherited from SessionManagerInterface
const HOST_KEY = '_session_hosts'
 
- Static Protected Attributes inherited from SessionManager
static $urlHostCache = []
 

Detailed Description

Customer session model

@api @method string getNoReferer() @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 22 of file Session.php.

Constructor & Destructor Documentation

◆ __construct()

Parameters
\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
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
Exceptions

Definition at line 134 of file Session.php.

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  }
$customerUrl
Definition: info.phtml:28

Member Function Documentation

◆ _createUrl()

_createUrl ( )
protected
Returns
\Magento\Framework\UrlInterface

Definition at line 572 of file Session.php.

573  {
574  return $this->_urlFactory->create();
575  }

◆ _logout()

_logout ( )
protected

Logout without dispatching event

Returns
$this

Definition at line 525 of file Session.php.

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  }

◆ _setAuthUrl()

_setAuthUrl (   $key,
  $url 
)
protected

Set auth url

Parameters
string$key
string$url
Returns
$this

Definition at line 512 of file Session.php.

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  }

◆ authenticate()

authenticate (   $loginUrl = null)

Authenticate controller action by login customer

Parameters
bool | null$loginUrl
Returns
bool

Definition at line 480 of file Session.php.

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(
498  $this->_createUrl()->getUrl(\Magento\Customer\Model\Url::ROUTE_ACCOUNT_LOGIN, $arguments)
499  );
500  }
501 
502  return false;
503  }
$arguments

◆ checkCustomerId()

checkCustomerId (   $customerId)

Check exists customer (light check)

Parameters
int$customerId
Returns
bool

Definition at line 394 of file Session.php.

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  }

◆ getCustomer()

getCustomer ( )

Retrieve customer model object

Returns
Customer use getCustomerId() instead

Definition at line 288 of file Session.php.

289  {
290  if ($this->_customerModel === null) {
291  $this->_customerModel = $this->_customerFactory->create()->load($this->getCustomerId());
292  }
293 
294  return $this->_customerModel;
295  }

◆ getCustomerConfigShare()

getCustomerConfigShare ( )

Retrieve customer sharing configuration model

Returns
Share

Definition at line 188 of file Session.php.

189  {
190  return $this->_configShare;
191  }

◆ getCustomerData()

getCustomerData ( )

Retrieve customer model object

Returns
CustomerData

Definition at line 220 of file Session.php.

221  {
222  if (!$this->_customer instanceof CustomerData && $this->getCustomerId()) {
223  $this->_customer = $this->customerRepository->getById($this->getCustomerId());
224  }
225 
226  return $this->_customer;
227  }

◆ getCustomerDataObject()

getCustomerDataObject ( )

Returns Customer data object with the customer information

Returns
CustomerData

Definition at line 234 of file Session.php.

235  {
236  /* TODO refactor this after all usages of the setCustomer is refactored */
237  return $this->getCustomer()->getDataModel();
238  }

◆ getCustomerGroupId()

getCustomerGroupId ( )

Get customer group id If customer is not logged in system, 'not logged in' group id will be returned

Returns
int

Definition at line 362 of file Session.php.

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  }

◆ getCustomerId()

getCustomerId ( )

Retrieve customer id from current session

@api

Returns
int|null

Definition at line 315 of file Session.php.

316  {
317  if ($this->storage->getData('customer_id')) {
318  return $this->storage->getData('customer_id');
319  }
320  return null;
321  }

◆ getId()

getId ( )

Retrieve customer id from current session

Returns
int|null

Definition at line 328 of file Session.php.

329  {
330  return $this->getCustomerId();
331  }

◆ isLoggedIn()

isLoggedIn ( )

Checking customer login status

@api

Returns
bool

Definition at line 381 of file Session.php.

382  {
383  return (bool)$this->getCustomerId()
384  && $this->checkCustomerId($this->getId())
385  && !$this->getIsCustomerEmulated();
386  }

◆ loginById()

loginById (   $customerId)

Authorization customer by identifier

@api

Parameters
int$customerId
Returns
bool

Definition at line 447 of file Session.php.

448  {
449  try {
450  $customer = $this->customerRepository->getById($customerId);
452  return true;
453  } catch (\Exception $e) {
454  return false;
455  }
456  }
$customer
Definition: customers.php:11
setCustomerDataAsLoggedIn($customer)
Definition: Session.php:426

◆ logout()

logout ( )

Logout customer

@api

Returns
$this

Definition at line 464 of file Session.php.

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  }

◆ regenerateId()

regenerateId ( )

Reset core session hosts after resetting session ID

Returns
$this

Implements SessionManagerInterface.

Definition at line 562 of file Session.php.

563  {
564  parent::regenerateId();
565  $this->_cleanHosts();
566  return $this;
567  }

◆ setAfterAuthUrl()

setAfterAuthUrl (   $url)

Set After auth url

Parameters
string$url
Returns
$this

Definition at line 552 of file Session.php.

553  {
554  return $this->_setAuthUrl('after_auth_url', $url);
555  }

◆ setBeforeAuthUrl()

setBeforeAuthUrl (   $url)

Set Before auth url

Parameters
string$url
Returns
$this

Definition at line 541 of file Session.php.

542  {
543  return $this->_setAuthUrl('before_auth_url', $url);
544  }

◆ setCustomer()

setCustomer ( Customer  $customerModel)

Set customer model and the customer id in session

Parameters
Customer$customerModel
Returns
$this use setCustomerId() instead

The next line is a workaround. It is used to distinguish users that are logged in from user data set via methods similar to setCustomerId()

Definition at line 260 of file Session.php.

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  }

◆ setCustomerAsLoggedIn()

setCustomerAsLoggedIn (   $customer)
Parameters
Customer$customer
Returns
$this

Definition at line 413 of file Session.php.

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  }
$customer
Definition: customers.php:11
setCustomer(Customer $customerModel)
Definition: Session.php:260

◆ setCustomerData()

setCustomerData ( CustomerData  $customer)

Set customer object and setting customer id in session

Parameters
CustomerData$customer
Returns
$this

Definition at line 199 of file Session.php.

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  }
$customer
Definition: customers.php:11

◆ setCustomerDataAsLoggedIn()

setCustomerDataAsLoggedIn (   $customer)
Parameters
CustomerData$customer
Returns
$this

Definition at line 426 of file Session.php.

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  }
$customer
Definition: customers.php:11
setCustomerData(CustomerData $customer)
Definition: Session.php:199
setCustomer(Customer $customerModel)
Definition: Session.php:260

◆ setCustomerDataObject()

setCustomerDataObject ( CustomerData  $customerData)

Set Customer data object with the customer information

Parameters
CustomerData$customerData
Returns
$this

Definition at line 246 of file Session.php.

247  {
248  $this->setId($customerData->getId());
249  $this->getCustomer()->updateData($customerData);
250  return $this;
251  }
$customerData

◆ setCustomerGroupId()

setCustomerGroupId (   $id)

Set customer group id

Parameters
int | null$id
Returns
$this

Definition at line 350 of file Session.php.

351  {
352  $this->storage->setData('customer_group_id', $id);
353  return $this;
354  }
$id
Definition: fieldset.phtml:14

◆ setCustomerId()

setCustomerId (   $id)

Set customer id

Parameters
int | null$id
Returns
$this

Definition at line 303 of file Session.php.

304  {
305  $this->storage->setData('customer_id', $id);
306  return $this;
307  }
$id
Definition: fieldset.phtml:14

◆ setId()

setId (   $customerId)

Set customer id

Parameters
int | null$customerId
Returns
$this

Definition at line 339 of file Session.php.

340  {
341  return $this->setCustomerId($customerId);
342  }

Field Documentation

◆ $_configShare

$_configShare
protected

Definition at line 67 of file Session.php.

◆ $_coreUrl

$_coreUrl = null
protected

Definition at line 62 of file Session.php.

◆ $_customer

$_customer
protected

Definition at line 29 of file Session.php.

◆ $_customerFactory

$_customerFactory
protected

Definition at line 82 of file Session.php.

◆ $_customerModel

$_customerModel
protected

Definition at line 41 of file Session.php.

◆ $_customerResource

$_customerResource
protected

Definition at line 34 of file Session.php.

◆ $_customerUrl

$_customerUrl
protected

Definition at line 55 of file Session.php.

◆ $_eventManager

$_eventManager
protected

Definition at line 92 of file Session.php.

◆ $_httpContext

$_httpContext
protected

Definition at line 97 of file Session.php.

◆ $_isCustomerIdChecked

$_isCustomerIdChecked = null
protected

Definition at line 48 of file Session.php.

◆ $_session

$_session
protected

Definition at line 72 of file Session.php.

◆ $_urlFactory

$_urlFactory
protected

Definition at line 87 of file Session.php.

◆ $customerRepository

$customerRepository
protected

Definition at line 77 of file Session.php.

◆ $groupManagement

$groupManagement
protected

Definition at line 102 of file Session.php.

◆ $response

$response
protected

Definition at line 107 of file Session.php.


The documentation for this class was generated from the following file: