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
3 changes: 3 additions & 0 deletions SPECIFICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,9 @@ the SPA). All list endpoints paginate and accept filters.
- `PUT /api/settings`: `{ values: { key: value } }`; the HR group is admin-only.
- `GET /api/settings/employees-preview?group=`: who leaves and who joins if the employees group changes.

On the read endpoints, `from` and `to` are real `YYYY-MM-DD` dates and `year` lies
between 2000 and 2100. Anything else is a 422 with a message, before anything is read.

All write endpoints require CSRF protection (default AppFramework) and validate role
server-side. Never trust client-computed `working_days` or `manager_uid`.

Expand Down
19 changes: 14 additions & 5 deletions lib/Controller/BalanceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCA\Absence\Db\LeaveTypeMapper;
use OCA\Absence\Exception\ForbiddenException;
use OCA\Absence\Service\BalanceService;
use OCA\Absence\Service\EntitlementValidator;
use OCA\Absence\Service\PermissionService;
use OCA\Absence\Service\ReportService;
use OCP\AppFramework\Controller;
Expand All @@ -34,14 +35,20 @@ public function __construct(
private LeaveTypeMapper $leaveTypeMapper,
private IL10N $l,
private LoggerInterface $logger,
private EntitlementValidator $validator,
) {
parent::__construct($appName, $request);
}

#[NoAdminRequired]
#[UserRateLimit(limit: 60, period: 60)]
public function mine(?int $year = null): DataResponse {
return $this->handle(fn () => $this->withheldForViewer($this->balanceService->getBalance((string)$this->userId, $year)));
return $this->handle(function () use ($year) {
if ($year !== null) {
$this->validator->assertYear($year);
}
return $this->withheldForViewer($this->balanceService->getBalance((string)$this->userId, $year));
});
}

