diff --git a/SPECIFICATION.md b/SPECIFICATION.md index 1ffcd736..4194892c 100644 --- a/SPECIFICATION.md +++ b/SPECIFICATION.md @@ -655,7 +655,8 @@ Rules, enforced server-side at every surface: approval workflow, HR-only edit and cancel paths. - **The category is visible to HR alone.** Everyone else — the line manager *and the employee's own views* — sees the absence as a neutral "Absent": - dates and status, no type, no reason, no notes, no comments, no history. + dates and status, no type, no reason, no notes, no comments, no history, and + not who recorded it (`decidedBy` is null, so no `decidedByName` either). Withholding happens at serialization (`typeId` is nulled; the client already renders a null type as "Absent"), in the coverage/timeline feeds (where the admin's "reveal" visibility setting deliberately does not extend to these @@ -1425,7 +1426,7 @@ guarded by the appropriate middleware/attribute-based access checks the SPA). All list endpoints paginate and accept filters. **Attachments (§3.8)** -- `GET /api/requests/{id}/attachments` — list (HR + employee only; [] otherwise). +- `GET /api/requests/{id}/attachments`: list (HR + employee only; [] for the others who see the request). - `POST /api/requests/{id}/attachments` — multipart upload (field `file`). - `GET /api/attachments/{id}` — download (plain link; permission is the gate). - `DELETE /api/attachments/{id}` — HR or the uploader. @@ -1444,6 +1445,10 @@ the SPA). All list endpoints paginate and accept filters. allowance. Null for a type that counts against nothing. It is there because "took three days" says nothing about whether any are left, and finding out otherwise means abandoning the view for the Balances report). + A request the caller may not see is a 404 "Request not found", like an unknown id, + on every route of one request (detail, edit, cancel, decisions, comments and its + attachment list and upload): ids are sequential and would otherwise tell how many + requests exist. HR sees every request, so for HR a 404 means there is none. - `PUT /api/requests/{id}` — edit (§5.3; behavior depends on current status). A field left out stays as it is; an empty `replacementUid` removes the replacement. - `POST /api/requests/{id}/cancel` — cancel / request withdrawal. diff --git a/lib/Service/AttachmentService.php b/lib/Service/AttachmentService.php index 850ddaaf..a2742703 100644 --- a/lib/Service/AttachmentService.php +++ b/lib/Service/AttachmentService.php @@ -65,9 +65,10 @@ public function __construct( * The attachments an actor may see on a request; [] when they may see none. * * @return Attachment[] + * @throws NotFoundException */ public function listForRequest(string $actorUid, int $requestId): array { - $request = $this->loadRequest($requestId); + $request = $this->loadVisibleRequest($actorUid, $requestId); if (!$this->canSee($actorUid, $request)) { return []; } @@ -96,7 +97,7 @@ public function canAttach(string $actorUid, LeaveRequest $request): bool { * @throws ValidationException|ForbiddenException|NotFoundException */ public function upload(string $actorUid, int $requestId, array $upload): Attachment { - $request = $this->loadRequest($requestId); + $request = $this->loadVisibleRequest($actorUid, $requestId); if (!$this->canAttach($actorUid, $request)) { throw new ForbiddenException($this->l->t('Not allowed to attach files to this request')); } @@ -241,6 +242,19 @@ private function loadRequest(int $requestId): LeaveRequest { } } + /** + * A request the actor may not see is refused like one that does not exist. + * + * @throws NotFoundException + */ + private function loadVisibleRequest(string $actorUid, int $requestId): LeaveRequest { + $request = $this->loadRequest($requestId); + if (!$this->permission->canView($actorUid, $request)) { + throw new NotFoundException($this->l->t('Request not found')); + } + return $request; + } + private function loadAttachment(int $attachmentId): Attachment { try { return $this->attachmentMapper->find($attachmentId); diff --git a/lib/Service/RequestService.php b/lib/Service/RequestService.php index 249d85f7..a36e9e27 100644 --- a/lib/Service/RequestService.php +++ b/lib/Service/RequestService.php @@ -250,8 +250,10 @@ private function resolveReplacement(string $employeeUid, LeaveType $type, ?strin // ---------------------------------------------------------------- reads ---- /** + * A request the actor may not see is refused like one that does not exist: + * ids are sequential, so a different answer would tell how many there are. + * * @throws NotFoundException - * @throws ForbiddenException */ public function get(string $actorUid, int $id): LeaveRequest { try { @@ -260,7 +262,7 @@ public function get(string $actorUid, int $id): LeaveRequest { throw new NotFoundException($this->l->t('Request not found')); } if (!$this->permission->canView($actorUid, $request)) { - throw new ForbiddenException($this->l->t('Not allowed to view this request')); + throw new NotFoundException($this->l->t('Request not found')); } return $request; } @@ -270,7 +272,6 @@ public function get(string $actorUid, int $id): LeaveRequest { * * @return array * @throws NotFoundException - * @throws ForbiddenException */ public function getDetail(string $actorUid, int $id): array { $request = $this->get($actorUid, $id); @@ -465,8 +466,8 @@ private function availableAfter(LeaveRequest $request, array $rows): ?float { /** * Reduce a serialized confidential request (§5.7) to the bare fact of the * absence: dates and status stay (the calendar shows that much anyway); the - * category and everything written about it go. The client renders a null - * type as a neutral "Absent". + * category, everything written about it and who decided or recorded it go. + * The client renders a null type as a neutral "Absent". * * @param array $data * @return array @@ -477,6 +478,7 @@ private function withheld(array $data): array { $data['reason'] = null; $data['attachmentNote'] = null; $data['decisionComment'] = null; + $data['decidedBy'] = null; return $data; } diff --git a/tests/Integration/AccessMatrixTest.php b/tests/Integration/AccessMatrixTest.php index f8cfec95..f8068c05 100644 --- a/tests/Integration/AccessMatrixTest.php +++ b/tests/Integration/AccessMatrixTest.php @@ -66,6 +66,8 @@ class AccessMatrixTest extends TestCase { private const ADMIN = 'admin'; private const ROLES = [self::ANON, self::OUTSIDER, self::COLLEAGUE, self::OWNER, self::MANAGER, self::HR, self::ADMIN]; private const LOGGED_IN = [self::OUTSIDER, self::COLLEAGUE, self::OWNER, self::MANAGER, self::HR, self::ADMIN]; + /** Who may see the owner's request */ + private const VIEWERS = [self::OWNER, self::MANAGER, self::HR]; /** @var array */ private array $snapshot = []; @@ -80,13 +82,15 @@ class AccessMatrixTest extends TestCase { * route name => [allowed roles, parameters, how the others are turned * away]. Placeholders in braces are the fixtures' ids. Allowed roles must * not get 401 or 403; the others get 403 (401 when not logged in) before - * anything is read or written, or, for a list, an empty one. + * anything is read or written, or, for a list, an empty one. On the routes + * of one request ('hidden'), whoever may not see the request gets 404 as + * for an unknown id, and allowed roles must not get 404 either. * - * @return array, 1: array, 2?: 'empty'}> + * @return array, 1: array, 2?: 'empty'|'hidden'|'hidden-empty'}> */ private static function matrix(): array { $hr = [self::HR]; - $viewers = [self::OWNER, self::MANAGER, self::HR]; + $viewers = self::VIEWERS; $from = ['from' => '2027-03-01', 'to' => '2027-03-31']; return [ 'page#index' => [self::LOGGED_IN, []], @@ -95,16 +99,16 @@ private static function matrix(): array { 'config#updatePersonal' => [self::LOGGED_IN, ['values' => []]], 'request#index' => [self::LOGGED_IN, []], - 'request#show' => [$viewers, ['id' => '{request}']], + 'request#show' => [$viewers, ['id' => '{request}'], 'hidden'], 'request#create' => [self::LOGGED_IN, ['typeId' => '{type}', 'startDate' => '2027-03-01', 'endDate' => '2027-03-02']], - 'request#update' => [[self::OWNER, self::HR], ['id' => '{request}', 'reason' => 'itest']], - 'request#cancel' => [[self::OWNER, self::HR], ['id' => '{request}']], - 'request#approve' => [[self::MANAGER, self::HR], ['id' => '{request}', 'updatedAt' => '{updatedAt}']], - 'request#reject' => [[self::MANAGER, self::HR], ['id' => '{request}', 'comment' => 'itest', 'updatedAt' => '{updatedAt}']], - 'request#addComment' => [$viewers, ['id' => '{request}', 'body' => 'itest']], - - 'attachment#index' => [[self::OWNER, self::HR], ['id' => '{request}'], 'empty'], - 'attachment#create' => [[self::OWNER, self::HR], ['id' => '{request}', 'file' => '{upload}']], + 'request#update' => [[self::OWNER, self::HR], ['id' => '{request}', 'reason' => 'itest'], 'hidden'], + 'request#cancel' => [[self::OWNER, self::HR], ['id' => '{request}'], 'hidden'], + 'request#approve' => [[self::MANAGER, self::HR], ['id' => '{request}', 'updatedAt' => '{updatedAt}'], 'hidden'], + 'request#reject' => [[self::MANAGER, self::HR], ['id' => '{request}', 'comment' => 'itest', 'updatedAt' => '{updatedAt}'], 'hidden'], + 'request#addComment' => [$viewers, ['id' => '{request}', 'body' => 'itest'], 'hidden'], + + 'attachment#index' => [[self::OWNER, self::HR], ['id' => '{request}'], 'hidden-empty'], + 'attachment#create' => [[self::OWNER, self::HR], ['id' => '{request}', 'file' => '{upload}'], 'hidden'], 'attachment#download' => [[self::OWNER, self::HR], ['id' => '{attachment}']], 'attachment#destroy' => [[self::OWNER, self::HR], ['id' => '{attachment}']], @@ -330,11 +334,13 @@ public function testRoute(string $name): void { // The turned-away first: an allowed call may delete what they are asked about $denied = array_values(array_diff(self::ROLES, $allowed)); + $lists = in_array($turnedAway, ['empty', 'hidden-empty'], true); + $hidden = in_array($turnedAway, ['hidden', 'hidden-empty'], true); $got = []; foreach ([...$denied, ...$allowed] as $role) { $response = $this->call($role, $name, $params); $got[$role] = $response->getStatus(); - if ($turnedAway === 'empty' && $response->getStatus() === 200) { + if ($lists && $response->getStatus() === 200) { $got[$role] = $response->getData() === [] ? 'empty' : 'listed'; } } @@ -342,12 +348,13 @@ public function testRoute(string $name): void { $expected = []; foreach ($got as $role => $status) { $expected[$role] = match (true) { - in_array($role, $allowed, true) => match ($turnedAway) { - 'empty' => 'listed', - default => in_array($status, [401, 403], true) ? 'allowed' : $status, + in_array($role, $allowed, true) => match (true) { + $lists => 'listed', + default => in_array($status, $hidden ? [401, 403, 404] : [401, 403], true) ? 'allowed' : $status, }, $role === self::ANON => 401, - $turnedAway === 'empty' => 'empty', + $hidden && !in_array($role, self::VIEWERS, true) => 404, + $lists => 'empty', default => 403, }; } @@ -430,6 +437,29 @@ private function holidayLabels(): array { ); } + /** @return array */ + public static function requestRoutes(): array { + return ['request#show' => ['request#show'], 'request#addComment' => ['request#addComment'], 'attachment#index' => ['attachment#index']]; + } + + /** + * A request somebody may not see answers like one that does not exist, so + * the ids tell nothing. HR, who sees them all, gets the real answer. + */ + #[DataProvider('requestRoutes')] + public function testAnUnseenRequestAnswersLikeAnUnknownOne(string $name): void { + $unknown = $this->ids['{request}'] + 1000; + $answer = function (string $role, int $id) use ($name): array { + $response = $this->call($role, $name, ['id' => $id, 'body' => 'itest']); + return [$response->getStatus(), $response->getData()['message'] ?? null]; + }; + + self::assertSame([404, 'Request not found'], $answer(self::COLLEAGUE, $unknown)); + self::assertSame($answer(self::COLLEAGUE, $unknown), $answer(self::COLLEAGUE, $this->ids['{request}'])); + self::assertSame([404, 'Request not found'], $answer(self::HR, $unknown)); + self::assertSame(200, $this->call(self::HR, $name, ['id' => $this->ids['{request}'], 'body' => 'itest'])->getStatus()); + } + /** * One request through the dispatcher and its middleware, from a fresh * container as in a new HTTP request. diff --git a/tests/Unit/Service/AttachmentServiceTest.php b/tests/Unit/Service/AttachmentServiceTest.php index 8c7e4539..e3f1ebef 100644 --- a/tests/Unit/Service/AttachmentServiceTest.php +++ b/tests/Unit/Service/AttachmentServiceTest.php @@ -76,6 +76,7 @@ private function request(string $status = LeaveRequest::STATUS_APPROVED, bool $h $request->setTypeId(2); $request->setStatus($status); $this->requestMapper->method('find')->willReturn($request); + $this->permission->method('canView')->willReturn(true); $this->permission->method('isHrOnlyRequest')->willReturn($hrOnly); return $request; } diff --git a/tests/Unit/Service/RequestServiceTest.php b/tests/Unit/Service/RequestServiceTest.php index 7159f39b..30071ed5 100644 --- a/tests/Unit/Service/RequestServiceTest.php +++ b/tests/Unit/Service/RequestServiceTest.php @@ -945,6 +945,32 @@ public function testAConfidentialAbsenceIsWithheldFromItsOwnEmployee(): void { self::assertSame($request->getStartDate(), $rows[0]['startDate']); } + public function testWhoRecordedAConfidentialAbsenceIsWithheldFromNonHr(): void { + $request = $this->pendingOwnRequest(); + $request->setStatus(LeaveRequest::STATUS_APPROVED); + $request->setTypeId(9); + $request->setDecidedBy('hr1'); + $this->requestMapper->method('find')->willReturn($request); + $this->requestMapper->method('findFiltered')->willReturn([$request]); + $this->leaveTypeMapper->method('hrOnlyTypeIds')->willReturn([9]); + $this->permission->method('canView')->willReturn(true); + $this->permission->method('isHr')->willReturnCallback(static fn (string $uid): bool => $uid === 'hr2'); + $this->permission->method('isHrOnlyRequest')->willReturn(true); + $user = $this->createMock(IUser::class); + $user->method('getDisplayName')->willReturn('Robin Vale'); + $this->userManager->method('get')->willReturn($user); + + foreach ([ + $this->service->getDetail('emp', 5), + $this->service->listSerialized('emp', ['scope' => 'mine'], null, null)[0], + $this->service->serializeForActor('emp', $request), + ] as $data) { + self::assertNull($data['decidedBy']); + self::assertArrayNotHasKey('decidedByName', $data); + } + self::assertSame('Robin Vale', $this->service->serializeForActor('hr2', $request)['decidedByName']); + } + private function queued(int $id, string $employeeUid, string $status, string $start, string $createdAt, string $updatedAt): LeaveRequest { $request = new LeaveRequest(); $request->setId($id);