Skip to content

Commit

Permalink
refactor: move user group redirect login getter to user helper
Browse files Browse the repository at this point in the history
  • Loading branch information
thorbrink committed Jan 17, 2025
1 parent e3e1db0 commit c34dcfe
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 27 deletions.
16 changes: 5 additions & 11 deletions library/Admin/Login/RedirectUserToGroupUrlIfIsPreferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@ public function redirectToGroupUrl($redirectTo, $request, $userInHook)
return $redirectTo;
}

$user = $this->userHelper->getUser($userInHook);
if ($user != null) {
$perfersGroupUrl = $this->userHelper->getUserPrefersGroupUrl($user);
$groupUrl = $this->userHelper->getUserGroupUrl(null, $user);

if ($perfersGroupUrl && $groupUrl) {
return $this->wpService->addQueryArg([
'loggedin' => 'true',
'prefersgroup' => 'true'
], $groupUrl);
}
$userGroupRedirectUrl = $this->userHelper->getRedirectToGroupUrl($userInHook);

if ($userGroupRedirectUrl != null) {
return $userGroupRedirectUrl;
}

return $redirectTo;
}
}
17 changes: 17 additions & 0 deletions library/Helper/User/Contracts/GetRedirectToGroupUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Municipio\Helper\User\Contracts;

interface GetRedirectToGroupUrl
{
/**
* Get redirect to group url
*
* @param string $redirectTo
* @param string $request
* @param WP_User $userInHook
*
* @return string
*/
public function getRedirectToGroupUrl(null|\WP_User|int $user = null): ?string;
}
39 changes: 35 additions & 4 deletions library/Helper/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Municipio\Helper\User;

use WpService\Contracts\{GetBlogDetails, GetUserMeta, IsWpError, WpGetObjectTerms, WpGetCurrentUser, GetUserBy};
use WpService\Contracts\{AddQueryArg, GetBlogDetails, GetUserMeta, IsWpError, WpGetObjectTerms, WpGetCurrentUser, GetUserBy};
use AcfService\Contracts\GetField;
use Municipio\Helper\User\Config\UserConfigInterface;
use Municipio\Helper\User\Contracts\{UserHasRole, GetUserGroup, GetUserGroupUrl, GetUserGroupUrlType, GetUserPrefersGroupUrl, GetUser};
use Municipio\Helper\User\Contracts\{GetRedirectToGroupUrl, UserHasRole, GetUserGroup, GetUserGroupUrl, GetUserGroupUrlType, GetUserPrefersGroupUrl, GetUser};
use Municipio\Helper\User\FieldResolver\UserGroupUrl;
use Municipio\UserGroup\Config\UserGroupConfigInterface;
use WP_Term;
Expand All @@ -14,13 +14,20 @@
/**
* User helper.
*/
class User implements UserHasRole, GetUserGroup, GetUserGroupUrl, GetUserGroupUrlType, GetUserPrefersGroupUrl, GetUser
class User implements
UserHasRole,
GetUserGroup,
GetUserGroupUrl,
GetUserGroupUrlType,
GetUserPrefersGroupUrl,
GetUser,
GetRedirectToGroupUrl
{
/**
* Constructor.
*/
public function __construct(
private WpGetCurrentUser&WpGetObjectTerms&IsWpError&GetUserMeta&GetBlogDetails&GetUserBy $wpService,
private WpGetCurrentUser&WpGetObjectTerms&IsWpError&GetUserMeta&GetBlogDetails&GetUserBy&AddQueryArg $wpService,
private GetField $acfService,
private UserConfigInterface $userConfig,
private UserGroupConfigInterface $userGroupConfig
Expand Down Expand Up @@ -160,4 +167,28 @@ public function getUserPrefersGroupUrl(null|WP_User|int $user = null): ?bool
}
return false;
}

