Skip to content
Open
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
5 changes: 5 additions & 0 deletions SPECIFICATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,11 @@ refused.

All four channels are required.

**Who can be told anything:** an existing, enabled account for which the app is
enabled. An admin can restrict the app to some groups, for example while it is
introduced team by team; nobody outside them gets a notification, an email or an
activity entry from it, as all of them lead to a page they cannot open.

- **Nextcloud notifications:** implement `OCP\Notification\INotifier`. Events:
new request (→ manager), decision made (→ employee), escalation (→ HR),
reminder (→ manager), withdrawal request (→ the manager who can decide it and is not away today, else
Expand Down
11 changes: 10 additions & 1 deletion lib/Service/ActivityPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

use OCA\Absence\Db\LeaveRequest;
use OCP\Activity\IManager;
use OCP\App\IAppManager;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;

/**
Expand All @@ -30,6 +32,8 @@ class ActivityPublisher {
public function __construct(
private IManager $activityManager,
private LoggerInterface $logger,
private IUserManager $userManager,
private IAppManager $appManager,
) {
}

Expand All @@ -38,7 +42,12 @@ public function __construct(
* @param array<string,mixed> $params
*/
public function publish(string $subject, #[\SensitiveParameter] array $params, array $affectedUsers, ?LeaveRequest $request = null): void {
$affectedUsers = array_values(array_unique(array_filter($affectedUsers)));
// Only people with the app: its entries, and the activity emails made from
// them, would lead others to a page they cannot open.
$affectedUsers = array_values(array_filter(
array_unique(array_filter($affectedUsers)),
fn (string $uid): bool => ($user = $this->userManager->get($uid)) !== null && $this->appManager->isEnabledForUser(ConfigService::APP_ID, $user),
));
if ($affectedUsers === []) {
return;
}
Expand Down
32 changes: 23 additions & 9 deletions lib/Service/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use OCA\Absence\Db\CommentMailMapper;
use OCA\Absence\Db\LeaveRequest;
use OCA\Absence\Db\RequestComment;
use OCP\App\IAppManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
Expand Down Expand Up @@ -63,6 +64,7 @@ public function __construct(
private CommentMailMapper $commentMails,
private PermissionService $permission,
private ConfigService $config,
private IAppManager $appManager,
) {
}

Expand Down Expand Up @@ -137,8 +139,7 @@ public function notifyComment(LeaveRequest $request, #[\SensitiveParameter] Requ
$authorUid = $comment->getAuthorUid();
$queued = [];
foreach (array_unique(array_filter($recipientUids)) as $uid) {
// A disabled account cannot act on any of this.
if ($uid === $authorUid || $this->userManager->get($uid)?->isEnabled() === false) {
if ($uid === $authorUid || !$this->canReceive($uid)) {
continue;
}
$this->sendNotification($uid, self::SUBJECT_COMMENT, $request, false, trim($comment->getBody()), $authorUid, null);
Expand All @@ -157,7 +158,7 @@ public function notifyComment(LeaveRequest $request, #[\SensitiveParameter] Requ
* @param list<RequestComment> $comments oldest first
*/
public function sendCommentMail(LeaveRequest $request, string $recipientUid, #[\SensitiveParameter] array $comments): void {
if ($comments === [] || $this->userManager->get($recipientUid)?->isEnabled() === false) {
if ($comments === [] || !$this->canReceive($recipientUid)) {
return;
}
$notes = array_map(static fn (RequestComment $comment): array => [
Expand Down Expand Up @@ -193,6 +194,18 @@ public function notifyAbsenceRecorded(LeaveRequest $request, string $managerUid)
$this->send($managerUid, self::SUBJECT_ABSENCE_RECORDED, $request, false);
}

/**
* Whether somebody can be told anything by this app: an existing, enabled
* account for which the app is enabled. An admin can restrict the app to
* some groups, for example while it is introduced team by team; everyone
* else must not get its notifications or emails, which lead to a page they
* cannot open.
*/
private function canReceive(string $uid): bool {
$user = $this->userManager->get($uid);
return $user !== null && $user->isEnabled() && $this->appManager->isEnabledForUser(ConfigService::APP_ID, $user);
}

/**
* @param ?string $note free text written by a person that the recipient would
* otherwise only find by opening the request: the
Expand All @@ -208,8 +221,7 @@ public function notifyAbsenceRecorded(LeaveRequest $request, string $managerUid)
* On a confidential request (§5.7) only HR gets $note and its author, as in the app.
*/
private function send(string $recipientUid, string $subject, LeaveRequest $request, bool $actionable, #[\SensitiveParameter] ?string $note = null, ?string $noteAuthorUid = null, ?array $notice = null): void {
// A disabled account cannot act on any of this.
if ($this->userManager->get($recipientUid)?->isEnabled() === false) {
if (!$this->canReceive($recipientUid)) {
return;
}
if ($this->permission->isHrOnlyRequest($request) && !$this->permission->isHr($recipientUid)) {
Expand Down Expand Up @@ -401,6 +413,9 @@ private function displayName(\OCP\IL10N $l, string $uid): string {
* one of its regions).
*/
public function notifyMissingRegion(string $uid, string $code = '', ?string $example = null): void {
if (!$this->canReceive($uid)) {
return;
}
try {
$notification = $this->notificationManager->createNotification();
$notification->setApp(ConfigService::APP_ID)
Expand Down Expand Up @@ -436,11 +451,10 @@ public function dismissMissingRegion(string $uid): void {
* @param string $expiryDate 'YYYY-MM-DD'
*/
public function notifyCarryOverExpiring(string $uid, float $days, string $expiryDate): void {
$user = $this->userManager->get($uid);
// A disabled account cannot act on any of this.
if ($user?->isEnabled() === false) {
if (!$this->canReceive($uid)) {
return;
}
$user = $this->userManager->get($uid);
try {
$notification = $this->notificationManager->createNotification();
$notification->setApp(ConfigService::APP_ID)
Expand Down Expand Up @@ -494,7 +508,7 @@ public function notifyCarryOverExpiring(string $uid, float $days, string $expiry
*/
public function notifyManagerChanged(string $employeeUid, array $before, array $after, string $actorUid): void {
foreach (array_unique([$employeeUid, ...$after, ...$before]) as $uid) {
if ($uid === $actorUid || $this->userManager->get($uid)?->isEnabled() !== true) {
if ($uid === $actorUid || !$this->canReceive($uid)) {
continue;
}
try {
Expand Down
70 changes: 70 additions & 0 deletions tests/Integration/AppRestrictedRecipientsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Absence\Tests\Integration;

use OCA\Absence\Db\LeaveRequest;
use OCA\Absence\Service\ConfigService;
use OCA\Absence\Service\NotificationService;
use OCA\Absence\Tests\Integration\Fixtures\RecordingNotifications;
use OCP\App\IAppManager;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\Notification\INotification;
use OCP\Server;
use PHPUnit\Framework\TestCase;

/**
* With the app enabled for some groups only, as while it is introduced team by
* team, nobody outside them hears from it. The activity stream is filtered the
* same way ({@see \OCA\Absence\Tests\Unit\Service\ActivityPublisherTest}).
*/
class AppRestrictedRecipientsTest extends TestCase {
private const GROUP = 'itest-restricted-pilot';
private const INSIDE = 'itest-restricted-in';
private const OUTSIDE = 'itest-restricted-out';

protected function setUp(): void {
parent::setUp();
$users = Server::get(IUserManager::class);
$groups = Server::get(IGroupManager::class);
$group = $groups->get(self::GROUP) ?? $groups->createGroup(self::GROUP);
foreach ([self::INSIDE, self::OUTSIDE] as $uid) {
$users->get($uid) ?? $users->createUser($uid, 'Itest-Password-restricted-1');
}
$group->addUser($users->get(self::INSIDE));
Server::get(IAppManager::class)->enableAppForGroups(ConfigService::APP_ID, [$group]);
}

protected function tearDown(): void {
Server::get(IAppManager::class)->enableApp(ConfigService::APP_ID);
foreach ([self::INSIDE, self::OUTSIDE] as $uid) {
Server::get(IUserManager::class)->get($uid)?->delete();
}
Server::get(IGroupManager::class)->get(self::GROUP)?->delete();
parent::tearDown();
}

public function testOnlyPeopleWithTheAppAreNotified(): void {
RecordingNotifications::register();
$request = new LeaveRequest();
$request->setId(4711);
$request->setEmployeeUid(self::INSIDE);
$request->setTypeId(1);
$request->setStartDate('2027-03-01');
$request->setEndDate('2027-03-01');

$notifications = Server::get(NotificationService::class);
$notifications->notifyNewRequest($request, self::OUTSIDE);
$notifications->notifyNewRequest($request, self::INSIDE);

$users = array_map(static fn (INotification $n): string => $n->getUser(), RecordingNotifications::$sent);
self::assertSame([self::INSIDE], $users);
}
}
3 changes: 2 additions & 1 deletion tests/Integration/CommentMailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use OCA\Absence\Service\NotificationService;
use OCA\Absence\Service\PermissionService;
use OCA\Absence\Tests\Integration\Fixtures\RecordingNotifications;
use OCP\App\IAppManager;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IAppConfig;
Expand Down Expand Up @@ -95,7 +96,7 @@ protected function setUp(): void {
});
$this->notifications = new NotificationService(Server::get(INotificationManager::class), $mailer, $users,
Server::get(IURLGenerator::class), Server::get(IFactory::class), Server::get(NoticeService::class),
Server::get(LoggerInterface::class), Server::get(CommentMailMapper::class), Server::get(PermissionService::class), Server::get(ConfigService::class));
Server::get(LoggerInterface::class), Server::get(CommentMailMapper::class), Server::get(PermissionService::class), Server::get(ConfigService::class), Server::get(IAppManager::class));
}

protected function tearDown(): void {
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/Service/ActivityPublisherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Absence\Tests\Unit\Service;

use OCA\Absence\Service\ActivityPublisher;
use OCP\Activity\IEvent;
use OCP\Activity\IManager;
use OCP\App\IAppManager;
use OCP\IUser;
use OCP\IUserManager;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;

class ActivityPublisherTest extends TestCase {
public function testOnlyPeopleWithTheAppGetAnEntry(): void {
$affected = [];
$event = $this->createMock(IEvent::class);
$event->method('setApp')->willReturnSelf();
$event->method('setType')->willReturnSelf();
$event->method('setAuthor')->willReturnSelf();
$event->method('setSubject')->willReturnSelf();
$event->method('setObject')->willReturnSelf();
$event->method('setAffectedUser')->willReturnCallback(function (string $uid) use (&$affected, $event): IEvent {
$affected[] = $uid;
return $event;
});
$activity = $this->createMock(IManager::class);
$activity->method('generateEvent')->willReturn($event);

$users = $this->createMock(IUserManager::class);
$users->method('get')->willReturnCallback(function (string $uid): ?IUser {
if ($uid === 'gone') {
return null;
}
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn($uid);
return $user;
});
$apps = $this->createMock(IAppManager::class);
$apps->method('isEnabledForUser')->willReturnCallback(static fn (string $app, IUser $user): bool => $user->getUID() !== 'outside');

(new ActivityPublisher($activity, $this->createMock(LoggerInterface::class), $users, $apps))
->publish(ActivityPublisher::SUBJECT_CREATED, [], ['inside', 'outside', 'gone', 'inside']);

self::assertSame(['inside'], $affected);
}
}
25 changes: 24 additions & 1 deletion tests/Unit/Service/NotificationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCA\Absence\Service\NotificationService;
use OCA\Absence\Service\PermissionService;
use OCA\Absence\Tests\Unit\TraceArgumentsTrait;
use OCP\App\IAppManager;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
Expand Down Expand Up @@ -66,12 +67,18 @@ class NotificationServiceTest extends TestCase {
private array $emailButtons = [];
/** The setting that lets emails quote what people wrote. */
private bool $quoteText = true;
private IAppManager&MockObject $appManager;

protected function setUp(): void {
parent::setUp();
$this->notificationManager = $this->createMock(INotificationManager::class);
$this->mailer = $this->createMock(IMailer::class);
$this->userManager = $this->createMock(IUserManager::class);
// Everyone has the app but the accounts whose uid ends in `-noapp`
$this->appManager = $this->createMock(IAppManager::class);
$this->appManager->method('isEnabledForUser')->willReturnCallback(
static fn (string $app, ?IUser $user): bool => $user !== null && !str_ends_with($user->getUID(), '-noapp'),
);
$urlGenerator = $this->createMock(IURLGenerator::class);
$urlGenerator->method('linkToRouteAbsolute')->willReturn('https://cloud.example.com/apps/absence/');
$l10nFactory = $this->createMock(IFactory::class);
Expand Down Expand Up @@ -164,12 +171,14 @@ function (RequestComment $comment, array $recipientUids): void {
$commentMails,
$permission,
$config,
$this->appManager,
);
}

/**
* Everyone named here exists, has mail configured and has a display name.
* A uid ending in `-disabled` is a disabled account.
* A uid ending in `-disabled` is a disabled account, one ending in `-noapp`
* an account the app is not enabled for.
*/
private function withUsers(string ...$uids): void {
$this->userManager->method('get')->willReturnCallback(
Expand All @@ -178,6 +187,7 @@ function (string $uid) use ($uids): ?IUser {
return null;
}
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn($uid);
$user->method('isEnabled')->willReturn(!str_ends_with($uid, '-disabled'));
$user->method('getEMailAddress')->willReturn($uid . '@example.com');
$user->method('getDisplayName')->willReturn(ucfirst($uid));
Expand Down Expand Up @@ -266,6 +276,7 @@ private function serviceWith(INotificationManager $manager): NotificationService
$this->createMock(CommentMailMapper::class),
$this->createMock(PermissionService::class),
$this->createMock(ConfigService::class),
$this->appManager,
);
}

Expand Down Expand Up @@ -375,6 +386,18 @@ public function testADisabledAccountIsNeitherNotifiedNorMailed(): void {
self::assertSame([], $this->pushed);
}

public function testSomebodyWithoutTheAppIsNeitherNotifiedNorMailed(): void {
$this->withUsers('emp', 'boss-noapp', 'robin-noapp', 'carol-noapp');
$this->mailer->expects(self::never())->method('send');

$this->service->notifyNewRequest($this->request(), 'boss-noapp');
$this->service->notifyCarryOverExpiring('robin-noapp', 3.0, '2032-03-31');
$this->service->notifyMissingRegion('carol-noapp');
$this->service->notifyManagerChanged('carol-noapp', [], ['boss'], 'hr');

self::assertSame([], $this->pushed);
}

public function testTheDecisionCommentTravelsWithTheOutcome(): void {
$this->withUsers('emp', 'boss');
$request = $this->request();
Expand Down
Loading