我已使用此代码
在前端模板中成功实现了会话超时 frontend\config\main.php
return [
'id' => 'app-frontend',
'basePath' => dirname(__DIR__),
'bootstrap' => ['log'],
'homeUrl' => '/myweb',
'controllerNamespace' => 'frontend\controllers',
'components' => [
'formatter' => [
'class' => 'yii\i18n\Formatter',
'thousandSeparator' => '.',
'decimalSeparator' => ','
//'currencyCode' => '$'
],
//session time out code here
'user' => [
'identityClass' => 'common\models\UserCustomer',
'enableAutoLogin' => false,
//'enableSession' => true,
'authTimeout' => 3600,
],
'session' => [
'class' => 'yii\web\Session',
'cookieparams' => ['httponly' => true, 'lifetime' => 3600],
'timeout' => 3600,
'useCookies' => true,
],
//end session time out
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'request' => [
'baseUrl' => '/myweb',
],
],
'params' => $params,
];
但我想知道如果我有多个管理员用户,我将如何在后端实现。
这是我的backend\config\main.php
return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'bootstrap' => ['log'],
//multiple user here
'modules' => [
'agent' => [
'class' => 'backend\modules\agent\Agent',
],
'company' => [
'class' => 'backend\modules\company\Company',
'defaultRoute' => 'home',
],
'vendor' => [
'class' => 'backend\modules\vendor\Vendor',
'defaultRoute' => 'default',
],
'services' => [
'class' => 'backend\modules\services\Services',
'defaultRoute' => 'site',
],
],
'homeUrl' => '/myweb/administrator',
'components' => [
'formatter' => [
'class' => 'yii\i18n\Formatter',
'thousandSeparator' => '.',
'decimalSeparator' => ',',
//'currencyCode' => '$'
],
// this code has no effect after 60 second inactivity
'user' => [
'identityClass' => 'common\models\UserBeo',
'enableAutoLogin' => false,
'authTimeout' => 60
],
'session' => [
'class' > 'yii\web\Session',
'cookieparams' => ['httponly' => true, 'lifetime' => 60],
'timeout' => 60,
'useCookies' => true,
],
// end session time out
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
'enablePrettyUrl' => true,
'showScriptName' => false,
/*
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
*/
],
'request' => [
'baseUrl' => '/myweb/administrator',
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
],
/*
'response' => [
'format' => yii\web\Response::FORMAT_JSON,
'charset' => 'UTF-8',
// ...
]
*/
],
'params' => $params,
];
在实现代码之后就像后端的前端一样,会话超时代码在60秒不活动后没有效果,
我的代码有问题吗?
无论如何,这是我的common\models\UserBeo
代码
<?php
namespace common\models;
use yii\web\IdentityInterface;
use common\models\TbUserAgent;
use common\models\TbUserCompany;
use common\models\TbUserVendor;
class UserBeo extends \yii\base\Model implements IdentityInterface
{
//tipe aktor, 0 untuk customer, 1 untuk company
private $username = '';
private $user_id = '';
private $email = '';
private $password = '';
private $jenis_user = '';
private $AktorType = '';
private $user_status ='';
private $is_agent = '';
public function rules()
{
return [
[['email', 'password','jenis_user'], 'required'],
[['email'], 'email'],
[['email','password','jenis_user'], 'safe']
];
}
function UserBeo($aktor_type)
{
$this->AktorType = $aktor_type;
}
public function getUsername()
{
return $this->username;
}
public function getAuthKey() {
return null;
}
public function getId() {
return $this->user_id;
}
public function validateAuthKey($authKey) {
//not implemented
}
public static function findIdentity($id) {
if($this->AktorType == EAktorType::Customer)
{
$user = TbCustomer::find()->where("customer_id = $id")->one();
$userbeo = new UserBeo();
$userbeo->setEmail($user->email);
$userbeo->setUser_id($user->customer_id);
$userbeo->setUsername($user->username);
$userbeo->is_agent = $user->is_agent;
$userbeo->jenis_user = EAktorType::Customer;
return $userbeo;
}
else if ($this->AktorType == EAktorType::Company)
{
$user = TbUserCompany::find()->where("user_id = $id")->one();
$userbeo = new UserBeo();
$userbeo->setEmail($user->email);
$userbeo->setUser_id($user->user_id);
$userbeo->setUsername($user->username);
$userbeo->jenis_user = EAktorType::Company;
return $userbeo;
}
else if ($this->AktorType == EAktorType::Agent)
{
$user = TbUserAgent::find()->where("user_id = $id")->one();
$userbeo = new UserBeo();
$userbeo->setEmail($user->email);
$userbeo->setUser_id($user->user_id);
$userbeo->setUsername($user->username);
$userbeo->jenis_user = EAktorType::Agent;
return $userbeo;
}
else if ($this->AktorType == EAktorType::Vendor)
{
$user = TbUserVendor::find()->where('user_id = $id')->one();
$userbeo = new UserBeo();
$userbeo->setEmail($user->email);
$userbeo->setUser_id($user->user_id);
$userbeo->setUsername($user->username);
$userbeo->jenis_user = EAktorType::Vendor;
return $userbeo;
}
}
public static function findIdentityByAccessToken($token, $type = null) {
//not implemented
}
//dibawah ini hanya getter dan setter saja
function getUser_id() {
return $this->user_id;
}
function getEmail() {
return $this->email;
}
function getPassword() {
return $this->password;
}
function getJenis_user() {
return $this->jenis_user;
}
function getAktorType() {
return $this->AktorType;
}
function getUser_status() {
return $this->user_status;
}
function setUser_id($user_id) {
$this->user_id = $user_id;
}
function setEmail($email) {
$this->email = $email;
}
function getIs_agent() {
return $this->is_agent;
}
function setPassword($password) {
$this->password = $password;
}
function setJenis_user($jenis_user) {
$this->jenis_user = $jenis_user;
}
function setAktorType($AktorType) {
$this->AktorType = $AktorType;
}
function setIsagent($AktorType) {
$this->is_agent = $AktorType;
}
function setUser_status($user_status) {
$this->user_status = $user_status;
}
function setUsername($user_name) {
$this->username = $user_name;
}
}
UserAgent
,UserCompany
和UserVendor
是后端的用户类型。
因此UserAgent
和UserVendor
可以在前端和后端登录,但UserCompany
只能登录后端。
如果您需要其他信息,请告诉我们。
提前致谢。