Skip to content

Conversation

@JonBendtsen
Copy link
Contributor

@JonBendtsen JonBendtsen commented Oct 29, 2025

QUAL API contract: RestException update, check if thirdparty exists before use in POST/PUT

This PR adds some RestExceptions to the contract api. It also checks if thirdparty exists before creating/updating a contract. As well as introduce some hurl testing of api contract

@JonBendtsen
Copy link
Contributor Author

New result if thirtparty id does not exist
image

Old result

{
  "error": {
    "0": "UnknownError: Cannot add or update a child row: a foreign key constraint fails (`dolidb`.`llx_contrat`, CONSTRAINT `fk_contrat_fk_soc` FOREIGN KEY (`fk_soc`) REFERENCES `llx_societe` (`rowid`))",
    "code": 500,
    "message": "Internal Server Error: Error creating contract"
  }
}

@JonBendtsen
Copy link
Contributor Author

PUT error before changes

{
  "error": {
    "code": 500,
    "message": "Internal Server Error: Error Cannot add or update a child row: a foreign key constraint fails (`dolidb`.`llx_contrat`, CONSTRAINT `fk_contrat_fk_soc` FOREIGN KEY (`fk_soc`) REFERENCES `llx_societe` (`rowid`))"
  }
}

New put error message :-)
image

@JonBendtsen JonBendtsen force-pushed the issue_35655_api_contracts branch from 086a7d6 to d67eeb4 Compare October 29, 2025 21:56
@JonBendtsen
Copy link
Contributor Author

The improvements that this PR addresses was detected during bug #35655

@JonBendtsen JonBendtsen force-pushed the issue_35655_api_contracts branch from d67eeb4 to 0909f6c Compare October 29, 2025 22:16
@JonBendtsen JonBendtsen changed the title QUAL: API contract: RestException update, check if thirdparty exists before creating contract QUAL: API contract: RestException update, check if thirdparty exists before creating/updating contract Oct 29, 2025
@JonBendtsen JonBendtsen force-pushed the issue_35655_api_contracts branch 3 times, most recently from 3c17736 to ea23265 Compare October 30, 2025 06:53
@JonBendtsen
Copy link
Contributor Author

Hurl tests good - one of these days autocorrect is gonna write hurl tastes good ;-)

JonSweet16:hurl jonbendtsen$ ./run.sh 
----- Run hurl test on APIs ---
First we run tests that do not require authentication
Success gui/00_HOME.hurl (1 request(s) in 293 ms)
Success api/00_foobar.hurl (1 request(s) in 326 ms)
Success api/status/00_status.hurl (1 request(s) in 343 ms)
Success api/00_explorer.hurl (1 request(s) in 349 ms)
Success public/payment/00_payment_newpayment.hurl (1 request(s) in 381 ms)
Success public/onlinesign/00_onlinesign_newonlinesign.hurl (1 request(s) in 400 ms)
Success api/login/00_login_POST.hurl (1 request(s) in 1347 ms)
Success api/login/00_login_GET.hurl (1 request(s) in 1347 ms)
--------------------------------------------------------------------------------
Executed files:    8
Executed requests: 8 (5.9/s)
Succeeded files:   8 (100.0%)
Failed files:      0 (0.0%)
Duration:          1350 ms

Now we are ready to run API tests that do require authentication
Success api/status/10_status.hurl (1 request(s) in 137 ms)
Success api/setup/10_setup_conf.hurl (1 request(s) in 514 ms)
Success api/setup/10_setup_company.hurl (1 request(s) in 535 ms)
Success api/mailings/10_mailings.hurl (34 request(s) in 3238 ms)
Success api/emailtemplates/10_emailtemplates.hurl (19 request(s) in 3553 ms)
Success api/users/10_users.hurl (17 request(s) in 3615 ms)
Success api/contracts/10_contracts.hurl (21 request(s) in 3813 ms)
Success api/warehouses/10_warehouses.hurl (34 request(s) in 5433 ms)
Success api/setup/10_setup_modules.hurl (14 request(s) in 5657 ms)
--------------------------------------------------------------------------------
Executed files:    10
Executed requests: 155 (27.4/s)
Succeeded files:   9 (90.0%)
Failed files:      1 (10.0%)
Duration:          5659 ms

@JonBendtsen JonBendtsen marked this pull request as ready for review October 30, 2025 07:12
@JonBendtsen
Copy link
Contributor Author

Maybe #36024 has to be merged before, because they might conflict?

@hregis
Copy link
Contributor

hregis commented Oct 31, 2025

@JonBendtsen

for acivateLine function the right is : DolibarrApiAccess::$user->hasRight('contrat', 'activer')
for unactivateLine function the right is : DolibarrApiAccess::$user->hasRight('contrat', 'desactiver')
in post function $this->contract->entity don't exists during the creation !

@JonBendtsen JonBendtsen force-pushed the issue_35655_api_contracts branch from e1ccc1d to ef3e495 Compare October 31, 2025 20:28
@eldy eldy merged commit 20a428e into Dolibarr:develop Nov 1, 2025
5 checks passed
@hregis
Copy link
Contributor

hregis commented Nov 2, 2025

@JonBendtsen @eldy

this is not good !! getEntity() can return "1,2,3,x" !

if ($field == 'entity' && $value != getEntity('contrat')) {

@hregis
Copy link
Contributor

hregis commented Nov 2, 2025

@JonBendtsen getEntity('contrat') return contract sharings with the current entity, not the authorized sharing of a user

@eldy
Copy link
Member

eldy commented Nov 2, 2025

@JonBendtsen getEntity('contrat') return contract sharings with the current entity, not the authorized sharing of a user

We could make the test on $conf->entity because conf->entity is already value we are logged on with DOLAPIENTITY
Or we can just replace with a inarray

We can also add a check in phpunit in CodingPhpTest to return a CI error if we find "= getEntity" to avoid error in future.

@eldy
Copy link
Member

eldy commented Nov 2, 2025

@JonBendtsen getEntity('contrat') return contract sharings with the current entity, not the authorized sharing of a user

@hregis I am not sure I understand where and why there is an issue.

getEntity() is used in public function index which I did not change, and in public function getLineswhich I also did not change?

Do you want me to use getEntity() more places? or remove the usage?

Pb is just that you do a != on string with several int instead of an int.
Try
!in_array((string) $entity, explode(',', getEntity('contract')))

@JonBendtsen
Copy link
Contributor Author

I had some editor update issues such that it did not show the right file contents even though I had switched the branch - probably because I changed the branch out under it. I will submit a new PR

@hregis
Copy link
Contributor

hregis commented Nov 2, 2025

@JonBendtsen @eldy The creation and updating of contracts (or other objects) should be limited to "$conf->entity" because contracts can be shared, but if we are in the current entity with value 1 and the "$entity" field at the time of creation is set to 2... we should also check if the user has the right to create contracts on entity 2!

aummind pushed a commit to aummind/dolibarr that referenced this pull request Nov 13, 2025
…before creating/updating contract (Dolibarr#36007)

* QUAL: API contract: RestException update, check if thirdparty exists before creating contract

* also checking if socid exists during put

* using the entity of this contract

* also fixing entity during post

* some hurl tests of contract api

* check if contract id is 0, because that is not possible

* check (un)activate permissions and getEntity()

---------

Co-authored-by: Jon Bendtsen <[email protected]>
Co-authored-by: Laurent Destailleur <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants