Php YII登录don';不要给我看菜单项
我的yii应用程序存在以下问题:Php YII登录don';不要给我看菜单项,php,cookies,yii,Php,Cookies,Yii,我的yii应用程序存在以下问题: 有几种类型的用户可以登录到我的应用程序,每种类型的用户都会在菜单中显示一些项目 当我不使用“记住我”复选框登录时,我看不到应该在菜单中看到的项目(我认为它不会为用户和用户角色创建会话) 但当我选中复选框登录时,我会看到菜单中的项目(我已启用基于Cookie的身份验证来选中此项) 如何让用户在登录时查看其项目,而不必选中“记住我”复选框 这是我的身份验证码 <?php /** * UserIdentity represents the data ne
- 有几种类型的用户可以登录到我的应用程序,每种类型的用户都会在菜单中显示一些项目
- 当我不使用“记住我”复选框登录时,我看不到应该在菜单中看到的项目(我认为它不会为用户和用户角色创建会话)
- 但当我选中复选框登录时,我会看到菜单中的项目(我已启用基于Cookie的身份验证来选中此项)
<?php
/**
* UserIdentity represents the data needed to identity a user.
* It contains the authentication method that checks if the provided
* data can identity the user.
*/
class UserIdentity extends CUserIdentity
{
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
private $_id;
public function authenticate()
{
$user = User::model()->findByAttributes(array('username'=>$this->username));
if($user === null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
elseif($user->password!== sha1($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else{
$this->errorCode=self::ERROR_NONE;
$this->setState ('role', $user->role);
$this->_id = $user->id;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
这是LoginForm登录
public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->username,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else
return false;
}
这是菜单代码
<div id="mainmenu">
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'Volunteers', 'url'=>array('/volunteers/index'),
'visible'=>((!Yii::app()->user->isGuest) &&
(Yii::app()->user->role==='admin'
|| Yii::app()->user->role==='DataEntry'
|| Yii::app()->user->role==='CCGazb'))
),
array('label'=>'Create Volunteer', 'url'=>array('/volunteers/create'),
'visible'=>((!Yii::app()->user->isGuest) &&
(Yii::app()->user->role==='Interviewer'))
),
array('label'=>'Interviews', 'url'=>array('/interviews/index'),
'visible'=>((!Yii::app()->user->isGuest) &&
(Yii::app()->user->role==='admin'
|| Yii::app()->user->role==='Interviewer'
|| Yii::app()->user->role==='CCGazb'))
),
array('label'=>'Teachers', 'url'=>array('/teachers/index'),
'visible'=>((!Yii::app()->user->isGuest) &&
(Yii::app()->user->role==='admin'
|| Yii::app()->user->role==='Interviewer'
|| Yii::app()->user->role==='CCTeachers'
|| Yii::app()->user->role==='Trainer'))
),
array('label'=>'Users', 'url'=>array('/user/index'),
'visible'=>((!Yii::app()->user->isGuest) &&
(Yii::app()->user->role==='admin'))
),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
)); ?>
</div><!-- mainmenu -->
您可以在操作中使用会话,如:
public function actionLogin() {
// If the user is already logged in send them to the return Url
if (!Yii::app()->user->isGuest)
$this->redirect(Yum::module()->returnUrl);
$this->layout = Yum::module()->loginLayout;
$this->loginForm = new YumUserLogin('login');
$success = false;
$action = 'login';
$login_type = null;
if (isset($_POST['YumUserLogin'])) {
$this->loginForm->attributes = $_POST['YumUserLogin'];
$t = Yum::module()->loginType;
// validate user input for the rest of login methods
if ($this->loginForm->validate()) {
if ($t & UserModule::LOGIN_BY_USERNAME) {
$success = $this->loginByUsername();
if ($success)
$login_type = 'username';
}
if ($t & UserModule::LOGIN_BY_EMAIL && !$success) {
$success = $this->loginByEmail();
if ($success)
$login_type = 'email';
}
}
//cookie with login type for later flow control in app
if ($success instanceof YumUser) {
if ($login_type) {
$cookie = new CHttpCookie('login_type', serialize($login_type));
$cookie->expire = time() + (3600 * 24 * 30);
Yii::app()->request->cookies['login_type'] = $cookie;
}
Yum::log(Yum::t(
'User {username} successfully logged in (Ip: {ip})', array(
'{ip}' => Yii::app()->request->getUserHostAddress(),
'{username}' => $success->username)));
if (Yum::module()->afterLogin !== false)
call_user_func(Yum::module()->afterLogin);
$this->redirectUser($success);
} else
// $this->loginForm->addError('username',Yum::t('Login is not possible with the given credentials'));
$this->loginForm->addError('username', Yum::t(''));
}
$this->render(Yum::module()->loginView, array(
'model' => $this->loginForm));
}
始终在问题中包含您的代码!张贴您的菜单代码!您使用什么条件语句来检查它是否是登录用户?我发布了所有相关代码。
public function actionLogin() {
// If the user is already logged in send them to the return Url
if (!Yii::app()->user->isGuest)
$this->redirect(Yum::module()->returnUrl);
$this->layout = Yum::module()->loginLayout;
$this->loginForm = new YumUserLogin('login');
$success = false;
$action = 'login';
$login_type = null;
if (isset($_POST['YumUserLogin'])) {
$this->loginForm->attributes = $_POST['YumUserLogin'];
$t = Yum::module()->loginType;
// validate user input for the rest of login methods
if ($this->loginForm->validate()) {
if ($t & UserModule::LOGIN_BY_USERNAME) {
$success = $this->loginByUsername();
if ($success)
$login_type = 'username';
}
if ($t & UserModule::LOGIN_BY_EMAIL && !$success) {
$success = $this->loginByEmail();
if ($success)
$login_type = 'email';
}
}
//cookie with login type for later flow control in app
if ($success instanceof YumUser) {
if ($login_type) {
$cookie = new CHttpCookie('login_type', serialize($login_type));
$cookie->expire = time() + (3600 * 24 * 30);
Yii::app()->request->cookies['login_type'] = $cookie;
}
Yum::log(Yum::t(
'User {username} successfully logged in (Ip: {ip})', array(
'{ip}' => Yii::app()->request->getUserHostAddress(),
'{username}' => $success->username)));
if (Yum::module()->afterLogin !== false)
call_user_func(Yum::module()->afterLogin);
$this->redirectUser($success);
} else
// $this->loginForm->addError('username',Yum::t('Login is not possible with the given credentials'));
$this->loginForm->addError('username', Yum::t(''));
}
$this->render(Yum::module()->loginView, array(
'model' => $this->loginForm));
}