diff --git a/lib/Settings/Personal.php b/lib/Settings/Personal.php index 2e45357..1711323 100644 --- a/lib/Settings/Personal.php +++ b/lib/Settings/Personal.php @@ -10,6 +10,7 @@ use OCA\Absence\Service\ConfigService; use OCA\Absence\Service\PersonalDefaultsService; +use OCP\App\IAppManager; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Services\IInitialState; use OCP\IUserSession; @@ -21,6 +22,7 @@ public function __construct( private IInitialState $initialState, private PersonalDefaultsService $personalDefaults, private IUserSession $userSession, + private IAppManager $appManager, ) { } @@ -33,7 +35,14 @@ public function getForm(): TemplateResponse { } #[\Override] - public function getSection(): string { + public function getSection(): ?string { + // The app may be restricted to some groups, but its settings are still + // registered for everyone. Hide the form from users without access, as + // the API would reject them anyway. + $user = $this->userSession->getUser(); + if ($user === null || !$this->appManager->isEnabledForUser(ConfigService::APP_ID, $user)) { + return null; + } // Append to the built-in Availability page (/settings/user/availability) // rather than a separate Absence section. return 'availability'; diff --git a/tests/Unit/Settings/PersonalTest.php b/tests/Unit/Settings/PersonalTest.php new file mode 100644 index 0000000..c3ce3ab --- /dev/null +++ b/tests/Unit/Settings/PersonalTest.php @@ -0,0 +1,66 @@ +userSession = $this->createMock(IUserSession::class); + $this->appManager = $this->createMock(IAppManager::class); + $this->form = new Personal( + $this->createMock(IInitialState::class), + $this->createMock(PersonalDefaultsService::class), + $this->userSession, + $this->appManager, + ); + } + + private function loginUser(): IUser&MockObject { + $user = $this->createMock(IUser::class); + $this->userSession->method('getUser')->willReturn($user); + return $user; + } + + public function testSectionIsAvailabilityWhenAppIsEnabledForTheUser(): void { + $user = $this->loginUser(); + $this->appManager->expects($this->once()) + ->method('isEnabledForUser') + ->with('absence', $user) + ->willReturn(true); + + $this->assertSame('availability', $this->form->getSection()); + } + + public function testNoSectionWhenAppIsNotEnabledForTheUser(): void { + $this->loginUser(); + $this->appManager->method('isEnabledForUser')->willReturn(false); + + $this->assertNull($this->form->getSection()); + } + + public function testNoSectionWithoutAUser(): void { + $this->userSession->method('getUser')->willReturn(null); + $this->appManager->expects($this->never())->method('isEnabledForUser'); + + $this->assertNull($this->form->getSection()); + } +}