Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- 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.3.1] - 2026-10-06

Expand Down
21 changes: 5 additions & 16 deletions src/Model/TicketReservationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
54 changes: 54 additions & 0 deletions tests/ReservationWorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

namespace GlpiPlugin\Advancedforms\Tests;

use CommonITILActor;
use Glpi\Exception\Http\AccessDeniedHttpException;
use Glpi\Tests\FormBuilder;
use GlpiPlugin\Advancedforms\Controller\ReservationRequestController;
Expand All @@ -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). */
Expand Down Expand Up @@ -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
{
Expand Down
Loading