PageRenderTime 26ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

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

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