Skip to content

Instantly share code, notes, and snippets.

@wouterj
Created March 27, 2013 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wouterj/5254054 to your computer and use it in GitHub Desktop.
Save wouterj/5254054 to your computer and use it in GitHub Desktop.
Simple User Management example
<?php
class Authentication
{
/**
* @var UserProviderInterface
*/
private $userprovider;
/**
* @var User
*/
private $_user;
/**
* @param UserProviderInterface $userprovider
*/
public function __construct($userprovider)
{
$this->setUserProvider($userprovider);
}
public function getUser()
{
if (null === $this->_user) {
$this->_user = $this->createUser();
}
return $this->_user;
}
protected function createUser()
{
try {
$user = $this->userprovider->getUser();
} catch (\Exception $e) {
// als er iets fout is gegaan (\Exception moet preciezer)
$user = new User('anonymous');
}
return $user;
}
private function setUserProvider(UserProviderInterface $userprovider)
{
$this->userpovider = $userprovider;
}
}
<?php
class Authorization
{
private $roleTree;
/**
* @var User
*/
private $user;
public function __construct($roleTree, User $user)
{
$this->setUser($user);
$this->setRoleTree($roleTree);
}
public function isGranted($role)
{
if ($roleTree->hasRights($this->getUser($user)->getRole(), $role)) {
return true;
}
return false;
}
private function setRoleTree($roleTree)
{
// ... the roletree is loaded from a configuration file
}
}
<?php
class Role implements RoleInterface
{
private $role;
public function __construct($role)
{
$this->setRole($role);
}
public function getRole()
{
return $this->role;
}
/**
* @param string $role
*/
private function setRole($role)
{
$this->role = $role;
}
}
<?php
interface RoleInterface
{
/**
* @return string
*/
public function getRole();
}
<?php
class User
{
private $name;
/**
* @var RoleInterface
*/
private $role;
/**
* @param string $name
*/
public function __construct($name)
{
$this->setName($name);
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
public function setRole(RoleInterface $role)
{
$this->role = $role;
}
/**
* @return RoleInterface
*/
public function getRole()
{
return $this->role;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment