Skip to content

Commit 36036d5

Browse files
authored
Merge pull request #15 from boite/develop
Let apps supply roles for users
2 parents a2dfa4f + 63d6658 commit 36036d5

3 files changed

Lines changed: 50 additions & 27 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: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@
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;
1318
private $accountNonExpired;
1419
private $credentialsNonExpired;
1520
private $accountNonLocked;
1621
private $roles;
17-
22+
1823
private $createdAt;
1924
private $lastSeenAt;
2025
private $deletedAt;
21-
26+
2227
private $accountUsers = array();
2328
private $policies = array();
2429

@@ -37,42 +42,42 @@ public function __construct($name)
3742
$this->roles = array();
3843
$this->salt = "KJH6212kjwek_fj23D01-239.1023fkjdsj^k2hdfssfjk!h234uiy4324";
3944
}
40-
45+
4146
public function getCreatedAt()
4247
{
4348
return $this->createdAt;
4449
}
45-
50+
4651
public function setCreatedAt($createdAt)
4752
{
4853
$this->createdAt = $createdAt;
4954
return $this;
5055
}
51-
56+
5257
public function getDeletedAt()
5358
{
5459
return $this->deletedAt;
5560
}
56-
61+
5762
public function setDeletedAt($deletedAt)
5863
{
5964
$this->deletedAt = $deletedAt;
6065
return $this;
6166
}
62-
67+
6368
public function getLastSeenAt()
6469
{
6570
return $this->lastSeenAt;
6671
}
67-
72+
6873
public function setLastSeenAt($lastSeenAt)
6974
{
7075
if ($this->lastSeenAt>0) {
7176
$this->lastSeenAt = $lastSeenAt;
7277
}
7378
return $this;
7479
}
75-
80+
7681
/**
7782
* {@inheritdoc}
7883
*/
@@ -88,7 +93,7 @@ public function getPassword()
8893
{
8994
return $this->password;
9095
}
91-
96+
9297
public function setPassword($password)
9398
{
9499
$this->password = $password;
@@ -115,18 +120,18 @@ public function setUsername($username)
115120
$this->name = $username;
116121
return $this;
117122
}
118-
123+
119124
public function getName()
120125
{
121126
return $this->name;
122127
}
123-
128+
124129
public function getDisplayName()
125130
{
126131
$account = $this->getUserAccount();
127132
return $account->getDisplayName();
128133
}
129-
134+
130135
/**
131136
* {@inheritdoc}
132137
*/
@@ -165,7 +170,7 @@ public function isEnabled()
165170
{
166171
return $this->enabled;
167172
}
168-
173+
169174
public function setEnabled($enabled)
170175
{
171176
$this->enabled = $enabled;
@@ -178,12 +183,12 @@ public function setEnabled($enabled)
178183
public function eraseCredentials()
179184
{
180185
}
181-
186+
182187
public function getEmail()
183188
{
184189
return $this->getUserAccount()->getEmail();
185190
}
186-
191+
187192
public function getPictureUrl($size = null)
188193
{
189194
return $this->getUserAccount()->getPictureUrl($size);
@@ -193,12 +198,12 @@ public function addAccountUser(AccountUser $accountUser)
193198
{
194199
$this->accountUsers[] = $accountUser;
195200
}
196-
201+
197202
public function getAccountUsers()
198203
{
199204
return $this->accountUsers;
200205
}
201-
206+
202207
public function getAccounts()
203208
{
204209
$accounts = array();
@@ -207,7 +212,7 @@ public function getAccounts()
207212
}
208213
return $accounts;
209214
}
210-
215+
211216
public function getUserAccount()
212217
{
213218
foreach ($this->getAccounts() as $account) {
@@ -217,7 +222,7 @@ public function getUserAccount()
217222
}
218223
throw new RuntimeException("This user has no user-account: " . $this->getName());
219224
}
220-
225+
221226
public function getAccountsByType($type)
222227
{
223228
$res = array();
@@ -228,17 +233,17 @@ public function getAccountsByType($type)
228233
}
229234
return $res;
230235
}
231-
236+
232237
public function addPolicy(Policy $policy)
233238
{
234239
$this->policies[] = $policy;
235240
}
236-
241+
237242
public function getPolicies()
238243
{
239244
return $this->policies;
240245
}
241-
246+
242247
public function addRole($roleName)
243248
{
244249
$this->roles[] = $roleName;

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)