#[NoAdminRequired]
Expand All @@ -51,6 +58,9 @@ public function forEmployee(string $uid, ?int $year = null): DataResponse {
if (!$this->permission->canViewBalanceOf((string)$this->userId, $uid)) {
throw new ForbiddenException($this->l->t('Not allowed to view this balance'));
}
if ($year !== null) {
$this->validator->assertYear($year);
}
return $this->withheldForViewer($this->balanceService->getBalance($uid, $year));
});
}
Expand All @@ -65,10 +75,9 @@ public function forEmployee(string $uid, ?int $year = null): DataResponse {
#[UserRateLimit(limit: 60, period: 60)]
public function team(?int $year = null): DataResponse {
return $this->handle(function () use ($year): array {
$rows = $this->reportService->teamBalances(
(string)$this->userId,
$year ?? $this->balanceService->currentYear(),
);
$year ??= $this->balanceService->currentYear();
$this->validator->assertYear($year);
$rows = $this->reportService->teamBalances((string)$this->userId, $year);
return $this->stripHrOnlyRows($rows);
});
}
Expand Down
6 changes: 6 additions & 0 deletions lib/Controller/EntitlementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OCA\Absence\Controller;

use OCA\Absence\Service\EntitlementService;
use OCA\Absence\Service\EntitlementValidator;
use OCA\Absence\Service\PermissionService;
use OCA\Absence\Service\ProRataService;
use OCP\AppFramework\Controller;
Expand All @@ -33,6 +34,7 @@ public function __construct(
private IL10N $l,
private LoggerInterface $logger,
private IUserManager $userManager,
private EntitlementValidator $validator,
) {
parent::__construct($appName, $request);
}
Expand All @@ -42,6 +44,9 @@ public function __construct(
public function index(string $employeeUid, ?int $year = null): DataResponse {
return $this->handle(function () use ($employeeUid, $year) {
$this->permission->assertHr((string)$this->userId);
if ($year !== null) {
$this->validator->assertYear($year);
}
return array_map(static fn ($e) => $e->jsonSerialize(), $this->service->listForEmployee($employeeUid, $year));
});
}
Expand Down Expand Up @@ -133,6 +138,7 @@ public function applyDefault(float $from, float $to): DataResponse {
public function proRata(string $employeeUid, int $year): DataResponse {
return $this->handle(function () use ($employeeUid, $year) {
$this->permission->assertHr((string)$this->userId);
$this->validator->assertYear($year);
return $this->proRata->suggest($employeeUid, $year);
});
}
Expand Down
10 changes: 9 additions & 1 deletion lib/Controller/ExportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use OCA\Absence\Exception\AbsenceException;
use OCA\Absence\Service\ClockService;
use OCA\Absence\Service\EntitlementValidator;
use OCA\Absence\Service\ExportService;
use OCA\Absence\Service\PermissionService;
use OCP\AppFramework\Controller;
Expand All @@ -31,6 +32,7 @@ public function __construct(
private PermissionService $permission,
private ClockService $clock,
private IL10N $l,
private EntitlementValidator $validator,
) {
parent::__construct($appName, $request);
}
Expand All @@ -57,7 +59,13 @@ public function balances(?int $year = null, ?string $group = null): DataResponse
if (!$this->permission->isHr((string)$this->userId)) {
return new DataResponse(['message' => $this->l->t('HR role required')], Http::STATUS_FORBIDDEN);
}
$export = $this->service->balancesCsv($year ?? $this->clock->userYear(), $group);
try {
$year ??= $this->clock->userYear();
$this->validator->assertYear($year);
$export = $this->service->balancesCsv($year, $group);
} catch (AbsenceException $e) {
return new DataResponse(['message' => $e->getMessage()], $e->getHttpStatus());
}
return new DataDownloadResponse($export['content'], $export['filename'], 'text/csv');
}
}
14 changes: 11 additions & 3 deletions lib/Controller/ReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace OCA\Absence\Controller;

use OCA\Absence\Service\ClockService;
use OCA\Absence\Service\EntitlementValidator;
use OCA\Absence\Service\InsightsService;
use OCA\Absence\Service\PermissionService;
use OCA\Absence\Service\ReportService;
Expand All @@ -33,6 +34,7 @@ public function __construct(
private ClockService $clock,
private IL10N $l,
private LoggerInterface $logger,
private EntitlementValidator $validator,
) {
parent::__construct($appName, $request);
}
Expand All @@ -46,7 +48,9 @@ public function __construct(
public function insights(?int $year = null): DataResponse {
return $this->handle(function () use ($year) {
$this->permission->assertHr((string)$this->userId);
return $this->insights->getInsights($year ?? $this->clock->userYear());
$year ??= $this->clock->userYear();
$this->validator->assertYear($year);
return $this->insights->getInsights($year);
});
}

Expand All @@ -55,7 +59,9 @@ public function insights(?int $year = null): DataResponse {
public function balances(?int $year = null, ?string $group = null): DataResponse {
return $this->handle(function () use ($year, $group) {
$this->permission->assertHr((string)$this->userId);
return $this->service->balancesReport($year ?? $this->clock->userYear(), $group);
$year ??= $this->clock->userYear();
$this->validator->assertYear($year);
return $this->service->balancesReport($year, $group);
});
}

Expand All @@ -69,7 +75,9 @@ public function balances(?int $year = null, ?string $group = null): DataResponse
public function sickLeave(?int $year = null, ?string $group = null, ?int $typeId = null): DataResponse {
return $this->handle(function () use ($year, $group, $typeId) {
$this->permission->assertHr((string)$this->userId);
return $this->service->sickLeaveReport($year ?? $this->clock->userYear(), $group, $typeId);
$year ??= $this->clock->userYear();
$this->validator->assertYear($year);
return $this->service->sickLeaveReport($year, $group, $typeId);
});
}

Expand Down
4 changes: 4 additions & 0 deletions lib/Service/EmployeeListService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\Absence\Service;

use OCA\Absence\Exception\ValidationException;
use OCP\App\IAppManager;
use OCP\IGroupManager;
use OCP\IUserManager;
Expand Down Expand Up @@ -43,14 +44,17 @@ public function __construct(
private IAppManager $appManager,
private ClockService $clock,
private HolidayService $holidays,
private EntitlementValidator $validator,
) {
}

/**
* @return array{year: int, employeesGroup: string, employeesGroupName: string, contacts: bool, rows: list<array<string, mixed>>}
* @throws ValidationException
*/
public function list(?int $year): array {
$year ??= $this->clock->userYear();
$this->validator->assertYear($year);
$group = $this->config->getEmployeesGroup();
return [
'year' => $year,
Expand Down
4 changes: 2 additions & 2 deletions lib/Service/RequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,10 @@ public function list(string $actorUid, array $filters, ?int $limit, ?int $offset
$query['typeId'] = (int)$filters['typeId'];
}
if (isset($filters['from'])) {
$query['from'] = (string)$filters['from'];
$query['from'] = $this->normaliseDate((string)$filters['from']);
}
if (isset($filters['to'])) {
$query['to'] = (string)$filters['to'];
$query['to'] = $this->normaliseDate((string)$filters['to']);
}
// The type filter ignores confidential types (§5.7) for anyone but HR, like
// every other surface where they see no category.
Expand Down
42 changes: 42 additions & 0 deletions tests/Integration/AccessMatrixTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,48 @@ public function testHrOutsideTheEmployeesGroupSeesTheCompany(string $name): void
self::assertSame([$this->ids['{request}']], array_column($response->getData()['events'], 'requestId'));
}

/** @return array<string, array{string, array<string,mixed>}> */
public static function invalidReadParameters(): array {
$march = ['from' => '2027-03-01', 'to' => '2027-03-31'];
return [
'request#index from' => ['request#index', ['from' => '2027-02-30']],
'request#index to' => ['request#index', ['to' => 'tomorrow']],
'request#index year' => ['request#index', ['scope' => 'hr', 'from' => '99999-01-01']],
'coverage#index' => ['coverage#index', ['from' => '2027-13-01'] + $march],
'calendar#index' => ['calendar#index', ['to' => '2027-03-32'] + $march],
'balance#mine' => ['balance#mine', ['year' => 99999]],
'balance#team' => ['balance#team', ['year' => 1]],
'balance#forEmployee' => ['balance#forEmployee', ['uid' => '{owner}', 'year' => -5]],
'employee#index' => ['employee#index', ['year' => 99999]],
'entitlement#index' => ['entitlement#index', ['employeeUid' => '{owner}', 'year' => 1999]],
'entitlement#proRata' => ['entitlement#proRata', ['employeeUid' => '{owner}', 'year' => 2101]],
'holiday#index' => ['holiday#index', ['year' => 99999]],
'holiday#applying' => ['holiday#applying', ['code' => self::HOLIDAY_REGION, 'from' => 'x'] + $march],
'report#balances' => ['report#balances', ['year' => 99999]],
'report#sickLeave' => ['report#sickLeave', ['year' => 99999]],
'report#insights' => ['report#insights', ['year' => 99999]],
'report#trends' => ['report#trends', ['from' => '2027-02-29'] + $march],
'export#requests' => ['export#requests', ['to' => '31.03.2027'] + $march],
'export#balances' => ['export#balances', ['year' => 99999]],
];
}

/**
* Dates and years on the read routes are checked before anything is read:
* an invalid date or a year outside 2000 to 2100 is refused with a message.
*
* @param array<string,mixed> $params
*/
#[DataProvider('invalidReadParameters')]
public function testInvalidReadParametersAreRefused(string $name, array $params): void {
$params = array_map(fn (mixed $value) => is_string($value) ? ($this->ids[$value] ?? $value) : $value, $params);

$response = $this->call(self::HR, $name, $params);

self::assertSame(422, $response->getStatus());
self::assertNotSame('', $response->getData()['message'] ?? '');
}

public function testOnlyEmployeesAndHrEditHolidaysWhenEveryoneMay(): void {
Server::get(IAppConfig::class)->setValueBool(ConfigService::APP_ID, ConfigLexicon::KEY_HOLIDAYS_EVERYONE, true);
$new = ['date' => '2027-05-05', 'region' => self::HOLIDAY_REGION, 'label' => 'itest access new'];
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Controller/EntitlementControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCA\Absence\Controller\EntitlementController;
use OCA\Absence\Db\EntitlementEvent;
use OCA\Absence\Service\EntitlementService;
use OCA\Absence\Service\EntitlementValidator;
use OCA\Absence\Service\PermissionService;
use OCA\Absence\Service\ProRataService;
use OCA\Absence\Tests\Unit\L10nMockTrait;
Expand Down Expand Up @@ -46,6 +47,7 @@ public function testHistoryNamesWhoMadeEachChange(): void {
$this->l10nMock(),
$this->createMock(LoggerInterface::class),
$userManager,
$this->createMock(EntitlementValidator::class),
);

$history = $controller->history(7)->getData();
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/Controller/ExportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\Absence\Db\LeaveTypeMapper;
use OCA\Absence\Service\ClockService;
use OCA\Absence\Service\EmployeeDirectory;
use OCA\Absence\Service\EntitlementValidator;
use OCA\Absence\Service\ExportService;
use OCA\Absence\Service\PermissionService;
use OCA\Absence\Service\ReportService;
Expand Down Expand Up @@ -43,6 +44,7 @@ public function testAnInvalidRangeIsAClientError(): void {
$permission,
$this->createMock(ClockService::class),
$this->l10nMock(),
$this->createMock(EntitlementValidator::class),
);

$response = $controller->requests('2027-02-01', '2027-01-01');
Expand Down
Loading