AdminConfig.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Backend Session configuration object
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Backend\Model\Session;
  9. use Magento\Backend\App\Area\FrontNameResolver;
  10. use Magento\Framework\App\DeploymentConfig;
  11. use Magento\Framework\Filesystem;
  12. use Magento\Framework\Session\Config;
  13. /**
  14. * Magento Backend session configuration
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class AdminConfig extends Config
  19. {
  20. /**
  21. * Configuration for admin session name
  22. */
  23. const SESSION_NAME_ADMIN = 'admin';
  24. /**
  25. * @var FrontNameResolver
  26. */
  27. protected $_frontNameResolver;
  28. /**
  29. * @var \Magento\Backend\App\BackendAppList
  30. */
  31. private $backendAppList;
  32. /**
  33. * @var \Magento\Backend\Model\UrlFactory
  34. */
  35. private $backendUrlFactory;
  36. /**
  37. * @param \Magento\Framework\ValidatorFactory $validatorFactory
  38. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  39. * @param \Magento\Framework\Stdlib\StringUtils $stringHelper
  40. * @param \Magento\Framework\App\RequestInterface $request
  41. * @param Filesystem $filesystem
  42. * @param DeploymentConfig $deploymentConfig
  43. * @param string $scopeType
  44. * @param \Magento\Backend\App\BackendAppList $backendAppList
  45. * @param FrontNameResolver $frontNameResolver
  46. * @param \Magento\Backend\Model\UrlFactory $backendUrlFactory
  47. * @param string $lifetimePath
  48. * @param string $sessionName
  49. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  50. */
  51. public function __construct(
  52. \Magento\Framework\ValidatorFactory $validatorFactory,
  53. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  54. \Magento\Framework\Stdlib\StringUtils $stringHelper,
  55. \Magento\Framework\App\RequestInterface $request,
  56. Filesystem $filesystem,
  57. DeploymentConfig $deploymentConfig,
  58. $scopeType,
  59. \Magento\Backend\App\BackendAppList $backendAppList,
  60. FrontNameResolver $frontNameResolver,
  61. \Magento\Backend\Model\UrlFactory $backendUrlFactory,
  62. $lifetimePath = self::XML_PATH_COOKIE_LIFETIME,
  63. $sessionName = self::SESSION_NAME_ADMIN
  64. ) {
  65. parent::__construct(
  66. $validatorFactory,
  67. $scopeConfig,
  68. $stringHelper,
  69. $request,
  70. $filesystem,
  71. $deploymentConfig,
  72. $scopeType,
  73. $lifetimePath
  74. );
  75. $this->_frontNameResolver = $frontNameResolver;
  76. $this->backendAppList = $backendAppList;
  77. $this->backendUrlFactory = $backendUrlFactory;
  78. $adminPath = $this->extractAdminPath();
  79. $this->setCookiePath($adminPath);
  80. $this->setName($sessionName);
  81. $this->setCookieSecure($this->_httpRequest->isSecure());
  82. }
  83. /**
  84. * Determine the admin path
  85. *
  86. * @return string
  87. */
  88. private function extractAdminPath()
  89. {
  90. $backendApp = $this->backendAppList->getCurrentApp();
  91. $cookiePath = null;
  92. $baseUrl = parse_url($this->backendUrlFactory->create()->getBaseUrl(), PHP_URL_PATH);
  93. if (!$backendApp) {
  94. $cookiePath = $baseUrl . $this->_frontNameResolver->getFrontName();
  95. return $cookiePath;
  96. }
  97. //In case of application authenticating through the admin login, the script name should be removed
  98. //from the path, because application has own script.
  99. $baseUrl = \Magento\Framework\App\Request\Http::getUrlNoScript($baseUrl);
  100. $cookiePath = $baseUrl . $backendApp->getCookiePath();
  101. return $cookiePath;
  102. }
  103. /**
  104. * Set session cookie lifetime to session duration
  105. *
  106. * @return $this
  107. * @since 100.1.0
  108. */
  109. protected function configureCookieLifetime()
  110. {
  111. return $this->setCookieLifetime(0);
  112. }
  113. }