From 3d2a826865af6630ce35aa50fccf61b203867e40 Mon Sep 17 00:00:00 2001 From: Jon Bendtsen Date: Thu, 4 Dec 2025 16:51:47 +0100 Subject: [PATCH] FIX #36407 BUG: API POST contact to a non-existing socid gives DB error --- htdocs/societe/class/api_contacts.class.php | 23 +++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/htdocs/societe/class/api_contacts.class.php b/htdocs/societe/class/api_contacts.class.php index f497704568d..17cf3cdccc4 100644 --- a/htdocs/societe/class/api_contacts.class.php +++ b/htdocs/societe/class/api_contacts.class.php @@ -22,6 +22,7 @@ use Luracast\Restler\RestException; //require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php'; //require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; +require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; /** @@ -365,6 +366,17 @@ class Contacts extends DolibarrApi } continue; } + if ($field == 'socid') { + $new_socid = (int) $value; + $loopthirdpartytmp = new Societe($this->db); + $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid); + if ($new_thirdparty_result < 1) { + throw new RestException(404, 'Thirdparty with id='.$new_socid.' not found or not allowed'); + } + if (!DolibarrApi::_checkAccessToResource('societe', $new_socid)) { + throw new RestException(403, 'Access to socid/thirdparty='.$new_socid.' is not allowed for login '.DolibarrApiAccess::$user->login); + } + } $this->contact->$field = $this->_checkValForAPI($field, $value, $this->contact); } @@ -422,6 +434,17 @@ class Contacts extends DolibarrApi } continue; } + if ($field == 'socid') { + $new_socid = (int) $value; + $loopthirdpartytmp = new Societe($this->db); + $new_thirdparty_result = $loopthirdpartytmp->fetch($new_socid); + if ($new_thirdparty_result < 1) { + throw new RestException(404, 'Thirdparty with id='.$new_socid.' not found or not allowed'); + } + if (!DolibarrApi::_checkAccessToResource('societe', $new_socid)) { + throw new RestException(403, 'Access to socid/thirdparty='.$new_socid.' is not allowed for login '.DolibarrApiAccess::$user->login); + } + } $this->contact->$field = $this->_checkValForAPI($field, $value, $this->contact); }