Skip to content

Instantly share code, notes, and snippets.

@matej21
Created February 9, 2014 22:19
Show Gist options
  • Save matej21/8906884 to your computer and use it in GitHub Desktop.
Save matej21/8906884 to your computer and use it in GitHub Desktop.
<?php
/**
* @author David Matejka
*/
class Identity extends \Nette\Object implements \Nette\Security\IIdentity
{
/** @var int */
protected $id;
/** @var IUser */
protected $entity;
/** @var bool is identity loaded? */
protected $loaded = FALSE;
/** @var array */
protected $roles = array();
/**
* @param IUser $entity
*/
public function __construct(IUser $entity)
{
$this->id = $entity->getId();
$this->entity = $entity;
}
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* @return array
*/
public function getRoles()
{
return $this->roles;
}
public function __sleep()
{
return array("id");
}
public function __wakeup()
{
$this->loaded = FALSE;
}
/**
* @param EntityDao $dao
*/
public function load(EntityDao $dao)
{
$this->entity = $dao->find($this->id);
$this->loaded = TRUE;
}
/**
* @return bool
*/
public function isLoaded()
{
return $this->loaded;
}
/**
* @return IUser
*/
public function getEntity()
{
return $this->entity;
}
public function &__get($name)
{
try {
$value = $this->entity->{$name};
} catch (MemberAccessException $e) {
$value = parent::__get($name);
}
return $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment