diff --git a/library/App.php b/library/App.php index 6ac753ae9..eb98a9933 100644 --- a/library/App.php +++ b/library/App.php @@ -454,6 +454,9 @@ private function setupUserGroupFeature(): void // Restrict private posts to user group (new \Municipio\UserGroup\RestrictPrivatePostToUserGroup($this->wpService, $userHelper, $userGroupRestrictionConfig))->addHooks(); + + // Redirect to user group url after SSO login if using MiniOrange plugin for SSO login + (new \Municipio\UserGroup\RedirectToUserGroupUrlAfterSsoLogin($userHelper, $this->wpService))->addHooks(); } /** diff --git a/library/UserGroup/RedirectToUserGroupUrlAfterSsoLogin.php b/library/UserGroup/RedirectToUserGroupUrlAfterSsoLogin.php new file mode 100644 index 000000000..835ba906c --- /dev/null +++ b/library/UserGroup/RedirectToUserGroupUrlAfterSsoLogin.php @@ -0,0 +1,43 @@ +wpService->addFilter(\Municipio\Integrations\MiniOrange\AllowRedirectAfterSsoLogin::REDIRECT_URL_FILTER_HOOK, [$this, 'getRedirectUrl'], 10, 1); + } + + /** + * Get redirect URL. + * + * @param string $url + * @return string + */ + public function getRedirectUrl(string $url): string + { + if (!$this->userHelper->getUserGroupUrl()) { + return $url; + } + + return $this->userHelper->getUserGroupUrl(); + } +} diff --git a/library/UserGroup/RedirectToUserGroupUrlAfterSsoLogin.test.php b/library/UserGroup/RedirectToUserGroupUrlAfterSsoLogin.test.php new file mode 100644 index 000000000..24d315f26 --- /dev/null +++ b/library/UserGroup/RedirectToUserGroupUrlAfterSsoLogin.test.php @@ -0,0 +1,59 @@ +createMock(GetUserGroupUrl::class); + $sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, new FakeWpService()); + $this->assertInstanceOf(RedirectToUserGroupUrlAfterSsoLogin::class, $sut); + } + + /** + * @testdox addHooks() adds a filter to the redirect URL + */ + public function testAddHooksAddsFilterToRedirectUrl() + { + $userHelper = $this->createMock(GetUserGroupUrl::class); + $wpService = new FakeWpService(['addFilter' => true]); + $sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, $wpService); + + $sut->addHooks(); + + $this->assertEquals( + \Municipio\Integrations\MiniOrange\AllowRedirectAfterSsoLogin::REDIRECT_URL_FILTER_HOOK, + $wpService->methodCalls['addFilter'][0][0] + ); + } + + /** + * @testdox getRedirectUrl() returns the URL from GetUserGroupUrl + */ + public function testGetRedirectUrlReturnsUrlFromGetUserGroupUrl() + { + $userHelper = $this->createMock(GetUserGroupUrl::class); + $userHelper->method('getUserGroupUrl')->willReturn('http://example.com'); + $sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, new FakeWpService()); + $this->assertEquals('http://example.com', $sut->getRedirectUrl('http://example.org')); + } + + /** + * @testdox getRedirectUrl() returns the original URL if GetUserGroupUrl returns null + */ + public function testGetRedirectUrlReturnsOriginalUrlIfGetUserGroupUrlReturnsNull() + { + $userHelper = $this->createMock(GetUserGroupUrl::class); + $userHelper->method('getUserGroupUrl')->willReturn(null); + $sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, new FakeWpService()); + $this->assertEquals('http://example.org', $sut->getRedirectUrl('http://example.org')); + } +}