Skip to content

Commit

Permalink
Merge pull request #15 from boite/develop
Browse files Browse the repository at this point in the history
Let apps supply roles for users
  • Loading branch information
joostfaassen committed Jul 2, 2019
2 parents a2dfa4f + 63d6658 commit 36036d5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
],
"require": {
"php": ">=5.3.0",
"linkorb/userbase-role-contracts": "^1.0",
"symfony/security": "~2.6 || ~3.0 || ^4",
"psr/cache": "~1.0",
"symfony/cache": "~3.0 || ^4"
Expand Down
55 changes: 30 additions & 25 deletions src/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@

namespace UserBase\Client\Model;

use LinkORB\Contracts\UserbaseRole\RoleInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Security\Core\Role\Role;
use RuntimeException;

final class User implements UserInterface, AdvancedUserInterface, AccountContainerInterface, PolicyContainerInterface
final class User implements
AccountContainerInterface,
AdvancedUserInterface,
PolicyContainerInterface,
RoleInterface,
UserInterface
{
private $password;
private $enabled;
private $accountNonExpired;
private $credentialsNonExpired;
private $accountNonLocked;
private $roles;

private $createdAt;
private $lastSeenAt;
private $deletedAt;

private $accountUsers = array();
private $policies = array();

Expand All @@ -37,42 +42,42 @@ public function __construct($name)
$this->roles = array();
$this->salt = "KJH6212kjwek_fj23D01-239.1023fkjdsj^k2hdfssfjk!h234uiy4324";
}

public function getCreatedAt()
{
return $this->createdAt;
}

public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}

public function getDeletedAt()
{
return $this->deletedAt;
}

public function setDeletedAt($deletedAt)
{
$this->deletedAt = $deletedAt;
return $this;
}

public function getLastSeenAt()
{
return $this->lastSeenAt;
}

public function setLastSeenAt($lastSeenAt)
{
if ($this->lastSeenAt>0) {
$this->lastSeenAt = $lastSeenAt;
}
return $this;
}

/**
* {@inheritdoc}
*/
Expand All @@ -88,7 +93,7 @@ public function getPassword()
{
return $this->password;
}

public function setPassword($password)
{
$this->password = $password;
Expand All @@ -115,18 +120,18 @@ public function setUsername($username)
$this->name = $username;
return $this;
}

public function getName()
{
return $this->name;
}

public function getDisplayName()
{
$account = $this->getUserAccount();
return $account->getDisplayName();
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -165,7 +170,7 @@ public function isEnabled()
{
return $this->enabled;
}

public function setEnabled($enabled)
{
$this->enabled = $enabled;
Expand All @@ -178,12 +183,12 @@ public function setEnabled($enabled)
public function eraseCredentials()
{
}

public function getEmail()
{
return $this->getUserAccount()->getEmail();
}

public function getPictureUrl($size = null)
{
return $this->getUserAccount()->getPictureUrl($size);
Expand All @@ -193,12 +198,12 @@ public function addAccountUser(AccountUser $accountUser)
{
$this->accountUsers[] = $accountUser;
}

public function getAccountUsers()
{
return $this->accountUsers;
}

public function getAccounts()
{
$accounts = array();
Expand All @@ -207,7 +212,7 @@ public function getAccounts()
}
return $accounts;
}

public function getUserAccount()
{
foreach ($this->getAccounts() as $account) {
Expand All @@ -217,7 +222,7 @@ public function getUserAccount()
}
throw new RuntimeException("This user has no user-account: " . $this->getName());
}

public function getAccountsByType($type)
{
$res = array();
Expand All @@ -228,17 +233,17 @@ public function getAccountsByType($type)
}
return $res;
}

public function addPolicy(Policy $policy)
{
$this->policies[] = $policy;
}

public function getPolicies()
{
return $this->policies;
}

public function addRole($roleName)
{
$this->roles[] = $roleName;
Expand Down
21 changes: 19 additions & 2 deletions src/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use RuntimeException;

use LinkORB\Contracts\UserbaseRole\RoleManagerInterface;
use LinkORB\Contracts\UserbaseRole\RoleProviderInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
Expand All @@ -14,9 +16,10 @@
use UserBase\Client\Event\UserLoadedEvent;


class UserProvider implements UserProviderInterface
class UserProvider implements UserProviderInterface, RoleManagerInterface
{
private $client;
private $roleProvider;
private $shouldRefresh;
private $dispatcher;

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

public function setRoleProvider(RoleProviderInterface $roleProvider)
{
$this->roleProvider = $roleProvider;
}

public function loadUserByUsername($username)
{
try {
Expand All @@ -35,14 +43,23 @@ public function loadUserByUsername($username)
$event = new UserLoadedEvent($user);
$this->dispatcher->dispatch('userbase.user_loaded', $event);
}
return $user;
} catch (RuntimeException $e) {
throw new UsernameNotFoundException(
"A User named \"{$username}\" cannot be found in Userbase.",
null,
$e
);
}

if (!$this->roleProvider) {
return $user;
}

foreach ($this->roleProvider->getRoles($user) as $roleName) {
$user->addRole($roleName);
}

return $user;
}

public function refreshUser(UserInterface $user)
Expand Down

0 comments on commit 36036d5

Please sign in to comment.