Skip to content

Commit

Permalink
Add handling for string concatenation of scopes when getting roles
Browse files Browse the repository at this point in the history
  • Loading branch information
mkilmanas committed Jan 16, 2024
1 parent 0af5b54 commit 5516f55
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function getRoles(): array
$scopes = $this->data['scope'] ?? [];

if (is_string($scopes)) {
$scopes = [$scopes];
$scopes = explode(' ', $scopes);
}

foreach ($roles as $role) {
Expand Down
48 changes: 48 additions & 0 deletions tests/Unit/Models/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Auth0\Tests\Unit\Models;

use Auth0\Symfony\Models\User;
use PHPUnit\Framework\TestCase;

class UserTest extends TestCase
{

/** @param string[] $expectedRoles */
private function assertHasRoles(User $user, array $expectedRoles): void
{
$userRoles = $user->getRoles();
foreach ($expectedRoles as $role) {
$this::assertContains($role, $userRoles);
}
}

public function testGetRolesWithSingleScope(): void
{
$user = new User([
'scope' => 'read:users',
]);

$this->assertHasRoles($user, ['ROLE_USER', 'ROLE_READ_USERS']);
}

public function testGetRolesWithArrayScope(): void
{
$user = new User([
'scope' => ['read:users', 'write:users'],
]);

$this->assertHasRoles($user, ['ROLE_USER', 'ROLE_READ_USERS', 'ROLE_WRITE_USERS']);
}

public function testGetRolesWithStringScope(): void
{
$user = new User([
'scope' => 'read:users write:users',
]);

$this->assertHasRoles($user, ['ROLE_USER', 'ROLE_READ_USERS', 'ROLE_WRITE_USERS']);
}
}

0 comments on commit 5516f55

Please sign in to comment.