From 6ec1b9c274999f5d0ad89d9639d8bbd1eaa9bf43 Mon Sep 17 00:00:00 2001 From: "Romain B." <8530352+Rom1-B@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:23:41 +0100 Subject: [PATCH] Fix: prevent requesters from approving own reservations (#93) ## Checklist before requesting a review *Please delete options that are not relevant.* - [x] I have performed a self-review of my code. - [x] I have added tests (when available) that prove my fix is effective or that my feature works. - [x] I have updated the CHANGELOG with a short functional description of the fix or new feature. - [ ] This change requires a documentation update. ## Description - It fixes #68 - Any requester, including self-service users on the simplified interface, could approve or refuse their own reservation request from the ticket timeline, as long as the ticket had no followup or task yet. - Answering a reservation request now requires the standard interface and the right to create reservations for other users, so only actual validators see and can use the approve/refuse actions. ## Screenshots (if appropriate): (cherry picked from commit b0fe8cfd561701f5e07ef813dcaaa99a9523da27) --- CHANGELOG.md | 1 + src/Model/TicketReservationRequest.php | 21 +++------- tests/ReservationWorkflowTest.php | 54 ++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1438de3..e6a0cb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fix GLPI compatibility constraint in plugin XML - Fix hidden question not available in anonymous forms - Fix `Tree Cascade Dropdown` question ignoring dropdown translations +- Fix users without the right to reserve for others being able to approve or refuse reservation requests ## [1.4.0] - 2026-10-06 diff --git a/src/Model/TicketReservationRequest.php b/src/Model/TicketReservationRequest.php index 7de4c9a..a5c44a2 100644 --- a/src/Model/TicketReservationRequest.php +++ b/src/Model/TicketReservationRequest.php @@ -268,22 +268,11 @@ public function canAnswer(): bool return false; } - if (!$ticket->canUpdateItem()) { - return false; - } - - // Approving/refusing creates (or would create) a Reservation for this - // request's own requester, not for whoever answers it. Mirror core's - // rule in Reservation::handleAddForm(): creating a reservation for - // someone else requires CREATE, not just RESERVEANITEM. - if ( - $this->getIntField('users_id') !== (int) Session::getLoginUserID() - && !Session::haveRight(Reservation::$rightname, CREATE) - ) { - return false; - } - - return true; + // canUpdateItem() alone lets any requester answer while the ticket is fresh, so also require the right + // core asks to reserve for others; such a user may answer their own request, as they could reserve directly. + return Session::getCurrentInterface() === 'central' + && Session::haveRight(Reservation::$rightname, CREATE) + && $ticket->canUpdateItem(); } /** Removes every pending/accepted request tied to a purged Ticket, along with any Reservation it created. */ diff --git a/tests/ReservationWorkflowTest.php b/tests/ReservationWorkflowTest.php index 979a3bf..89be5db 100644 --- a/tests/ReservationWorkflowTest.php +++ b/tests/ReservationWorkflowTest.php @@ -33,6 +33,7 @@ namespace GlpiPlugin\Advancedforms\Tests; +use CommonITILActor; use Glpi\Exception\Http\AccessDeniedHttpException; use Glpi\Tests\FormBuilder; use GlpiPlugin\Advancedforms\Controller\ReservationRequestController; @@ -51,6 +52,8 @@ use Session; use Symfony\Component\HttpFoundation\Request; use Ticket; +use Ticket_User; +use User; use UserEmail; /** End-to-end test covering the integration seams across all reservation pieces (see per-class unit tests for the rest). */ @@ -296,6 +299,57 @@ public function testApproveDeniedWithoutTicketUpdateRightLeavesRequestUnchanged( $this->assertSame(TicketReservationRequest::STATUS_WAITING, (int) $request->fields['status']); } + public function testRequesterCannotApproveOwnRequest(): void + { + [$ticket] = $this->submitReservationForm(require_approval: true); + + $request = new TicketReservationRequest(); + $rows = $request->find(['tickets_id' => $ticket->getID()]); + $this->assertCount(1, $rows); + $this->assertTrue($request->getFromDB((int) array_key_first($rows))); + + // Make the self-service "post-only" user the requester of both the ticket and the reservation request. + $users_id = getItemByTypeName(User::class, 'post-only', true); + $this->createItem(Ticket_User::class, [ + 'tickets_id' => $ticket->getID(), + 'users_id' => $users_id, + 'type' => CommonITILActor::REQUESTER, + ]); + $this->updateItem(TicketReservationRequest::class, $request->getID(), ['users_id' => $users_id]); + $this->assertTrue($request->getFromDB($request->getID())); + + $this->login('post-only', 'postonly'); + $this->assertTrue($ticket->getFromDB($ticket->getID())); + $this->assertTrue($ticket->canUpdateItem()); + + $this->assertFalse($request->canAnswer()); + + $this->expectException(AccessDeniedHttpException::class); + (new ReservationRequestController())(Request::create('/', 'POST', [ + 'id' => $request->getID(), + 'action' => 'approve', + ])); + } + + public function testCentralRequesterWithoutReservationCreateRightCannotAnswer(): void + { + [$ticket] = $this->submitReservationForm(require_approval: true); + + $request = new TicketReservationRequest(); + $rows = $request->find(['tickets_id' => $ticket->getID()]); + $this->assertCount(1, $rows); + $this->assertTrue($request->getFromDB((int) array_key_first($rows))); + + $this->updateItem(TicketReservationRequest::class, $request->getID(), ['users_id' => Session::getLoginUserID()]); + $this->assertTrue($request->getFromDB($request->getID())); + $this->assertTrue($request->canAnswer()); + + $_SESSION['glpiactiveprofile'][Reservation::$rightname] = READ | ReservationItem::RESERVEANITEM; + $this->assertTrue($ticket->canUpdateItem()); + + $this->assertFalse($request->canAnswer()); + } + /** Counts queued notifications for TicketReservationRequest only, ignoring unrelated core ones (New ticket, etc). */ private function countReservationRequestQueuedNotifications(?string $event = null): int {