SO Yii でアプリにログインしようとすると、サイト コントローラーの検証またはログイン機能で停止するようです。ログインすることはなく、何も役に立ちません。何も表示されない、エラーなし、nada zip
私はフォローしていました: http://www.larryullman.com/2010/01/04/simple-authentication-with-the-yii-framework/
UserIdentity クラス:
class UserIdentity extends CUserIdentity
public function authenticate()
{
$user = User::model()->findByAttributes(array('username'=>$this->username));
$saltedPW = ""; // null string for salted PW
if ($user===null) { // No user found!
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
if ($user!==null){
// salt the user password string then hash
// incase $user pw is not a salted hash, rather a password string
$saltedPW = md5(Yii::app()->params["salt"] . $user->password);
//testing the password
if (($user->password !== $this->password) ||
($saltedPW !== $this->password))
{
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
else
{
$this->errorCode=self::ERROR_NONE;
}
}
return !$this->errorCode;
}
}
サイトコントローラーからログイン:
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);
}
else
{
$this->render('login',array('model'=>$model));
}
}
// display the login form
$this->render('login',array('model'=>$model));
}
ログインフォーム
class LoginForm extends CFormModel
{
public $username;
public $password;
public $rememberMe;
private $_identity;
/**
* 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',
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect username or password.');
}
}
/**
* Logs in the user using the given username and password in the model.
* @return boolean whether login is successful
*/
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;
}
}