From 152bbeb90a9ad54509d0e9be90b0fadb7f8464b8 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 21 Mar 2023 18:27:32 +0100 Subject: [PATCH] Securize and enhance objectonoff for external modules --- htdocs/core/ajax/objectonoff.php | 57 +++++++++++++++---- htdocs/core/class/commonobject.class.php | 3 +- htdocs/core/lib/ajax.lib.php | 5 +- htdocs/core/lib/security.lib.php | 10 ++-- .../template/class/myobject.class.php | 4 +- 5 files changed, 57 insertions(+), 22 deletions(-) diff --git a/htdocs/core/ajax/objectonoff.php b/htdocs/core/ajax/objectonoff.php index 77cd3234cd8..54344da699e 100644 --- a/htdocs/core/ajax/objectonoff.php +++ b/htdocs/core/ajax/objectonoff.php @@ -18,7 +18,7 @@ /** * \file htdocs/core/ajax/objectonoff.php * \brief File to set status for an object - * This Ajax service is called when option MAIN_DIRECT_STATUS_UPDATE is set. + * This Ajax service is oftenly called when option MAIN_DIRECT_STATUS_UPDATE is set. */ if (!defined('NOTOKENRENEWAL')) { @@ -53,15 +53,34 @@ $format = 'int'; $object = new GenericObject($db); -$tablename = $element; -if ($tablename == 'websitepage') { - $tablename = 'website_page'; -} +$tmparray = explode('@', $element); +if (empty($tmparray[1])) { + $subelement = ''; -$object->table_element = $tablename; + $object->module = $element; + $object->element = $element; + $object->table_element = $element; + + // Special case for compatibility + if ($object->table_element == 'websitepage') { + $object->table_element = 'website_page'; + } +} else { + $element = $tmparray[0]; + $subelement = $tmparray[1]; + + $object->module = $element; + $object->element = $subelement; + $object->table_element = $object->module.'_'.$object->element; +} $object->id = $id; $object->fields[$field] = array('type' => $format, 'enabled' => 1); +$module = $object->module; +$element = $object->element; + +//var_dump($object->module); var_dump($object->element); var_dump($object->table_element); + // Security check if (!empty($user->socid)) { $socid = $user->socid; @@ -70,12 +89,20 @@ if (!empty($user->socid)) { //$user->hasRight('societe', 'lire') = 0;$user->rights->fournisseur->lire = 0; //restrictedArea($user, 'societe', $id); -if (in_array($field, array('status'))) { - restrictedArea($user, $element, $id); +// We check permission. +// Check is done on $user->rights->element->create or $user->rights->element->subelement->create (because $action = 'set') +if (preg_match('/status$/', $field)) { + $module = $object->module; + $element = $object->element; + $usesublevelpermission = ($module != $element ? $element : ''); + if ($usesublevelpermission && !isset($user->rights->$module->$element)) { // There is no permission on object defined, we will check permission on module directly + $usesublevelpermission = ''; + } + restrictedArea($user, $object->module, $object, $object->table_element, $usesublevelpermission); } elseif ($element == 'product' && in_array($field, array('tosell', 'tobuy', 'tobatch'))) { // Special case for products - restrictedArea($user, 'produit|service', $id, 'product&product', '', '', 'rowid'); + restrictedArea($user, 'produit|service', $object, 'product&product', '', '', 'rowid'); } else { - httponly_accessforbidden("Bad value for combination of parameters element/field."); + httponly_accessforbidden("Bad value for combination of parameters element/field."); // This includes the exit. } @@ -89,7 +116,7 @@ print '