Skip to content

Commit

Permalink
feat: add redirect to user group URL after SSO login for MiniOrange i…
Browse files Browse the repository at this point in the history
…ntegration
  • Loading branch information
thorbrink committed Jan 16, 2025
1 parent b93e443 commit ddc4ade
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
3 changes: 3 additions & 0 deletions library/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
43 changes: 43 additions & 0 deletions library/UserGroup/RedirectToUserGroupUrlAfterSsoLogin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Municipio\UserGroup;

use Municipio\Helper\User\Contracts\GetUserGroupUrl;
use Municipio\HooksRegistrar\Hookable;
use WpService\Contracts\AddFilter;

/**
* Redirect to user group URL after SSO login.
*/
class RedirectToUserGroupUrlAfterSsoLogin implements Hookable
{
/**
* Constructor.
*/
public function __construct(private GetUserGroupUrl $userHelper, private AddFilter $wpService)
{
}

/**
* @inheritDoc
*/
public function addHooks(): void
{
$this->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();
}
}
59 changes: 59 additions & 0 deletions library/UserGroup/RedirectToUserGroupUrlAfterSsoLogin.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Municipio\UserGroup;

use Municipio\Helper\User\Contracts\GetUserGroupUrl;
use PHPUnit\Framework\TestCase;
use WpService\Implementations\FakeWpService;

class RedirectToUserGroupUrlAfterSsoLoginTest extends TestCase
{
/**
* @testdox class can be instantiated
*/
public function testCanBeInstantiated()
{
$userHelper = $this->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'));
}
}

0 comments on commit ddc4ade

Please sign in to comment.