Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
Session.php
Go to the documentation of this file.
1 <?php
7 
10 
27 class Session extends \Magento\Framework\Session\SessionManager implements \Magento\Backend\Model\Auth\StorageInterface
28 {
32  const XML_PATH_SESSION_LIFETIME = 'admin/security/session_lifetime';
33 
40 
46  protected $_aclBuilder;
47 
51  protected $_backendUrl;
52 
56  protected $_config;
57 
74  public function __construct(
75  \Magento\Framework\App\Request\Http $request,
76  \Magento\Framework\Session\SidResolverInterface $sidResolver,
77  \Magento\Framework\Session\Config\ConfigInterface $sessionConfig,
78  \Magento\Framework\Session\SaveHandlerInterface $saveHandler,
79  \Magento\Framework\Session\ValidatorInterface $validator,
83  \Magento\Framework\App\State $appState,
84  \Magento\Framework\Acl\Builder $aclBuilder,
85  \Magento\Backend\Model\UrlInterface $backendUrl,
86  \Magento\Backend\App\ConfigInterface $config
87  ) {
88  $this->_config = $config;
89  $this->_aclBuilder = $aclBuilder;
90  $this->_backendUrl = $backendUrl;
91  parent::__construct(
92  $request,
96  $validator,
97  $storage,
100  $appState
101  );
102  }
103 
110  public function refreshAcl($user = null)
111  {
112  if ($user === null) {
113  $user = $this->getUser();
114  }
115  if (!$user) {
116  return $this;
117  }
118  if (!$this->getAcl() || $user->getReloadAclFlag()) {
119  $this->setAcl($this->_aclBuilder->getAcl());
120  }
121  if ($user->getReloadAclFlag()) {
122  $user->unsetData('password');
123  $user->setReloadAclFlag('0')->save();
124  }
125  return $this;
126  }
127 
135  public function isAllowed($resource, $privilege = null)
136  {
137  $user = $this->getUser();
138  $acl = $this->getAcl();
139 
140  if ($user && $acl) {
141  try {
142  return $acl->isAllowed($user->getAclRole(), $resource, $privilege);
143  } catch (\Exception $e) {
144  try {
145  if (!$acl->has($resource)) {
146  return $acl->isAllowed($user->getAclRole(), null, $privilege);
147  }
148  } catch (\Exception $e) {
149  }
150  }
151  }
152  return false;
153  }
154 
160  public function isLoggedIn()
161  {
162  return $this->getUser() && $this->getUser()->getId();
163  }
164 
170  public function prolong()
171  {
172  $lifetime = $this->_config->getValue(self::XML_PATH_SESSION_LIFETIME);
173  $cookieValue = $this->cookieManager->getCookie($this->getName());
174 
175  if ($cookieValue) {
176  $this->setUpdatedAt(time());
177  $cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
178  ->setDuration($lifetime)
179  ->setPath($this->sessionConfig->getCookiePath())
180  ->setDomain($this->sessionConfig->getCookieDomain())
181  ->setSecure($this->sessionConfig->getCookieSecure())
182  ->setHttpOnly($this->sessionConfig->getCookieHttpOnly());
183  $this->cookieManager->setPublicCookie($this->getName(), $cookieValue, $cookieMetadata);
184  }
185  }
186 
192  public function isFirstPageAfterLogin()
193  {
194  if ($this->_isFirstAfterLogin === null) {
195  $this->_isFirstAfterLogin = $this->getData('is_first_visit', true);
196  }
198  }
199 
207  {
208  $this->_isFirstAfterLogin = (bool)$value;
209  return $this->setIsFirstVisit($this->_isFirstAfterLogin);
210  }
211 
217  public function processLogin()
218  {
219  if ($this->getUser()) {
220  $this->regenerateId();
221 
222  if ($this->_backendUrl->useSecretKey()) {
223  $this->_backendUrl->renewSecretUrls();
224  }
225 
226  $this->setIsFirstPageAfterLogin(true);
227  $this->setAcl($this->_aclBuilder->getAcl());
228  $this->setUpdatedAt(time());
229  }
230  return $this;
231  }
232 
238  public function processLogout()
239  {
240  $this->destroy();
241  return $this;
242  }
243 
252  public function isValidForPath($path)
253  {
254  return true;
255  }
256 }
isAllowed($resource, $privilege=null)
Definition: Session.php:135
__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, CookieManagerInterface $cookieManager, CookieMetadataFactory $cookieMetadataFactory, \Magento\Framework\App\State $appState, \Magento\Framework\Acl\Builder $aclBuilder, \Magento\Backend\Model\UrlInterface $backendUrl, \Magento\Backend\App\ConfigInterface $config)
Definition: Session.php:74
$config
Definition: fraud_order.php:17
$resource
Definition: bulk.php:12
$value
Definition: gender.phtml:16
$user
Definition: dummy_user.php:13