Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support string concatenation of scopes #183

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']);
}
}
Loading