Skip to content

Commit 5e564dd

Browse files
SSO: Use client specific user roles (#515)
* 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
1 parent 62e4a0e commit 5e564dd

File tree

5 files changed

+26
-15
lines changed

5 files changed

+26
-15
lines changed

.php-cs-fixer.dist

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ return $config
2323
],
2424
],
2525
'single_line_throw' => false,
26-
'global_namespace_import' => true
26+
'global_namespace_import' => true,
27+
'trailing_comma_in_multiline' => false
2728
])
2829
->setUsingCache(false)
2930
->setFinder($finder);

config/services.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ services:
283283
$logoutUrl: '%app.idp.endpoint.logout%'
284284
$baseUri: '%app.base_url%'
285285

286+
mealz.oauthuserprovider: '@App\Mealz\UserBundle\Provider\OAuthUserProvider'
287+
App\Mealz\UserBundle\Provider\OAuthUserProvider:
288+
arguments:
289+
$authClientID: '%app.idp.client_id%'
290+
286291
App\Mealz\UserBundle\Repository\ProfileRepository:
287292
arguments:
288293
$entityClass: App\Mealz\UserBundle\Entity\Profile

src/Mealz/UserBundle/Provider/OAuthUserProvider.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@ class OAuthUserProvider implements UserProviderInterface, OAuthAwareUserProvider
3838
'aoe_employee' => self::ROLE_USER,
3939
];
4040

41-
private EntityManagerInterface $entityManager;
42-
private RoleRepositoryInterface $roleRepo;
43-
44-
public function __construct(EntityManagerInterface $entityManager, RoleRepositoryInterface $roleRepo)
45-
{
46-
$this->entityManager = $entityManager;
47-
$this->roleRepo = $roleRepo;
41+
public function __construct(
42+
private readonly EntityManagerInterface $entityManager,
43+
private readonly RoleRepositoryInterface $roleRepo,
44+
private readonly string $authClientID
45+
) {
4846
}
4947

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

72-
$idpUserRoles = $response->getData()['roles'] ?? [];
73-
$role = $this->toMealsRole($idpUserRoles);
70+
$data = $response->getData();
71+
$globalUserRoles = $data['roles'] ?? [];
72+
$appUserRoles = $data['resource_access'][$this->authClientID]['roles'] ?? [];
73+
$role = $this->toMealsRole(array_merge($globalUserRoles, $appUserRoles));
7474
$roles = (null === $role) ? [] : [$role];
7575

7676
try {
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
services:
2-
mealz.oauthuserprovider:
3-
alias: App\Mealz\UserBundle\Provider\OAuthUserProvider
4-
2+
# Deprecated, do not define any services here. Use services.yaml in root level config directory instead.

src/Mealz/UserBundle/Tests/Service/OAuthProviderTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class OAuthProviderTest extends AbstractControllerTestCase
2020
{
2121
use ProphecyTrait;
2222

23+
private const string AUTH_CLIENT_ID = 'meals-app';
24+
2325
private OAuthUserProvider $sut;
2426

2527
protected function setUp(): void
@@ -34,7 +36,8 @@ protected function setUp(): void
3436

3537
$this->sut = new OAuthUserProvider(
3638
$em,
37-
self::getContainer()->get(RoleRepositoryInterface::class)
39+
self::getContainer()->get(RoleRepositoryInterface::class),
40+
self::AUTH_CLIENT_ID
3841
);
3942
}
4043

@@ -136,7 +139,11 @@ private function getMockedUserResponse(
136139
'family_name' => $lastName,
137140
'given_name' => $firstName,
138141
'email' => $email,
139-
'roles' => $roles,
142+
'resource_access' => [
143+
self::AUTH_CLIENT_ID => [
144+
'roles' => $roles,
145+
],
146+
],
140147
];
141148
$responseProphet = $this->prophesize(UserResponseInterface::class);
142149
$responseProphet->getData()->shouldBeCalledOnce()->willReturn($userData);

0 commit comments

Comments
 (0)