I have OO user. It’s look like this one…
class UserIdentity extends CUserIdentity
{
const ERROR_CONFIRM = 15;
//
private $id;
private $oUser;
/**
* @return bool|int
*/
public function authenticate()
{
$this->errorCode = self::ERROR_UNKNOWN_IDENTITY; // default
$oLoader = new LoaderUser();
$oLoader
->createCriteria('byUsername', strtolower($this->username))
->load();
if ($oLoader->count() !== 1) {
$this->errorCode = self::ERROR_USERNAME_INVALID;
} else {
$oUser = $oLoader->getResult(true);
$bPassword = CPasswordHelper::verifyPassword($this->password, $oUser->getPassword());
if (!$bPassword) {
$this->errorCode = self::ERROR_PASSWORD_INVALID;
} elseif (!$oUser->getConfirmStatus()) {
$this->errorCode = self::ERROR_CONFIRM;
} else {
$this->id = $oUser->getID();
$this->oUser = $oUser;
$this->errorCode = self::ERROR_NONE;
}
}
return $this->errorCode;
}
/**
* @return null|oUser
*/
public function getUser()
{
return $this->oUser;
}
/**
* @return string|int
*/
public function getID()
{
return $this->id;
}
}
class WebUser extends CWebUser
{
/**
* @param string $sName
* @param string|array|null $aParam
*
* @return mixed
* @throws Exception
*/
public function __call($sName, $aParam)
{
if ($this->hasState('oUser')) {
$oUser = $this->getState('oUser');
if (method_exists($oUser, $sName)) {
return call_user_func_array(array($oUser, $sName), $aParam);
}
}
return parent::__call($sName, $aParam);
}
/**
* @param UserIdentity $oIdentity
* @param int $iDuration
*
* @return bool|void
*/
public function login($oIdentity, $iDuration)
{
$this->setState('oUser', $oIdentity->getUser());
parent::login($oIdentity, $iDuration);
}
}
And our object oUser
class oUser extends ObjectAbstract implements oUserInterface
{
/**
* @param string|int $iID
*
* @return $this
*/
public function loadModelByID($iID)
{
$this->setModel(User::model()->findByPk($iID));
return $this;
}
/**
* @return bool
*/
public function isBanned()
{
return ($this->getModel()->oBanUser) ? true : false;
}
/**
* Проверяет обаладет ли пользователь ролью. Агрумент должен
* выглядить как Role::ADMIN.
*
* @param string $sRole
*
* @return bool
*/
public function hasRole($sRole)
{
$bReturn = false;
$aUserRole = $this->getRoles();
if ($aUserRole) {
foreach ($aUserRole as $oUserRole) {
$oRole = $oUserRole->oRole;
if ($oRole->name == $sRole || $oRole->name == Role::ADMIN) {
$bReturn = true;
break;
}
}
}
return $bReturn;
}
/**
* Проверяет обладает ли пользователь вообще какими либо ролями.
*
* @return bool
*/
public function hasRoles()
{
return ($this->getRoles()) ? true : false;
}
/**
* @return bool
*/
public function hasRoleRequests()
{
return ($this->getRoleRequests()) ? true : false;
}
/**
* @return int
*/
public function getID()
{
return $this->getModel()->id;
}
/**
* @return string
*/
public function getName()
{
return $this->getModel()->username;
}
/**
* Возвращает массив ролей (UserRole).
*
* @return array|mixed|null
*/
public function getRoles()
{
return $this->getModel()->aUserRole;
}
/**
* Возвращает массив запросов на роли (UserRoleRequest).
*
* @return array|mixed|null
*/
public function getRoleRequests()
{
$aReturn = null;
$aRoleRequest = $this->getModel()->aUserRoleRequest;
if ($aRoleRequest) {
foreach ($aRoleRequest as $oRoleRequest) {
if (!$oRoleRequest->dateEnd) {
$aReturn[] = $oRoleRequest;
}
}
}
return $aReturn;
}
//...
It’s not perfect - I know… So question (but for it’s trouble) is that web user is defined on login. Every F5 on page it takes data from cache… And relations too! For example when I add role, or delete. After refresh it’s still has the roles what were on login. So to refresh the object (oUser) I need to re-login. How to avoid this cache or how to refresh “oUser” on each page refresh?
PS: I can simply not to use relations() to get data, but I want to use them. And I think it’s an option of CActiveRecord no the Yii one.