PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/magento/app/code/core/Mage/Admin/Model/Session.php

https://bitbucket.org/jit_bec/shopifine
PHP | 231 lines | 113 code | 18 blank | 100 comment | 21 complexity | 36e2e10aaa818d4f2717ea269eee30be MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Admin
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Auth session model
  28. *
  29. * @category Mage
  30. * @package Mage_Admin
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Admin_Model_Session extends Mage_Core_Model_Session_Abstract
  34. {
  35. /**
  36. * Whether it is the first page after successfull login
  37. *
  38. * @var boolean
  39. */
  40. protected $_isFirstPageAfterLogin;
  41. /**
  42. * Class constructor
  43. *
  44. */
  45. public function __construct()
  46. {
  47. $this->init('admin');
  48. }
  49. /**
  50. * Pull out information from session whether there is currently the first page after log in
  51. *
  52. * The idea is to set this value on login(), then redirect happens,
  53. * after that on next request the value is grabbed once the session is initialized
  54. * Since the session is used as a singleton, the value will be in $_isFirstPageAfterLogin until the end of request,
  55. * unless it is reset intentionally from somewhere
  56. *
  57. * @param string $namespace
  58. * @param string $sessionName
  59. * @return Mage_Admin_Model_Session
  60. * @see self::login()
  61. */
  62. public function init($namespace, $sessionName = null)
  63. {
  64. parent::init($namespace, $sessionName);
  65. $this->isFirstPageAfterLogin();
  66. return $this;
  67. }
  68. /**
  69. * Try to login user in admin
  70. *
  71. * @param string $username
  72. * @param string $password
  73. * @param Mage_Core_Controller_Request_Http $request
  74. * @return Mage_Admin_Model_User|null
  75. */
  76. public function login($username, $password, $request = null)
  77. {
  78. if (empty($username) || empty($password)) {
  79. return;
  80. }
  81. try {
  82. /** @var $user Mage_Admin_Model_User */
  83. $user = Mage::getModel('admin/user');
  84. $user->login($username, $password);
  85. if ($user->getId()) {
  86. $this->renewSession();
  87. if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
  88. Mage::getSingleton('adminhtml/url')->renewSecretUrls();
  89. }
  90. $this->setIsFirstPageAfterLogin(true);
  91. $this->setUser($user);
  92. $this->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
  93. $requestUri = $this->_getRequestUri($request);
  94. if ($requestUri) {
  95. Mage::dispatchEvent('admin_session_user_login_success', array('user' => $user));
  96. header('Location: ' . $requestUri);
  97. exit;
  98. }
  99. } else {
  100. Mage::throwException(Mage::helper('adminhtml')->__('Invalid User Name or Password.'));
  101. }
  102. } catch (Mage_Core_Exception $e) {
  103. Mage::dispatchEvent('admin_session_user_login_failed',
  104. array('user_name' => $username, 'exception' => $e));
  105. if ($request && !$request->getParam('messageSent')) {
  106. Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
  107. $request->setParam('messageSent', true);
  108. }
  109. }
  110. return $user;
  111. }
  112. /**
  113. * Refresh ACL resources stored in session
  114. *
  115. * @param Mage_Admin_Model_User $user
  116. * @return Mage_Admin_Model_Session
  117. */
  118. public function refreshAcl($user = null)
  119. {
  120. if (is_null($user)) {
  121. $user = $this->getUser();
  122. }
  123. if (!$user) {
  124. return $this;
  125. }
  126. if (!$this->getAcl() || $user->getReloadAclFlag()) {
  127. $this->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
  128. }
  129. if ($user->getReloadAclFlag()) {
  130. $user->unsetData('password');
  131. $user->setReloadAclFlag('0')->save();
  132. }
  133. return $this;
  134. }
  135. /**
  136. * Check current user permission on resource and privilege
  137. *
  138. * Mage::getSingleton('admin/session')->isAllowed('admin/catalog')
  139. * Mage::getSingleton('admin/session')->isAllowed('catalog')
  140. *
  141. * @param string $resource
  142. * @param string $privilege
  143. * @return boolean
  144. */
  145. public function isAllowed($resource, $privilege = null)
  146. {
  147. $user = $this->getUser();
  148. $acl = $this->getAcl();
  149. if ($user && $acl) {
  150. if (!preg_match('/^admin/', $resource)) {
  151. $resource = 'admin/' . $resource;
  152. }
  153. try {
  154. return $acl->isAllowed($user->getAclRole(), $resource, $privilege);
  155. } catch (Exception $e) {
  156. try {
  157. if (!$acl->has($resource)) {
  158. return $acl->isAllowed($user->getAclRole(), null, $privilege);
  159. }
  160. } catch (Exception $e) { }
  161. }
  162. }
  163. return false;
  164. }
  165. /**
  166. * Check if user is logged in
  167. *
  168. * @return boolean
  169. */
  170. public function isLoggedIn()
  171. {
  172. return $this->getUser() && $this->getUser()->getId();
  173. }
  174. /**
  175. * Check if it is the first page after successfull login
  176. *
  177. * @return boolean
  178. */
  179. public function isFirstPageAfterLogin()
  180. {
  181. if (is_null($this->_isFirstPageAfterLogin)) {
  182. $this->_isFirstPageAfterLogin = $this->getData('is_first_visit', true);
  183. }
  184. return $this->_isFirstPageAfterLogin;
  185. }
  186. /**
  187. * Setter whether the current/next page should be treated as first page after login
  188. *
  189. * @param bool $value
  190. * @return Mage_Admin_Model_Session
  191. */
  192. public function setIsFirstPageAfterLogin($value)
  193. {
  194. $this->_isFirstPageAfterLogin = (bool)$value;
  195. return $this->setIsFirstVisit($this->_isFirstPageAfterLogin);
  196. }
  197. /**
  198. * Custom REQUEST_URI logic
  199. *
  200. * @param Mage_Core_Controller_Request_Http $request
  201. * @return string|null
  202. */
  203. protected function _getRequestUri($request = null)
  204. {
  205. if (Mage::getSingleton('adminhtml/url')->useSecretKey()) {
  206. return Mage::getSingleton('adminhtml/url')->getUrl('*/*/*', array('_current' => true));
  207. } elseif ($request) {
  208. return $request->getRequestUri();
  209. } else {
  210. return null;
  211. }
  212. }
  213. }