From 33fce7e20f3b575769f7ff33c7037771d384c6eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= <14975046+skjnldsv@users.noreply.github.com> Date: Thu, 3 Dec 2026 12:29:29 +0100 Subject: [PATCH 1/3] fix(notifications): only tell people who have the app MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With the app enabled for some groups only, while it is introduced team by team, people outside them still got its notifications, emails and activity entries, all leading to a page they cannot open: an employee's manager or replacement, HR members, comment recipients. Every notification, email and activity entry now goes only to an enabled account the app is enabled for. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: John Molakvoæ <14975046+skjnldsv@users.noreply.github.com> --- SPECIFICATION.md | 5 ++ lib/Service/ActivityPublisher.php | 11 ++- lib/Service/NotificationService.php | 32 ++++++--- .../AppRestrictedRecipientsTest.php | 70 +++++++++++++++++++ tests/Integration/CommentMailTest.php | 3 +- tests/Unit/Service/ActivityPublisherTest.php | 54 ++++++++++++++ .../Unit/Service/NotificationServiceTest.php | 25 ++++++- 7 files changed, 188 insertions(+), 12 deletions(-) create mode 100644 tests/Integration/AppRestrictedRecipientsTest.php create mode 100644 tests/Unit/Service/ActivityPublisherTest.php diff --git a/SPECIFICATION.md b/SPECIFICATION.md index 3e80c07..02932d2 100644 --- a/SPECIFICATION.md +++ b/SPECIFICATION.md @@ -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 diff --git a/lib/Service/ActivityPublisher.php b/lib/Service/ActivityPublisher.php index e5da9ec..e9e5bdf 100644 --- a/lib/Service/ActivityPublisher.php +++ b/lib/Service/ActivityPublisher.php @@ -10,6 +10,8 @@ use OCA\Absence\Db\LeaveRequest; use OCP\Activity\IManager; +use OCP\App\IAppManager; +use OCP\IUserManager; use Psr\Log\LoggerInterface; /** @@ -30,6 +32,8 @@ class ActivityPublisher { public function __construct( private IManager $activityManager, private LoggerInterface $logger, + private IUserManager $userManager, + private IAppManager $appManager, ) { } @@ -38,7 +42,12 @@ public function __construct( * @param array $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; } diff --git a/lib/Service/NotificationService.php b/lib/Service/NotificationService.php index e0d74be..563793f 100644 --- a/lib/Service/NotificationService.php +++ b/lib/Service/NotificationService.php @@ -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; @@ -63,6 +64,7 @@ public function __construct( private CommentMailMapper $commentMails, private PermissionService $permission, private ConfigService $config, + private IAppManager $appManager, ) { } @@ -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); @@ -157,7 +158,7 @@ public function notifyComment(LeaveRequest $request, #[\SensitiveParameter] Requ * @param list $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 => [ @@ -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 @@ -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)) { @@ -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) @@ -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) @@ -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 { diff --git a/tests/Integration/AppRestrictedRecipientsTest.php b/tests/Integration/AppRestrictedRecipientsTest.php new file mode 100644 index 0000000..455317f --- /dev/null +++ b/tests/Integration/AppRestrictedRecipientsTest.php @@ -0,0 +1,70 @@ +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); + } +} diff --git a/tests/Integration/CommentMailTest.php b/tests/Integration/CommentMailTest.php index 1584790..954813f 100644 --- a/tests/Integration/CommentMailTest.php +++ b/tests/Integration/CommentMailTest.php @@ -25,6 +25,7 @@ use OCP\AppFramework\Utility\ITimeFactory; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IAppConfig; +use OCP\App\IAppManager; use OCP\IDBConnection; use OCP\IGroupManager; use OCP\IURLGenerator; @@ -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 { diff --git a/tests/Unit/Service/ActivityPublisherTest.php b/tests/Unit/Service/ActivityPublisherTest.php new file mode 100644 index 0000000..da22d68 --- /dev/null +++ b/tests/Unit/Service/ActivityPublisherTest.php @@ -0,0 +1,54 @@ +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); + } +} diff --git a/tests/Unit/Service/NotificationServiceTest.php b/tests/Unit/Service/NotificationServiceTest.php index 95e6a1c..e385cd4 100644 --- a/tests/Unit/Service/NotificationServiceTest.php +++ b/tests/Unit/Service/NotificationServiceTest.php @@ -18,6 +18,7 @@ use OCA\Absence\Tests\Unit\TraceArgumentsTrait; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\App\IAppManager; use OCP\IUser; use OCP\IUserManager; use OCP\L10N\IFactory; @@ -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); @@ -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( @@ -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)); @@ -266,6 +276,7 @@ private function serviceWith(INotificationManager $manager): NotificationService $this->createMock(CommentMailMapper::class), $this->createMock(PermissionService::class), $this->createMock(ConfigService::class), + $this->appManager, ); } @@ -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(); From 608d6129423311ac280663b8fde1f697eedb5d5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= <14975046+skjnldsv@users.noreply.github.com> Date: Thu, 3 Dec 2026 14:18:45 +0100 Subject: [PATCH 2/3] style: php-cs-fixer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: John Molakvoæ <14975046+skjnldsv@users.noreply.github.com> --- tests/Integration/CommentMailTest.php | 2 +- tests/Unit/Service/NotificationServiceTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Integration/CommentMailTest.php b/tests/Integration/CommentMailTest.php index 954813f..f096192 100644 --- a/tests/Integration/CommentMailTest.php +++ b/tests/Integration/CommentMailTest.php @@ -22,10 +22,10 @@ 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; -use OCP\App\IAppManager; use OCP\IDBConnection; use OCP\IGroupManager; use OCP\IURLGenerator; diff --git a/tests/Unit/Service/NotificationServiceTest.php b/tests/Unit/Service/NotificationServiceTest.php index e385cd4..ebbf624 100644 --- a/tests/Unit/Service/NotificationServiceTest.php +++ b/tests/Unit/Service/NotificationServiceTest.php @@ -16,9 +16,9 @@ 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\App\IAppManager; use OCP\IUser; use OCP\IUserManager; use OCP\L10N\IFactory; From 80ac32adaf2dd224388b694663e83897c8393cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= <14975046+skjnldsv@users.noreply.github.com> Date: Thu, 3 Dec 2026 14:19:11 +0100 Subject: [PATCH 3/3] test: the manager change takes no HR list any more MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: John Molakvoæ <14975046+skjnldsv@users.noreply.github.com> --- tests/Unit/Service/NotificationServiceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Service/NotificationServiceTest.php b/tests/Unit/Service/NotificationServiceTest.php index ebbf624..320fe25 100644 --- a/tests/Unit/Service/NotificationServiceTest.php +++ b/tests/Unit/Service/NotificationServiceTest.php @@ -393,7 +393,7 @@ public function testSomebodyWithoutTheAppIsNeitherNotifiedNorMailed(): void { $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', []); + $this->service->notifyManagerChanged('carol-noapp', [], ['boss'], 'hr'); self::assertSame([], $this->pushed); }