/**
* @inheritDoc
*/
public function getRedirectToGroupUrl(null|WP_User|int $user = null): ?string
{
$user = $this->getUser($user);

if (!$user) {
return null;
}

$perfersGroupUrl = $this->getUserPrefersGroupUrl($user);
$groupUrl = $this->getUserGroupUrl(null, $user);

if ($perfersGroupUrl && $groupUrl) {
return $this->wpService->addQueryArg([
'loggedin' => 'true',
'prefersgroup' => 'true'
], $groupUrl);
}

return null;
}
}
13 changes: 7 additions & 6 deletions library/UserGroup/RedirectToUserGroupUrlAfterSsoLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Municipio\Helper\User\Contracts\GetUserGroupUrl;
use Municipio\HooksRegistrar\Hookable;
use WpService\Contracts\AddFilter;
use Municipio\Admin\Login\RedirectUserToGroupUrlIfIsPreferred;
use Municipio\Helper\User\Contracts\GetRedirectToGroupUrl;

/**
* Redirect to user group URL after SSO login.
Expand All @@ -15,7 +15,7 @@ class RedirectToUserGroupUrlAfterSsoLogin implements Hookable
/**
* Constructor.
*/
public function __construct(private GetUserGroupUrl $userHelper, private AddFilter $wpService)
public function __construct(private GetUserGroupUrl&GetRedirectToGroupUrl $userHelper, private AddFilter $wpService)
{
}

Expand All @@ -35,10 +35,11 @@ public function addHooks(): void
*/
public function getRedirectUrl(string $redirectTo, int|null $userId = null): string
{
$redirectTo = (new RedirectUserToGroupUrlIfIsPreferred(
$this->wpService,
$this->userHelper
))->redirectToGroupUrl($redirectTo, '', $userId);
$userGroupRedirectUrl = $this->userHelper->getRedirectToGroupUrl($userId);

if ($userGroupRedirectUrl != null) {
return $userGroupRedirectUrl;
}

return $redirectTo;
}
Expand Down
13 changes: 7 additions & 6 deletions library/UserGroup/RedirectToUserGroupUrlAfterSsoLogin.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Municipio\UserGroup;

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

Expand All @@ -13,7 +14,7 @@ class RedirectToUserGroupUrlAfterSsoLoginTest extends TestCase
*/
public function testCanBeInstantiated()
{
$userHelper = $this->createMock(GetUserGroupUrl::class);
$userHelper = $this->createMock(User::class);
$sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, new FakeWpService());
$this->assertInstanceOf(RedirectToUserGroupUrlAfterSsoLogin::class, $sut);
}
Expand All @@ -23,7 +24,7 @@ public function testCanBeInstantiated()
*/
public function testAddHooksAddsFilterToRedirectUrl()
{
$userHelper = $this->createMock(GetUserGroupUrl::class);
$userHelper = $this->createMock(User::class);
$wpService = new FakeWpService(['addFilter' => true]);
$sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, $wpService);

Expand All @@ -40,8 +41,8 @@ public function testAddHooksAddsFilterToRedirectUrl()
*/
public function testGetRedirectUrlReturnsUrlFromGetUserGroupUrl()
{
$userHelper = $this->createMock(GetUserGroupUrl::class);
$userHelper->method('getUserGroupUrl')->willReturn('http://example.com');
$userHelper = $this->createMock(User::class);
$userHelper->method('getRedirectToGroupUrl')->willReturn('http://example.com');
$sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, new FakeWpService());
$this->assertEquals('http://example.com', $sut->getRedirectUrl('http://example.org'));
}
Expand All @@ -51,8 +52,8 @@ public function testGetRedirectUrlReturnsUrlFromGetUserGroupUrl()
*/
public function testGetRedirectUrlReturnsOriginalUrlIfGetUserGroupUrlReturnsNull()
{
$userHelper = $this->createMock(GetUserGroupUrl::class);
$userHelper->method('getUserGroupUrl')->willReturn(null);
$userHelper = $this->createMock(User::class);
$userHelper->method('getRedirectToGroupUrl')->willReturn(null);
$sut = new RedirectToUserGroupUrlAfterSsoLogin($userHelper, new FakeWpService());
$this->assertEquals('http://example.org', $sut->getRedirectUrl('http://example.org'));
}
Expand Down

0 comments on commit c34dcfe

Please sign in to comment.