PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Backend/Model/Auth.php

https://github.com/FiveDigital/magento2
PHP | 196 lines | 84 code | 19 blank | 93 comment | 9 complexity | 69dc4831d8812ef77a81894640c2eca7 MD5 | raw file
Possible License(s): CC-BY-SA-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_Backend
  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. * Backend Auth model
  28. *
  29. * @category Mage
  30. * @package Mage_Backend
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Backend_Model_Auth
  34. {
  35. /**
  36. * @var Mage_Backend_Model_Auth_StorageInterface
  37. */
  38. protected $_authStorage = null;
  39. /**
  40. * @var Mage_Backend_Model_Auth_Credential_StorageInterface
  41. */
  42. protected $_credentialStorage = null;
  43. /**
  44. * Set auth storage if it is instance of Mage_Backend_Model_Auth_StorageInterface
  45. *
  46. * @param Mage_Backend_Model_Auth_StorageInterface $storage
  47. * @return Mage_Backend_Model_Auth
  48. * @throw Mage_Backend_Model_Auth_Exception if $storage is not correct
  49. */
  50. public function setAuthStorage($storage)
  51. {
  52. if (!($storage instanceof Mage_Backend_Model_Auth_StorageInterface)) {
  53. self::throwException('Authentication storage is incorrect.');
  54. }
  55. $this->_authStorage = $storage;
  56. return $this;
  57. }
  58. /**
  59. * Return auth storage.
  60. * If auth storage was not defined outside - returns default object of auth storage
  61. *
  62. * @return Mage_Backend_Model_Auth_StorageInterface
  63. */
  64. public function getAuthStorage()
  65. {
  66. if (is_null($this->_authStorage)) {
  67. $this->_authStorage = Mage::getSingleton('Mage_Backend_Model_Auth_Session');
  68. }
  69. return $this->_authStorage;
  70. }
  71. /**
  72. * Return current (successfully authenticated) user,
  73. * an instance of Mage_Backend_Model_Auth_Credential_StorageInterface
  74. *
  75. * @return Mage_Backend_Model_Auth_Credential_StorageInterface
  76. */
  77. public function getUser()
  78. {
  79. return $this->getAuthStorage()->getUser();
  80. }
  81. /**
  82. * Initialize credential storage from configuration
  83. *
  84. * @return void
  85. * @throw Mage_Backend_Model_Auth_Exception if credential storage absent or has not correct configuration
  86. */
  87. protected function _initCredentialStorage()
  88. {
  89. $areaConfig = Mage::getConfig()->getAreaConfig(Mage::helper('Mage_Backend_Helper_Data')->getAreaCode());
  90. $storage = Mage::getModel($areaConfig['auth']['credential_storage']);
  91. if ($storage instanceof Mage_Backend_Model_Auth_Credential_StorageInterface) {
  92. $this->_credentialStorage = $storage;
  93. return;
  94. }
  95. self::throwException(
  96. Mage::helper('Mage_Backend_Helper_Data')->__('There are no authentication credential storage.')
  97. );
  98. }
  99. /**
  100. * Return credential storage object
  101. *
  102. * @return null | Mage_Backend_Model_Auth_Credential_StorageInterface
  103. */
  104. public function getCredentialStorage()
  105. {
  106. if (is_null($this->_credentialStorage)) {
  107. $this->_initCredentialStorage();
  108. }
  109. return $this->_credentialStorage;
  110. }
  111. /**
  112. * Perform login process
  113. *
  114. * @param string $username
  115. * @param string $password
  116. * @return void
  117. * @throws Mage_Backend_Model_Auth_Exception if login process was unsuccessful
  118. */
  119. public function login($username, $password)
  120. {
  121. if (empty($username) || empty($password)) {
  122. self::throwException(Mage::helper('Mage_Backend_Helper_Data')->__('Invalid User Name or Password.'));
  123. }
  124. try {
  125. $this->_initCredentialStorage();
  126. $this->getCredentialStorage()->login($username, $password);
  127. if ($this->getCredentialStorage()->getId()) {
  128. $this->getAuthStorage()->setUser($this->getCredentialStorage());
  129. $this->getAuthStorage()->processLogin();
  130. Mage::dispatchEvent('backend_auth_user_login_success', array('user' => $this->getCredentialStorage()));
  131. }
  132. if (!$this->getAuthStorage()->getUser()) {
  133. self::throwException(Mage::helper('Mage_Backend_Helper_Data')->__('Invalid User Name or Password.'));
  134. }
  135. } catch (Mage_Backend_Model_Auth_Plugin_Exception $e) {
  136. Mage::dispatchEvent('backend_auth_user_login_failed', array('user_name' => $username, 'exception' => $e));
  137. throw $e;
  138. } catch (Mage_Core_Exception $e) {
  139. Mage::dispatchEvent('backend_auth_user_login_failed', array('user_name' => $username, 'exception' => $e));
  140. self::throwException(Mage::helper('Mage_Backend_Helper_Data')->__('Invalid User Name or Password.'));
  141. }
  142. }
  143. /**
  144. * Perform logout process
  145. *
  146. * @return void
  147. */
  148. public function logout()
  149. {
  150. $this->getAuthStorage()->processLogout();
  151. Mage::dispatchEvent('admin_session_user_logout');
  152. }
  153. /**
  154. * Check if current user is logged in
  155. *
  156. * @return boolean
  157. */
  158. public function isLoggedIn()
  159. {
  160. return $this->getAuthStorage()->isLoggedIn();
  161. }
  162. /**
  163. * Throws specific Backend Authentication Exception
  164. *
  165. * @static
  166. * @param string $msg
  167. * @param string $code
  168. * @throws Mage_Backend_Model_Auth_Exception
  169. */
  170. public static function throwException($msg = null, $code = null)
  171. {
  172. if (is_null($msg)) {
  173. $msg = Mage::helper('Mage_Backend_Helper_Data')->__('Authentication error occurred.');
  174. }
  175. throw new Mage_Backend_Model_Auth_Exception($msg, $code);
  176. }
  177. }