PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminAccessController.php

https://github.com/netplayer/PrestaShop
PHP | 253 lines | 181 code | 30 blank | 42 comment | 26 complexity | d235a57d152c1cc586e3408e4728138f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  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@prestashop.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 PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class AdminAccessControllerCore extends AdminController
  27. {
  28. /* @var array : Black list of id_tab that do not have access */
  29. public $accesses_black_list = array();
  30. public function __construct()
  31. {
  32. $this->bootstrap = true;
  33. $this->show_toolbar = false;
  34. $this->table = 'access';
  35. $this->className = 'Profile';
  36. $this->multishop_context = Shop::CONTEXT_ALL;
  37. $this->lang = false;
  38. $this->context = Context::getContext();
  39. // Blacklist AdminLogin
  40. $this->accesses_black_list[] = Tab::getIdFromClassName('AdminLogin');
  41. parent::__construct();
  42. }
  43. /**
  44. * AdminController::renderForm() override
  45. * @see AdminController::renderForm()
  46. */
  47. public function renderForm()
  48. {
  49. $current_profile = (int)$this->getCurrentProfileId();
  50. $profiles = Profile::getProfiles($this->context->language->id);
  51. $tabs = Tab::getTabs($this->context->language->id);
  52. $accesses = array();
  53. foreach ($profiles as $profile)
  54. $accesses[$profile['id_profile']] = Profile::getProfileAccesses($profile['id_profile']);
  55. // Deleted id_tab that do not have access
  56. foreach ($tabs as $key => $tab)
  57. {
  58. // Don't allow permissions for unnamed tabs (ie. AdminLogin)
  59. if (empty($tab['name']))
  60. unset($tabs[$key]);
  61. foreach ($this->accesses_black_list as $id_tab)
  62. if ($tab['id_tab'] == (int)$id_tab)
  63. unset($tabs[$key]);
  64. }
  65. $modules = array();
  66. foreach ($profiles as $profile)
  67. {
  68. $modules[$profile['id_profile']] = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  69. SELECT ma.`id_module`, m.`name`, ma.`view`, ma.`configure`
  70. FROM '._DB_PREFIX_.'module_access ma
  71. LEFT JOIN '._DB_PREFIX_.'module m
  72. ON ma.id_module = m.id_module
  73. WHERE id_profile = '.(int)$profile['id_profile'].'
  74. ORDER BY m.name
  75. ');
  76. foreach ($modules[$profile['id_profile']] as $k => &$module)
  77. {
  78. $m = Module::getInstanceById($module['id_module']);
  79. // the following condition handles invalid modules
  80. if ($m)
  81. $module['name'] = $m->displayName;
  82. else
  83. unset($modules[$profile['id_profile']][$k]);
  84. }
  85. uasort($modules[$profile['id_profile']], array($this, 'sortModuleByName'));
  86. }
  87. $this->fields_form = array('');
  88. $this->tpl_form_vars = array(
  89. 'profiles' => $profiles,
  90. 'accesses' => $accesses,
  91. 'tabs' => $tabs,
  92. 'current_profile' => (int)$current_profile,
  93. 'admin_profile' => (int)_PS_ADMIN_PROFILE_,
  94. 'access_edit' => $this->tabAccess['edit'],
  95. 'perms' => array('view', 'add', 'edit', 'delete'),
  96. 'modules' => $modules,
  97. 'link' => $this->context->link
  98. );
  99. return parent::renderForm();
  100. }
  101. /**
  102. * AdminController::initContent() override
  103. * @see AdminController::initContent()
  104. */
  105. public function initContent()
  106. {
  107. $this->display = 'edit';
  108. $this->initTabModuleList();
  109. if (!$this->loadObject(true))
  110. return;
  111. $this->initPageHeaderToolbar();
  112. $this->content .= $this->renderForm();
  113. $this->context->smarty->assign(array(
  114. 'content' => $this->content,
  115. 'url_post' => self::$currentIndex.'&token='.$this->token,
  116. 'show_page_header_toolbar' => $this->show_page_header_toolbar,
  117. 'page_header_toolbar_title' => $this->page_header_toolbar_title,
  118. 'page_header_toolbar_btn' => $this->page_header_toolbar_btn
  119. ));
  120. }
  121. public function initToolbarTitle()
  122. {
  123. $this->toolbar_title = array_unique($this->breadcrumbs);
  124. }
  125. public function initPageHeaderToolbar()
  126. {
  127. parent::initPageHeaderToolbar();
  128. unset($this->page_header_toolbar_btn['cancel']);
  129. }
  130. public function ajaxProcessUpdateAccess()
  131. {
  132. if (_PS_MODE_DEMO_)
  133. throw new PrestaShopException(Tools::displayError('This functionality has been disabled.'));
  134. if ($this->tabAccess['edit'] != '1')
  135. throw new PrestaShopException(Tools::displayError('You do not have permission to edit this.'));
  136. if (Tools::isSubmit('submitAddAccess'))
  137. {
  138. $perm = Tools::getValue('perm');
  139. if (!in_array($perm, array('view', 'add', 'edit', 'delete', 'all')))
  140. throw new PrestaShopException('permission does not exist');
  141. $enabled = (int)Tools::getValue('enabled');
  142. $id_tab = (int)Tools::getValue('id_tab');
  143. $id_profile = (int)Tools::getValue('id_profile');
  144. $where = '`id_tab`';
  145. $join = '';
  146. if (Tools::isSubmit('addFromParent'))
  147. {
  148. $where = 't.`id_parent`';
  149. $join = 'LEFT JOIN `'._DB_PREFIX_.'tab` t ON (t.`id_tab` = a.`id_tab`)';
  150. }
  151. if ($id_tab == -1)
  152. {
  153. if ($perm == 'all')
  154. $sql = '
  155. UPDATE `'._DB_PREFIX_.'access` a
  156. SET `view` = '.(int)$enabled.', `add` = '.(int)$enabled.', `edit` = '.(int)$enabled.', `delete` = '.(int)$enabled.'
  157. WHERE `id_profile` = '.(int)$id_profile;
  158. else
  159. $sql = '
  160. UPDATE `'._DB_PREFIX_.'access` a
  161. SET `'.bqSQL($perm).'` = '.(int)$enabled.'
  162. WHERE `id_profile` = '.(int)$id_profile;
  163. }
  164. else
  165. {
  166. if ($perm == 'all')
  167. $sql = '
  168. UPDATE `'._DB_PREFIX_.'access` a '.$join.'
  169. SET `view` = '.(int)$enabled.', `add` = '.(int)$enabled.', `edit` = '.(int)$enabled.', `delete` = '.(int)$enabled.'
  170. WHERE '.$where.' = '.(int)$id_tab.' AND `id_profile` = '.(int)$id_profile;
  171. else
  172. $sql = '
  173. UPDATE `'._DB_PREFIX_.'access` a '.$join.'
  174. SET `'.bqSQL($perm).'` = '.(int)$enabled.'
  175. WHERE '.$where.' = '.(int)$id_tab.' AND `id_profile` = '.(int)$id_profile;
  176. }
  177. $res = Db::getInstance()->execute($sql) ? 'ok' : 'error';
  178. die($res);
  179. }
  180. }
  181. public function ajaxProcessUpdateModuleAccess()
  182. {
  183. if (_PS_MODE_DEMO_)
  184. throw new PrestaShopException(Tools::displayError('This functionality has been disabled.'));
  185. if ($this->tabAccess['edit'] != '1')
  186. throw new PrestaShopException(Tools::displayError('You do not have permission to edit this.'));
  187. if (Tools::isSubmit('changeModuleAccess'))
  188. {
  189. $perm = Tools::getValue('perm');
  190. $enabled = (int)Tools::getValue('enabled');
  191. $id_module = (int)Tools::getValue('id_module');
  192. $id_profile = (int)Tools::getValue('id_profile');
  193. if (!in_array($perm, array('view', 'configure')))
  194. throw new PrestaShopException('permission does not exist');
  195. if ($id_module == -1)
  196. $sql = '
  197. UPDATE `'._DB_PREFIX_.'module_access`
  198. SET `'.bqSQL($perm).'` = '.(int)$enabled.'
  199. WHERE `id_profile` = '.(int)$id_profile;
  200. else
  201. $sql = '
  202. UPDATE `'._DB_PREFIX_.'module_access`
  203. SET `'.bqSQL($perm).'` = '.(int)$enabled.'
  204. WHERE `id_module` = '.(int)$id_module.'
  205. AND `id_profile` = '.(int)$id_profile;
  206. $res = Db::getInstance()->execute($sql) ? 'ok' : 'error';
  207. die($res);
  208. }
  209. }
  210. /**
  211. * Get the current profile id
  212. *
  213. * @return the $_GET['profile'] if valid, else 1 (the first profile id)
  214. */
  215. public function getCurrentProfileId()
  216. {
  217. return (isset($_GET['id_profile']) && !empty($_GET['id_profile']) && is_numeric($_GET['id_profile'])) ? (int)$_GET['id_profile'] : 1;
  218. }
  219. private function sortModuleByName($a, $b)
  220. {
  221. return strnatcmp($a['name'], $b['name']);
  222. }
  223. }