Skip to content

Commit 63d6658

Browse files
committed
feat: Utilise Role information from client apps.
Userbase client applications may supply their own roles for users loaded by UserProvider. UserProvider implements RoleManagerInterface and User implements RoleInterface from the userbase-role-contracts package. UserProvider will use any RoleProviderInterface to add roles to a User loaded in the loadUserByUsername method.
1 parent 9d34b62 commit 63d6658

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
],
1414
"require": {
1515
"php": ">=5.3.0",
16+
"linkorb/userbase-role-contracts": "^1.0",
1617
"symfony/security": "~2.6 || ~3.0 || ^4",
1718
"psr/cache": "~1.0",
1819
"symfony/cache": "~3.0 || ^4"

src/Model/User.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
namespace UserBase\Client\Model;
44

5+
use LinkORB\Contracts\UserbaseRole\RoleInterface;
56
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
6-
use Symfony\Component\Security\Core\Role\Role;
77
use RuntimeException;
88

9-
final class User implements UserInterface, AdvancedUserInterface, AccountContainerInterface, PolicyContainerInterface
9+
final class User implements
10+
AccountContainerInterface,
11+
AdvancedUserInterface,
12+
PolicyContainerInterface,
13+
RoleInterface,
14+
UserInterface
1015
{
1116
private $password;
1217
private $enabled;

src/UserProvider.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use RuntimeException;
66

7+
use LinkORB\Contracts\UserbaseRole\RoleManagerInterface;
8+
use LinkORB\Contracts\UserbaseRole\RoleProviderInterface;
79
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
810
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
911
use Symfony\Component\Security\Core\User\UserInterface;
@@ -14,9 +16,10 @@
1416
use UserBase\Client\Event\UserLoadedEvent;
1517

1618

17-
class UserProvider implements UserProviderInterface
19+
class UserProvider implements UserProviderInterface, RoleManagerInterface
1820
{
1921
private $client;
22+
private $roleProvider;
2023
private $shouldRefresh;
2124
private $dispatcher;
2225

@@ -27,6 +30,11 @@ public function __construct(Client $client, $shouldRefresh = true, EventDispatch
2730
$this->dispatcher = $dispatcher;
2831
}
2932

33+
public function setRoleProvider(RoleProviderInterface $roleProvider)
34+
{
35+
$this->roleProvider = $roleProvider;
36+
}
37+
3038
public function loadUserByUsername($username)
3139
{
3240
try {
@@ -35,14 +43,23 @@ public function loadUserByUsername($username)
3543
$event = new UserLoadedEvent($user);
3644
$this->dispatcher->dispatch('userbase.user_loaded', $event);
3745
}
38-
return $user;
3946
} catch (RuntimeException $e) {
4047
throw new UsernameNotFoundException(
4148
"A User named \"{$username}\" cannot be found in Userbase.",
4249
null,
4350
$e
4451
);
4552
}
53+
54+
if (!$this->roleProvider) {
55+
return $user;
56+
}
57+
58+
foreach ($this->roleProvider->getRoles($user) as $roleName) {
59+
$user->addRole($roleName);
60+
}
61+
62+
return $user;
4663
}
4764

4865
public function refreshUser(UserInterface $user)

0 commit comments

Comments
 (0)