PageRenderTime 81ms CodeModel.GetById 13ms RepoModel.GetById 2ms app.codeStats 0ms

/downloader/Maged/Model/Session.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 203 lines | 96 code | 21 blank | 86 comment | 19 complexity | d2636d68fc3a67136b3574130b2d2f7a MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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_Connect
  23. * @copyright Copyright (c) 2010 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. * Class session
  28. *
  29. * @category Mage
  30. * @package Mage_Connect
  31. * @copyright Copyright (c) 2009 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  32. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  33. */
  34. class Maged_Model_Session extends Maged_Model
  35. {
  36. /**
  37. * Session
  38. *
  39. * @var Mage_Admin_Model_Session
  40. */
  41. protected $_session;
  42. /**
  43. * Init session
  44. *
  45. * @return Maged_Model_Session
  46. */
  47. public function start()
  48. {
  49. if (class_exists('Mage') && Mage::isInstalled()) {
  50. // initialize Magento Config
  51. Mage::app();
  52. $this->_session = Mage::getSingleton('admin/session');
  53. } else {
  54. session_start();
  55. }
  56. return $this;
  57. }
  58. /**
  59. * Get value by key
  60. *
  61. * @param string $key
  62. * @return mixed
  63. */
  64. public function get($key)
  65. {
  66. return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
  67. }
  68. /**
  69. * Set value for key
  70. *
  71. * @param string $key
  72. * @param mixed $value
  73. */
  74. public function set($key, $value)
  75. {
  76. $_SESSION[$key] = $value;
  77. return $this;
  78. }
  79. /**
  80. * Authentication to downloader
  81. */
  82. public function authenticate()
  83. {
  84. if (!$this->_session) {
  85. return $this;
  86. }
  87. if (!empty($_GET['return'])) {
  88. $this->set('return_url', $_GET['return']);
  89. }
  90. if ($this->getUserId()) {
  91. return $this;
  92. }
  93. if (!$this->controller()->isInstalled()) {
  94. return $this;
  95. }
  96. try {
  97. if ( (isset($_POST['username']) && empty($_POST['username'])) ||
  98. (isset($_POST['password']) && empty($_POST['password'])))
  99. {
  100. $this->addMessage('error', 'Invalid user name or password');
  101. }
  102. if (empty($_POST['username']) || empty($_POST['password'])) {
  103. $this->controller()->setAction('login');
  104. return $this;
  105. }
  106. $user = $this->_session->login($_POST['username'], $_POST['password']);
  107. $this->_session->refreshAcl();
  108. if (!$user->getId() || !$this->_session->isAllowed('all')) {
  109. $this->addMessage('error', 'Invalid user name or password');
  110. $this->controller()->setAction('login');
  111. return $this;
  112. }
  113. } catch (Exception $e) {
  114. $this->addMessage('error', $e->getMessage());
  115. }
  116. $this->controller()
  117. ->redirect($this->controller()->url($this->controller()->getAction()).'&loggedin', true);
  118. }
  119. /**
  120. * Log Out
  121. *
  122. * @return Maged_Model_Session
  123. */
  124. public function logout()
  125. {
  126. if (!$this->_session) {
  127. return $this;
  128. }
  129. $this->_session->unsUser();
  130. return $this;
  131. }
  132. /**
  133. * Retrieve user
  134. *
  135. * @return mixed
  136. */
  137. public function getUserId()
  138. {
  139. return ($session = $this->_session) && ($user = $session->getUser()) ? $user->getId() : false;
  140. }
  141. /**
  142. * Add Message
  143. *
  144. * @param string $type
  145. * @param string $msg
  146. * @return Maged_Model_Session
  147. */
  148. public function addMessage($type, $msg)
  149. {
  150. $msgs = $this->getMessages(false);
  151. $msgs[$type][] = $msg;
  152. $this->set('messages', $msgs);
  153. return $this;
  154. }
  155. /**
  156. * Retrieve messages from cache
  157. *
  158. * @param boolean $clear
  159. * @return mixed
  160. */
  161. public function getMessages($clear = true)
  162. {
  163. $msgs = $this->get('messages');
  164. $msgs = $msgs ? $msgs : array();
  165. if ($clear) {
  166. unset($_SESSION['messages']);
  167. }
  168. return $msgs;
  169. }
  170. /**
  171. * Retrieve url to adminhtml
  172. *
  173. * @return string
  174. */
  175. public function getReturnUrl()
  176. {
  177. if (!$this->_session || !$this->_session->isLoggedIn()) {
  178. return '';
  179. }
  180. return Mage::getSingleton('adminhtml/url')->getUrl('adminhtml');
  181. }
  182. }