我是Yii的新手。我正在使用Yii进行一个项目。登录操作没有任何作用,我真的很困惑。我不知道该怎么办。这是我的代码。有人能帮我修吗?谢谢大家。
-
这是UserIdentity.php
class UserIdentity extends CUserIdentity { private $_id; public function authenticate() { $user = User::model()->findByAttributes(array('username'=>$this->username)); if ($user===null) { $this->errorCode=self::ERROR_USERNAME_INVALID; } else if ($user->password !== md5($this->password) ) { $this->errorCode=self::ERROR_PASSWORD_INVALID; } else { // Okay! $this->errorCode=self::ERROR_NONE; } return !$this->errorCode; } public function getId() // override Id { return $this->_id; } }
-
这是我在SiteController.php 中的操作登录
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)); }
-
最后,介绍了LoginForm.php 中的身份验证功能
public function authenticate($attribute,$params) { if(!$this->hasErrors()) // we only want to authenticate when no input errors { $identity=new UserIdentity($this->username,$this->password); $identity->authenticate(); switch($identity->errorCode) { case UserIdentity::ERROR_NONE: Yii::app()->user->login($identity); break; case UserIdentity::ERROR_USERNAME_INVALID: $this->addError('username','Username is incorrect.'); break; default: // UserIdentity::ERROR_PASSWORD_INVALID $this->addError('password','Password is incorrect.'); break; } } }
尝试这个用户标识类
class UserIdentity extends CUserIdentity {
private $_id;
public $role;
const ERROR_EMAIL_INVALID = 3;
const ERROR_STATUS_NOTACTIV = 4;
const ERROR_STATUS_BAN = 5;
/**
* 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.
*/
public function authenticate() {
if (strpos($this->username, "@")) {
$user = User::model()->findByAttributes(array('email' => $this->username));
} else {
$user = User::model()->findByAttributes(array('username' => $this->username));
}
if ($user === null)
if (strpos($this->username, "@")) {
$this->errorCode = self::ERROR_EMAIL_INVALID;
} else {
$this->errorCode = self::ERROR_USERNAME_INVALID;
} else if (Yii::app()->getModule('admin')->encrypting($this->password) !== $user->password)
$this->errorCode = self::ERROR_PASSWORD_INVALID;
else if ($user->status == 0 && Yii::app()->getModule('admin')->loginNotActiv == false)
$this->errorCode = self::ERROR_STATUS_NOTACTIV;
else if ($user->status == -1)
$this->errorCode = self::ERROR_STATUS_BAN;
else {
$this->_id = $user->id;
$this->username = $user->username;
$this->errorCode = self::ERROR_NONE;
}
return !$this->errorCode;
}
/**
* @return integer the ID of the user record
*/
public function getId() {
return $this->_id;
}
}
以下是登录操作
public function actionLogin() {
if (Yii::app()->user->isGuest) {
$model = new UserLogin;
// collect user input data
if (isset($_POST['UserLogin'])) {
$model->attributes = $_POST['UserLogin'];
// validate user input and redirect to previous page if valid
if ($model->validate()) {
/*=========changed HERE===============*/
//previous code
//$this->redirect(array("/profile"));
$url = 'redirect where you want';
$this->redirect(array($url));
}
}
// display the login form
$this->render('/user/login', array('model' => $model));
}
else
{
$user_id = Yii::app()->user->id;
if($user_id != ''){
$this->redirect(array("/profile"));
} else
$this->redirect(Yii::app()->controller->module->returnUrl);
}
}
这是你的型号
class UserLogin extends CFormModel
{
public $username;
public $password;
public $rememberMe;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
// username and password are required
array('username, password', 'required'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
);
}
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'rememberMe'=>"Remember me next time",
'username'=>"username or email",
'password'=>"password",
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors()) // we only want to authenticate when no input errors
{
$identity=new UserIdentity($this->username,$this->password);
$identity->authenticate();
switch($identity->errorCode)
{
case UserIdentity::ERROR_NONE:
$duration=$this->rememberMe ? Yii::app()->controller->module->rememberMeTime : 0;
Yii::app()->user->login($identity,$duration);
break;
case UserIdentity::ERROR_EMAIL_INVALID:
$this->addError("username","Email is incorrect.");
break;
case UserIdentity::ERROR_USERNAME_INVALID:
$this->addError("username","Username is incorrect.");
break;
case UserIdentity::ERROR_STATUS_NOTACTIV:
$this->addError("status","Your account is not activated.");
break;
case UserIdentity::ERROR_STATUS_BAN:
$this->addError("status","Your account is blocked.");
break;
case UserIdentity::ERROR_PASSWORD_INVALID:
$this->addError("password","Password is incorrect.");
break;
}
}
}
}
通过使用以上所有的动作模型和用户身份,您可以轻松登录yii。