diff --git a/admin-dev/tabs/AdminAccess.php b/admin-dev/tabs/AdminAccess.php index 141226d1d..5888fc254 100644 --- a/admin-dev/tabs/AdminAccess.php +++ b/admin-dev/tabs/AdminAccess.php @@ -29,26 +29,127 @@ include_once(_PS_ADMIN_DIR_.'/../classes/AdminTab.php'); class AdminAccess extends AdminTab { - public function postProcess() + private $return_status; + private $return_message; + + public function processSubmitAddAccess() { - if (Tools::isSubmit('submitAddaccess') AND $action = Tools::getValue('action') AND $id_tab = (int)(Tools::getValue('id_tab')) AND $id_profile = (int)(Tools::getValue('id_profile')) AND $this->tabAccess['edit'] == 1) - { - if ($id_tab == -1 AND $action == 'all' AND (int)(Tools::getValue('perm')) == 0) - Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'access` SET `view` = '.(int)(Tools::getValue('perm')).', `add` = '.(int)(Tools::getValue('perm')).', `edit` = '.(int)(Tools::getValue('perm')).', `delete` = '.(int)(Tools::getValue('perm')).' WHERE `id_profile` = '.(int)($id_profile).' AND `id_tab` != 31'); - elseif ($id_tab == -1 AND $action == 'all') - Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'access` SET `view` = '.(int)(Tools::getValue('perm')).', `add` = '.(int)(Tools::getValue('perm')).', `edit` = '.(int)(Tools::getValue('perm')).', `delete` = '.(int)(Tools::getValue('perm')).' WHERE `id_profile` = '.(int)($id_profile)); - elseif ($id_tab == -1) - Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'access` SET `'.pSQL($action).'` = '.(int)(Tools::getValue('perm')).' WHERE `id_profile` = '.(int)($id_profile)); - elseif ($action == 'all') - Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'access` SET `view` = '.(int)(Tools::getValue('perm')).', `add` = '.(int)(Tools::getValue('perm')).', `edit` = '.(int)(Tools::getValue('perm')).', `delete` = '.(int)(Tools::getValue('perm')).' WHERE `id_tab` = '.(int)($id_tab).' AND `id_profile` = '.(int)($id_profile)); - else - Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'access` SET `'.pSQL($action).'` = '.(int)(Tools::getValue('perm')).' WHERE `id_tab` = '.(int)($id_tab).' AND `id_profile` = '.(int)($id_profile)); - } + $perm = Tools::getValue('perm') ; + if (!in_array($perm, array('view', 'add', 'edit', 'delete', 'all'))) + throw new PrestashopException('permission not exists'); + + $enabled = (int)Tools::getValue('enabled') ; + $id_tab = (int)(Tools::getValue('id_tab')); + $id_profile = (int)(Tools::getValue('id_profile')); + $res = true; + + if ($id_tab == -1 AND $perm == 'all' AND $enabled == 0) + $res &= Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'access` SET `view` = '.$enabled.', `add` = '.$enabled.', `edit` = '.$enabled.', `delete` = '.$enabled.' WHERE `id_profile` = '.(int)($id_profile).' AND `id_tab` != 31'); + else if ($id_tab == -1 AND $perm == 'all') + $res &= Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'access` SET `view` = '.$enabled.', `add` = '.$enabled.', `edit` = '.$enabled.', `delete` = '.$enabled.' WHERE `id_profile` = '.(int)($id_profile)); + else if ($id_tab == -1) + $res &= Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'access` SET `'.pSQL($perm).'` = '.$enabled.' WHERE `id_profile` = '.(int)($id_profile)); + else if ($perm == 'all') + $res &= Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'access` SET `view` = '.$enabled.', `add` = '.$enabled.', `edit` = '.$enabled.', `delete` = '.$enabled.' WHERE `id_tab` = '.(int)($id_tab).' AND `id_profile` = '.(int)($id_profile)); + else + $res &= Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'access` SET `'.pSQL($perm).'` = '.$enabled.' WHERE `id_tab` = '.(int)($id_tab).' AND `id_profile` = '.(int)($id_profile)); + + $this->return_status = $res?'ok':'error'; + if ($res) + $this->return_message = $this->l('Access successfully updated'); + else + $this->return_message = $this->l('An error when updating access'); + } + + public function processChangeModuleAccess() + { + $perm = Tools::getValue('perm'); + $enabled = (int)Tools::getValue('enabled'); + $id_module = (int)Tools::getValue('id_module'); + $id_profile = (int)Tools::getValue('id_profile'); + $res = true; + + if (!in_array($perm, array('view', 'configure'))) + throw new PrestashopException('permission not exists'); + + if ($id_module == -1) + $res &= Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'module_access` SET `'.pSQL($perm).'` = '.(int)$enabled.' WHERE `id_profile` = '.(int)$id_profile); + else + $res &= Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'module_access` SET `'.pSQL($perm).'` = '.(int)$enabled.' WHERE `id_module` = '.(int)$id_module.' AND `id_profile` = '.(int)$id_profile); + + $this->return_status = $res?'ok':'error'; + if ($res) + $this->return_message = $this->l('Access successfully updated.'); + else + $this->return_message = $this->l('An error when updating access.'); } + + public function displayAjax() + { + $return = array('result'=>$this->return_status,'msg'=>$this->return_message); + + echo Tools::jsonEncode($return); + } public function display() { $this->displayForm(); + echo ''; } /** @@ -64,11 +165,10 @@ class AdminAccess extends AdminTab public function displayForm($isMainTab = true) { parent::displayForm(); - $currentProfile = (int)($this->getCurrentProfileId()); $tabs = Tab::getTabs($this->context->language->id); $profiles = Profile::getProfiles($this->context->language->id); - $accesses = Profile::getProfileAccesses($this->context->employee->id_profile); + $accesses = Profile::getProfileAccesses($currentProfile); echo '
| '.$this->l('Modules').' | -tabAccess['edit'] == 1 ? 'onclick="changeModuleAccess(this, -1, \'view\');"' : 'disabled="disabled"').' /> '.$this->l('View').' | -tabAccess['edit'] == 1 ? 'onclick="changeModuleAccess(this, -1, \'configure\');"' : 'disabled="disabled"').' /> '.$this->l('Configure').' | +tabAccess['edit'] == 1 ? 'class="changeModuleAccess" rel="-1||view"' : 'disabled="disabled"').' /> '.$this->l('View').' | +tabAccess['edit'] == 1 ? 'class="changeModuleAccess" rel="-1||configure"' : 'disabled="disabled"').' /> '.$this->l('Configure').' |
|---|---|---|---|---|
| » '.$module['name'].' | - tabAccess['edit'] == 1 ? 'onclick="changeModuleAccess(this, '.(int)$module['id_module'].', \'view\');"' : 'disabled="disabled"').' + '.($this->tabAccess['edit'] == 1 ? 'class="ajax-ma-view changeModuleAccess" rel="'.(int)$module['id_module'].'||view"' : ' class="ajax-ma-view" disabled="disabled"').' /> | - tabAccess['edit'] == 1 ? 'onclick="changeModuleAccess(this, '.(int)$module['id_module'].', \'configure\');"' : 'disabled="disabled"').' + '.($this->tabAccess['edit'] == 1 ? ' class="ajax-ma-configure changeModuleAccess" rel="'.(int)$module['id_module'].'||configure"' : ' class="ajax-ma-configure" disabled="disabled"').' /> |