diff --git a/htdocs/contrat/class/api_contracts.class.php b/htdocs/contrat/class/api_contracts.class.php index d6ecec7a5ef69..d56df54fe7168 100644 --- a/htdocs/contrat/class/api_contracts.class.php +++ b/htdocs/contrat/class/api_contracts.class.php @@ -21,6 +21,7 @@ use Luracast\Restler\RestException; require_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php'; +require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php'; /** * API class for contracts @@ -76,7 +77,7 @@ public function get($id) } if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { - throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login); } $this->contract->fetchObjectLinked(); @@ -208,8 +209,16 @@ public function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, public function post($request_data = null) { if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) { - throw new RestException(403, "Insufficient rights"); + throw new RestException(403, "Missing permission: Create/modify contracts/subscriptions"); } + + $socid = (int) $request_data['socid']; + $thirdpartytmp = new Societe($this->db); + $thirdparty_result = $thirdpartytmp->fetch($socid); + if ($thirdparty_result < 1) { + throw new RestException(404, 'Thirdparty with id='.$socid.' not found or not allowed'); + } + // Check mandatory fields $result = $this->_validate($request_data); @@ -268,7 +277,7 @@ public function getLines($id, $sortfield = "d.rowid", $sortorder = 'ASC', $limit } if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { - throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login); } $obj_ret = []; @@ -360,7 +369,7 @@ public function postLine($id, $request_data = null) } if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { - throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login); } $request_data = (object) $request_data; @@ -420,7 +429,7 @@ public function putLine($id, $lineid, $request_data = null) } if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { - throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login); } $request_data = (object) $request_data; @@ -568,7 +577,7 @@ public function activateLine($id, $lineid, $datestart, $dateend = null, $comment } if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { - throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login); } $updateRes = $this->contract->active_line(DolibarrApiAccess::$user, $lineid, (int) $datestart, $dateend, $comment); @@ -606,7 +615,7 @@ public function unactivateLine($id, $lineid, $datestart, $comment = null) } if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { - throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login); } $updateRes = $this->contract->close_line(DolibarrApiAccess::$user, $lineid, (int) $datestart, $comment); @@ -646,7 +655,7 @@ public function deleteLine($id, $lineid) } if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { - throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login); } // TODO Check the lineid $lineid is a line of object @@ -673,14 +682,26 @@ public function put($id, $request_data = null) if (!DolibarrApiAccess::$user->hasRight('contrat', 'creer')) { throw new RestException(403); } - $result = $this->contract->fetch($id); if (!$result) { throw new RestException(404, 'Contrat not found'); } + $old_socid = $this->contract->socid; + $oldthirdpartytmp = new Societe($this->db); + $old_thirdparty_result = $oldthirdpartytmp->fetch($old_socid); + if ($old_thirdparty_result < 1) { + throw new RestException(404, 'Thirdparty with id='.$old_socid.' not found or not allowed'); + } + $new_socid = (int) $request_data['socid']; + $newthirdpartytmp = new Societe($this->db); + $new_thirdparty_result = $newthirdpartytmp->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('contrat', $this->contract->id)) { - throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login); } foreach ($request_data as $field => $value) { if ($field == 'id') { @@ -698,6 +719,15 @@ public function put($id, $request_data = null) 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'); + } + } + $this->contract->$field = $this->_checkValForAPI($field, $value, $this->contract); } @@ -720,7 +750,7 @@ public function put($id, $request_data = null) public function delete($id) { if (!DolibarrApiAccess::$user->hasRight('contrat', 'supprimer')) { - throw new RestException(403); + throw new RestException(403, 'Missing permission: Delete contracts/subscriptions'); } $result = $this->contract->fetch($id); if (!$result) { @@ -728,7 +758,7 @@ public function delete($id) } if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { - throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login); } if (!$this->contract->delete(DolibarrApiAccess::$user)) { @@ -773,7 +803,7 @@ public function validate($id, $notrigger = 0) } if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { - throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login); } $result = $this->contract->validate(DolibarrApiAccess::$user, '', $notrigger); @@ -822,7 +852,7 @@ public function close($id, $notrigger = 0) } if (!DolibarrApi::_checkAccessToResource('contrat', $this->contract->id)) { - throw new RestException(403, 'Access not allowed for login '.DolibarrApiAccess::$user->login); + throw new RestException(403, 'Access to this contract is not allowed for login '.DolibarrApiAccess::$user->login); } $result = $this->contract->closeAll(DolibarrApiAccess::$user, $notrigger);