_record = $record; } public function getDbRecord() { if (!$this->_record) { $this->_record = UserFacade::getInstance()->findByLogin($this->Name); } return $this->_record; } private $_authKey; public function setAuthKey($key) { $this->_authKey = $key; } public function getAuthKey() { return $this->_authKey; } public function createUser($username) { $dbUser = UserFacade::getInstance()->findByLogin($username); if (!$dbUser) { return NULL; } $user = new DbUser($this->Manager); $user->setDbRecord($dbUser); $user->Name = $dbUser->Login; if ($dbUser->IsAdmin) { $user->Roles = 'Admin'; } $user->IsGuest = FALSE; return $user; } public function validateUser($login, $password) { $user = UserFacade::getInstance()->findByLogin($login); $dbPassword = $user ? $user->Password : ''; if (UserFacade::getInstance()->verifyPassword($password, $dbPassword) && $user) { $user->LastLogin = date('Y-m-d H:i:s'); $user->save(); return TRUE; } else { return FALSE; } } public function createUserFromCookie($cookie) { $userFacade = UserFacade::getInstance(); try { $userData = $userFacade->getUserFromCookieData($cookie); if ($userData) { $user = $this->createUser($userData->User); $user->AuthKey = $userData->Key; return $user; } return NULL; } catch (TInvalidDataException $e) { return NULL; } return NULL; } public function saveUserToCookie($cookie) { $userFacade = UserFacade::getInstance(); $authKey = $userFacade->createUserAuthKey($this); if ($authKey) { $cookieData = $userFacade->compileCookieData($authKey); $cookie->setValue($cookieData); $cookie->setExpire(4294967296); // 2**32 } } public function __call($name, $args) { $match = []; if (preg_match('/^getIs(.+)$/', $name, $match)) { return $this->isInRole($match[1]); } throw new Exception('Unimplemented CustomDbUser method'); } } ?>