I have a problem with my yii application which is:
There are types of users that can log into my application, each will be shown some items in the menu.
When I log in not using the remember me checkbox I don't see the items I should see in the menu (I think it doesn't create a session for user and user role).
But when I log in with the checkbox checked, I see the items in the menu (I have enabled Cookie based authentication to check this).
How can I get the user to see his items when he/she logs in without having them to check the Remember me checkbox?
This is my authentication code
<?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;
}
}
This is my action login
public function actionLogin()
{
$model=new LoginForm;
// if it is ajax validation request
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
// collect user input data
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
// validate user input and redirect to the previous page if valid
if($model->validate() && $model->login())
$this->redirect(Yii::app()->user->returnUrl);
}
// display the login form
$this->render('login',array('model'=>$model));
}
and this is the LoginForm login
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;
}
And this is the menu code
<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 -->
You can use session in action like :
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));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.