Skip to content

Commit

Permalink
SSO: Use client specific user roles (#515)
Browse files Browse the repository at this point in the history
* SSO: Use client specific user roles

* Reorder arguments to oauth authenticator

* Update oauth authenticator unit tests

* Update php-cs-fixer conf

Do not check for comma after last method/function argument
  • Loading branch information
chetan-thapliyal authored Sep 12, 2024
1 parent 62e4a0e commit 5e564dd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .php-cs-fixer.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ return $config
],
],
'single_line_throw' => false,
'global_namespace_import' => true
'global_namespace_import' => true,
'trailing_comma_in_multiline' => false
])
->setUsingCache(false)
->setFinder($finder);
5 changes: 5 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ services:
$logoutUrl: '%app.idp.endpoint.logout%'
$baseUri: '%app.base_url%'

mealz.oauthuserprovider: '@App\Mealz\UserBundle\Provider\OAuthUserProvider'
App\Mealz\UserBundle\Provider\OAuthUserProvider:
arguments:
$authClientID: '%app.idp.client_id%'

App\Mealz\UserBundle\Repository\ProfileRepository:
arguments:
$entityClass: App\Mealz\UserBundle\Entity\Profile
Expand Down
18 changes: 9 additions & 9 deletions src/Mealz/UserBundle/Provider/OAuthUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ class OAuthUserProvider implements UserProviderInterface, OAuthAwareUserProvider
'aoe_employee' => self::ROLE_USER,
];

private EntityManagerInterface $entityManager;
private RoleRepositoryInterface $roleRepo;

public function __construct(EntityManagerInterface $entityManager, RoleRepositoryInterface $roleRepo)
{
$this->entityManager = $entityManager;
$this->roleRepo = $roleRepo;
public function __construct(
private readonly EntityManagerInterface $entityManager,
private readonly RoleRepositoryInterface $roleRepo,
private readonly string $authClientID
) {
}

public function loadUserByIdentifier(string $identifier): UserInterface
Expand All @@ -69,8 +67,10 @@ public function loadUserByOAuthUserResponse(UserResponseInterface $response): Us
$lastName = $response->getLastName() ?? '';
$email = $response->getEmail();

$idpUserRoles = $response->getData()['roles'] ?? [];
$role = $this->toMealsRole($idpUserRoles);
$data = $response->getData();
$globalUserRoles = $data['roles'] ?? [];
$appUserRoles = $data['resource_access'][$this->authClientID]['roles'] ?? [];
$role = $this->toMealsRole(array_merge($globalUserRoles, $appUserRoles));
$roles = (null === $role) ? [] : [$role];

try {
Expand Down
4 changes: 1 addition & 3 deletions src/Mealz/UserBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
services:
mealz.oauthuserprovider:
alias: App\Mealz\UserBundle\Provider\OAuthUserProvider

# Deprecated, do not define any services here. Use services.yaml in root level config directory instead.
11 changes: 9 additions & 2 deletions src/Mealz/UserBundle/Tests/Service/OAuthProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class OAuthProviderTest extends AbstractControllerTestCase
{
use ProphecyTrait;

private const string AUTH_CLIENT_ID = 'meals-app';

private OAuthUserProvider $sut;

protected function setUp(): void
Expand All @@ -34,7 +36,8 @@ protected function setUp(): void

$this->sut = new OAuthUserProvider(
$em,
self::getContainer()->get(RoleRepositoryInterface::class)
self::getContainer()->get(RoleRepositoryInterface::class),
self::AUTH_CLIENT_ID
);
}

Expand Down Expand Up @@ -136,7 +139,11 @@ private function getMockedUserResponse(
'family_name' => $lastName,
'given_name' => $firstName,
'email' => $email,
'roles' => $roles,
'resource_access' => [
self::AUTH_CLIENT_ID => [
'roles' => $roles,
],
],
];
$responseProphet = $this->prophesize(UserResponseInterface::class);
$responseProphet->getData()->shouldBeCalledOnce()->willReturn($userData);
Expand Down

0 comments on commit 5e564dd

Please sign in to comment.