PageRenderTime 29ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/admin/AdminAccessController.php

https://bitbucket.org/fanch1/testlb
PHP | 225 lines | 146 code | 31 blank | 48 comment | 18 complexity | 5ba9021f847962885a4aa9128b2cecc9 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, BSD-2-Clause, GPL-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * 2007-2017 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. * https://opensource.org/licenses/OSL-3.0
  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-2017 PrestaShop SA
  23. * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * @property Profile $object
  28. */
  29. class AdminAccessControllerCore extends AdminController
  30. {
  31. /* @var array : Black list of id_tab that do not have access */
  32. public $accesses_black_list = array();
  33. public function __construct()
  34. {
  35. $this->bootstrap = true;
  36. $this->show_toolbar = false;
  37. $this->table = 'access';
  38. $this->className = 'Profile';
  39. $this->multishop_context = Shop::CONTEXT_ALL;
  40. $this->lang = false;
  41. $this->context = Context::getContext();
  42. // Blacklist AdminLogin
  43. $this->accesses_black_list[] = Tab::getIdFromClassName('AdminLogin');
  44. parent::__construct();
  45. }
  46. /**
  47. * AdminController::renderForm() override
  48. * @see AdminController::renderForm()
  49. */
  50. public function renderForm()
  51. {
  52. $current_profile = (int)$this->getCurrentProfileId();
  53. $profiles = Profile::getProfiles($this->context->language->id);
  54. $tabs = Tab::getTabs($this->context->language->id);
  55. $accesses = array();
  56. foreach ($profiles as $profile) {
  57. $accesses[$profile['id_profile']] = Profile::getProfileAccesses($profile['id_profile']);
  58. }
  59. // Deleted id_tab that do not have access
  60. foreach ($tabs as $key => $tab) {
  61. // Don't allow permissions for unnamed tabs (ie. AdminLogin)
  62. if (empty($tab['name'])) {
  63. unset($tabs[$key]);
  64. }
  65. foreach ($this->accesses_black_list as $id_tab) {
  66. if ($tab['id_tab'] == (int)$id_tab) {
  67. unset($tabs[$key]);
  68. }
  69. }
  70. }
  71. $modules = array();
  72. foreach ($profiles as $profile) {
  73. $modules[$profile['id_profile']] = Module::getModulesAccessesByIdProfile($profile['id_profile']);
  74. uasort($modules[$profile['id_profile']], array($this, 'sortModuleByName'));
  75. }
  76. $this->fields_form = array('');
  77. $this->tpl_form_vars = array(
  78. 'profiles' => $profiles,
  79. 'accesses' => $accesses,
  80. 'id_tab_parentmodule' => (int)Tab::getIdFromClassName('AdminParentModules'),
  81. 'id_tab_module' => (int)Tab::getIdFromClassName('AdminModules'),
  82. 'tabs' => $this->displayTabs($tabs),
  83. 'current_profile' => (int)$current_profile,
  84. 'admin_profile' => (int)_PS_ADMIN_PROFILE_,
  85. 'access_edit' => $this->access('edit'),
  86. 'perms' => array('view', 'add', 'edit', 'delete'),
  87. 'id_perms' => array('view' => 0, 'add' => 1, 'edit' => 2, 'delete' => 3, 'all' => 4),
  88. 'modules' => $modules,
  89. 'link' => $this->context->link,
  90. 'employee_profile_id' => (int) $this->context->employee->id_profile,
  91. );
  92. return parent::renderForm();
  93. }
  94. /**
  95. * AdminController::initContent() override
  96. * @see AdminController::initContent()
  97. */
  98. public function initContent()
  99. {
  100. $this->display = 'edit';
  101. if (!$this->loadObject(true)) {
  102. return;
  103. }
  104. $this->content .= $this->renderForm();
  105. $this->context->smarty->assign(array(
  106. 'content' => $this->content,
  107. ));
  108. }
  109. public function initToolbarTitle()
  110. {
  111. $this->toolbar_title = array_unique($this->breadcrumbs);
  112. }
  113. public function initPageHeaderToolbar()
  114. {
  115. parent::initPageHeaderToolbar();
  116. unset($this->page_header_toolbar_btn['cancel']);
  117. }
  118. public function ajaxProcessUpdateAccess()
  119. {
  120. if (_PS_MODE_DEMO_) {
  121. throw new PrestaShopException($this->trans('This functionality has been disabled.', array(), 'Admin.Notifications.Error'));
  122. }
  123. if ($this->access('edit') != '1') {
  124. throw new PrestaShopException($this->trans('You do not have permission to edit this.', array(), 'Admin.Notifications.Error'));
  125. }
  126. if (Tools::isSubmit('submitAddAccess')) {
  127. $res = array();
  128. $access = new Access;
  129. $perm = Tools::getValue('perm');
  130. if (!in_array($perm, array('view', 'add', 'edit', 'delete', 'all'))) {
  131. throw new PrestaShopException('permission does not exist');
  132. }
  133. $enabled = (int)Tools::getValue('enabled');
  134. $id_tab = (int)Tools::getValue('id_tab');
  135. $id_profile = (int)Tools::getValue('id_profile');
  136. $addFromParent = (int)Tools::getValue('addFromParent');
  137. die($access->updateLgcAccess((int)$id_profile, $id_tab, $perm, $enabled, $addFromParent));
  138. }
  139. }
  140. public function ajaxProcessUpdateModuleAccess()
  141. {
  142. if (_PS_MODE_DEMO_) {
  143. throw new PrestaShopException($this->trans('This functionality has been disabled.', array(), 'Admin.Notifications.Error'));
  144. }
  145. if ($this->access('edit') != '1') {
  146. throw new PrestaShopException($this->trans('You do not have permission to edit this.', array(), 'Admin.Notifications.Error'));
  147. }
  148. if (Tools::isSubmit('changeModuleAccess')) {
  149. $access = new Access;
  150. $perm = Tools::getValue('perm');
  151. $enabled = (int)Tools::getValue('enabled');
  152. $id_module = (int)Tools::getValue('id_module');
  153. $id_profile = (int)Tools::getValue('id_profile');
  154. if (!in_array($perm, array('view', 'configure', 'uninstall'))) {
  155. throw new PrestaShopException('permission does not exist');
  156. }
  157. die($access->updateLgcModuleAccess((int)$id_profile, $id_module, $perm, $enabled));
  158. }
  159. }
  160. /**
  161. * Get the current profile id
  162. *
  163. * @return int the $_GET['profile'] if valid, else 1 (the first profile id)
  164. */
  165. public function getCurrentProfileId()
  166. {
  167. return (isset($_GET['id_profile']) && !empty($_GET['id_profile']) && is_numeric($_GET['id_profile'])) ? (int)$_GET['id_profile'] : 1;
  168. }
  169. private function sortModuleByName($a, $b)
  170. {
  171. return strnatcmp($a['name'], $b['name']);
  172. }
  173. /**
  174. * return human readable Tabs hierarchy for display
  175. *
  176. */
  177. private function displayTabs(array $tabs)
  178. {
  179. $tabsTree = $this->getChildrenTab($tabs);
  180. return $tabsTree;
  181. }
  182. private function getChildrenTab(array &$tabs, $id_parent = 0)
  183. {
  184. $children = [];
  185. foreach ($tabs as &$tab) {
  186. $id = $tab['id_tab'];
  187. if ($tab['id_parent'] == $id_parent) {
  188. $children[$id] = $tab;
  189. $children[$id]['children'] = $this->getChildrenTab($tabs, $id);
  190. }
  191. }
  192. return $children;
  193. }
  194. }