From 5516f55ed502d966bbbf771b7c12987a80bbf0f0 Mon Sep 17 00:00:00 2001 From: Marijus Kilmanas Date: Tue, 16 Jan 2024 14:53:19 +0200 Subject: [PATCH] Add handling for string concatenation of scopes when getting roles --- src/Models/User.php | 2 +- tests/Unit/Models/UserTest.php | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/Models/UserTest.php diff --git a/src/Models/User.php b/src/Models/User.php index 920d631..9b659e6 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -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) { diff --git a/tests/Unit/Models/UserTest.php b/tests/Unit/Models/UserTest.php new file mode 100644 index 0000000..f6bcd0e --- /dev/null +++ b/tests/Unit/Models/UserTest.php @@ -0,0 +1,48 @@ +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']); + } +}