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

/app/code/core/Mage/Adminhtml/local_11controllers/IndexController.php

https://bitbucket.org/acidel/buykoala
PHP | 399 lines | 260 code | 36 blank | 103 comment | 31 complexity | a350edf59b11aa7aba8f2892a1d14a2b MD5 | raw file
  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_Adminhtml
  23. * @copyright Copyright (c) 2011 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. * Index admin controller
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action
  34. {
  35. /**
  36. * Render specified template
  37. *
  38. * @param string $tplName
  39. * @param array $data parameters required by template
  40. */
  41. protected function _outTemplate($tplName, $data = array())
  42. {
  43. $this->_initLayoutMessages('adminhtml/session');
  44. $block = $this->getLayout()->createBlock('adminhtml/template')->setTemplate("$tplName.phtml");
  45. foreach ($data as $index => $value) {
  46. $block->assign($index, $value);
  47. }
  48. $html = $block->toHtml();
  49. Mage::getSingleton('core/translate_inline')->processResponseBody($html);
  50. $this->getResponse()->setBody($html);
  51. }
  52. /**
  53. * Admin area entry point
  54. * Always redirects to the startup page url
  55. */
  56. public function indexAction()
  57. {
  58. $session = Mage::getSingleton('admin/session');
  59. $url = $session->getUser()->getStartupPageUrl();
  60. if ($session->isFirstPageAfterLogin()) {
  61. // retain the "first page after login" value in session (before redirect)
  62. $session->setIsFirstPageAfterLogin(true);
  63. }
  64. $this->_redirect($url);
  65. }
  66. /**
  67. * Administrator login action
  68. */
  69. public function loginAction()
  70. {
  71. if (Mage::getSingleton('admin/session')->isLoggedIn()) {
  72. $this->_redirect('*');
  73. return;
  74. }
  75. $loginData = $this->getRequest()->getParam('login');
  76. $data = array();
  77. if(is_array($loginData) && array_key_exists('username', $loginData)) {
  78. $data['username'] = $loginData['username'];
  79. } else {
  80. $data['username'] = null;
  81. }
  82. $this->_outTemplate('login', $data);
  83. }
  84. /**
  85. * Administrator logout action
  86. */
  87. public function logoutAction()
  88. {
  89. /** @var $adminSession Mage_Admin_Model_Session */
  90. $adminSession = Mage::getSingleton('admin/session');
  91. $adminSession->unsetAll();
  92. $adminSession->getCookie()->delete($adminSession->getSessionName());
  93. $adminSession->addSuccess(Mage::helper('adminhtml')->__('You have logged out.'));
  94. $this->_redirect('*');
  95. }
  96. /**
  97. * Global Search Action
  98. */
  99. public function globalSearchAction()
  100. {
  101. $searchModules = Mage::getConfig()->getNode("adminhtml/global_search");
  102. $items = array();
  103. if (!Mage::getSingleton('admin/session')->isAllowed('admin/global_search')) {
  104. $items[] = array(
  105. 'id' => 'error',
  106. 'type' => Mage::helper('adminhtml')->__('Error'),
  107. 'name' => Mage::helper('adminhtml')->__('Access Denied'),
  108. 'description' => Mage::helper('adminhtml')->__('You have not enough permissions to use this functionality.')
  109. );
  110. $totalCount = 1;
  111. } else {
  112. if (empty($searchModules)) {
  113. $items[] = array(
  114. 'id' => 'error',
  115. 'type' => Mage::helper('adminhtml')->__('Error'),
  116. 'name' => Mage::helper('adminhtml')->__('No search modules were registered'),
  117. 'description' => Mage::helper('adminhtml')->__('Please make sure that all global admin search modules are installed and activated.')
  118. );
  119. $totalCount = 1;
  120. } else {
  121. $start = $this->getRequest()->getParam('start', 1);
  122. $limit = $this->getRequest()->getParam('limit', 10);
  123. $query = $this->getRequest()->getParam('query', '');
  124. foreach ($searchModules->children() as $searchConfig) {
  125. if ($searchConfig->acl && !Mage::getSingleton('admin/session')->isAllowed($searchConfig->acl)){
  126. continue;
  127. }
  128. $className = $searchConfig->getClassName();
  129. if (empty($className)) {
  130. continue;
  131. }
  132. $searchInstance = new $className();
  133. $results = $searchInstance->setStart($start)
  134. ->setLimit($limit)
  135. ->setQuery($query)
  136. ->load()
  137. ->getResults();
  138. $items = array_merge_recursive($items, $results);
  139. }
  140. $totalCount = sizeof($items);
  141. }
  142. }
  143. $block = $this->getLayout()->createBlock('adminhtml/template')
  144. ->setTemplate('system/autocomplete.phtml')
  145. ->assign('items', $items);
  146. $this->getResponse()->setBody($block->toHtml());
  147. }
  148. /**
  149. * Example action
  150. */
  151. public function exampleAction()
  152. {
  153. $this->_outTemplate('example');
  154. }
  155. /**
  156. * Test action
  157. */
  158. public function testAction()
  159. {
  160. echo $this->getLayout()->createBlock('core/profiler')->toHtml();
  161. }
  162. /**
  163. * Change locale action
  164. */
  165. public function changeLocaleAction()
  166. {
  167. $locale = $this->getRequest()->getParam('locale');
  168. if ($locale) {
  169. Mage::getSingleton('adminhtml/session')->setLocale($locale);
  170. }
  171. $this->_redirectReferer();
  172. }
  173. /**
  174. * Denied JSON action
  175. */
  176. public function deniedJsonAction()
  177. {
  178. $this->getResponse()->setBody($this->_getDeniedJson());
  179. }
  180. /**
  181. * Retrieve response for deniedJsonAction()
  182. */
  183. protected function _getDeniedJson()
  184. {
  185. return Mage::helper('core')->jsonEncode(array(
  186. 'ajaxExpired' => 1,
  187. 'ajaxRedirect' => $this->getUrl('*/index/login')
  188. ));
  189. }
  190. /**
  191. * Denied IFrame action
  192. */
  193. public function deniedIframeAction()
  194. {
  195. $this->getResponse()->setBody($this->_getDeniedIframe());
  196. }
  197. /**
  198. * Retrieve response for deniedIframeAction()
  199. */
  200. protected function _getDeniedIframe()
  201. {
  202. return '<script type="text/javascript">parent.window.location = \''
  203. . $this->getUrl('*/index/login') . '\';</script>';
  204. }
  205. /**
  206. * Forgot administrator password action
  207. */
  208. public function forgotpasswordAction()
  209. {
  210. $email = (string) $this->getRequest()->getParam('email');
  211. $params = $this->getRequest()->getParams();
  212. if (!empty($email) && !empty($params)) {
  213. // Validate received data to be an email address
  214. if (!Zend_Validate::is($email, 'EmailAddress')) {
  215. $this->_getSession()->addError($this->__('Invalid email address.'));
  216. $this->_outTemplate('forgotpassword');
  217. return;
  218. }
  219. $collection = Mage::getResourceModel('admin/user_collection');
  220. /** @var $collection Mage_Admin_Model_Mysql4_User_Collection */
  221. $collection->addFieldToFilter('email', $email);
  222. $collection->load(false);
  223. if ($collection->getSize() > 0) {
  224. foreach ($collection as $item) {
  225. $user = Mage::getModel('admin/user')->load($item->getId());
  226. if ($user->getId()) {
  227. $newResetPasswordLinkToken = Mage::helper('admin')->generateResetPasswordLinkToken();
  228. $user->changeResetPasswordLinkToken($newResetPasswordLinkToken);
  229. $user->save();
  230. $user->sendPasswordResetConfirmationEmail();
  231. }
  232. break;
  233. }
  234. }
  235. $this->_getSession()
  236. ->addSuccess(Mage::helper('adminhtml')->__('If there is an account associated with %s you will receive an email with a link to reset your password.', Mage::helper('adminhtml')->htmlEscape($email)));
  237. $this->_redirect('*/*/login');
  238. return;
  239. } elseif (!empty($params)) {
  240. $this->_getSession()->addError(Mage::helper('adminhtml')->__('The email address is empty.'));
  241. }
  242. $data = array(
  243. 'email' => $email
  244. );
  245. $this->_outTemplate('forgotpassword', $data);
  246. }
  247. /**
  248. * Display reset forgotten password form
  249. *
  250. * User is redirected on this action when he clicks on the corresponding link in password reset confirmation email
  251. */
  252. public function resetPasswordAction()
  253. {
  254. $resetPasswordLinkToken = (string) $this->getRequest()->getQuery('token');
  255. $userId = (int) $this->getRequest()->getQuery('id');
  256. try {
  257. $this->_validateResetPasswordLinkToken($userId, $resetPasswordLinkToken);
  258. $data = array(
  259. 'userId' => $userId,
  260. 'resetPasswordLinkToken' => $resetPasswordLinkToken
  261. );
  262. $this->_outTemplate('resetforgottenpassword', $data);
  263. } catch (Exception $exception) {
  264. $this->_getSession()->addError(Mage::helper('adminhtml')->__('Your password reset link has expired.'));
  265. $this->_redirect('*/*/forgotpassword', array('_nosecret' => true));
  266. }
  267. }
  268. /**
  269. * Reset forgotten password
  270. *
  271. * Used to handle data recieved from reset forgotten password form
  272. */
  273. public function resetPasswordPostAction()
  274. {
  275. $resetPasswordLinkToken = (string) $this->getRequest()->getQuery('token');
  276. $userId = (int) $this->getRequest()->getQuery('id');
  277. $password = (string) $this->getRequest()->getPost('password');
  278. $passwordConfirmation = (string) $this->getRequest()->getPost('confirmation');
  279. try {
  280. $this->_validateResetPasswordLinkToken($userId, $resetPasswordLinkToken);
  281. } catch (Exception $exception) {
  282. $this->_getSession()->addError(Mage::helper('adminhtml')->__('Your password reset link has expired.'));
  283. $this->_redirect('*/*/');
  284. return;
  285. }
  286. $errorMessages = array();
  287. if (iconv_strlen($password) <= 0) {
  288. array_push($errorMessages, Mage::helper('adminhtml')->__('New password field cannot be empty.'));
  289. }
  290. /** @var $user Mage_Admin_Model_User */
  291. $user = Mage::getModel('admin/user')->load($userId);
  292. $user->setNewPassword($password);
  293. $user->setPasswordConfirmation($passwordConfirmation);
  294. $validationErrorMessages = $user->validate();
  295. if (is_array($validationErrorMessages)) {
  296. $errorMessages = array_merge($errorMessages, $validationErrorMessages);
  297. }
  298. if (!empty($errorMessages)) {
  299. foreach ($errorMessages as $errorMessage) {
  300. $this->_getSession()->addError($errorMessage);
  301. }
  302. $data = array(
  303. 'userId' => $userId,
  304. 'resetPasswordLinkToken' => $resetPasswordLinkToken
  305. );
  306. $this->_outTemplate('resetforgottenpassword', $data);
  307. return;
  308. }
  309. try {
  310. // Empty current reset password token i.e. invalidate it
  311. $user->setRpToken(null);
  312. $user->setRpTokenCreatedAt(null);
  313. $user->setPasswordConfirmation(null);
  314. $user->save();
  315. $this->_getSession()->addSuccess(Mage::helper('adminhtml')->__('Your password has been updated.'));
  316. $this->_redirect('*/*/login');
  317. } catch (Exception $exception) {
  318. $this->_getSession()->addError($exception->getMessage());
  319. $data = array(
  320. 'userId' => $userId,
  321. 'resetPasswordLinkToken' => $resetPasswordLinkToken
  322. );
  323. $this->_outTemplate('resetforgottenpassword', $data);
  324. return;
  325. }
  326. }
  327. /**
  328. * Check if password reset token is valid
  329. *
  330. * @param int $userId
  331. * @param string $resetPasswordLinkToken
  332. * @throws Mage_Core_Exception
  333. */
  334. protected function _validateResetPasswordLinkToken($userId, $resetPasswordLinkToken)
  335. {
  336. if (!is_int($userId)
  337. || !is_string($resetPasswordLinkToken)
  338. || empty($resetPasswordLinkToken)
  339. || empty($userId)
  340. || $userId < 0
  341. ) {
  342. throw Mage::exception('Mage_Core', Mage::helper('adminhtml')->__('Invalid password reset token.'));
  343. }
  344. /** @var $user Mage_Admin_Model_User */
  345. $user = Mage::getModel('admin/user')->load($userId);
  346. if (!$user || !$user->getId()) {
  347. throw Mage::exception('Mage_Core', Mage::helper('adminhtml')->__('Wrong account specified.'));
  348. }
  349. $userToken = $user->getRpToken();
  350. if (strcmp($userToken, $resetPasswordLinkToken) != 0 || $user->isResetPasswordLinkTokenExpired()) {
  351. throw Mage::exception('Mage_Core', Mage::helper('adminhtml')->__('Your password reset link has expired.'));
  352. }
  353. }
  354. /**
  355. * Check if user has permissions to access this controller
  356. *
  357. * @return boolean
  358. */
  359. protected function _isAllowed()
  360. {
  361. return true;
  362. }
  363. }