PDA

View Full Version : راهنمايي : login خودكار بعد از ثبت نام با اين روش



desatir7316
شنبه 22 شهریور 1393, 08:09 صبح
سلام دوستان
براي login خودكار بعد از ثبت نام از اين روش استفاده كردم
به نظر شما مشكل امنيتي چيزي نداره؟

توي مدل user اين رو اضافه كردم:


public function afterValidate() {
parent::afterValidate();
//ensure we dont have any other errors
if(!$this->hasErrors()){
$this->password = md5($this->password);
Yii::app()->user->setState('autoLoginPassword', $this->password);
}

}

اكشن create از كنترلر user رو هم به اين شكل تغيير دادم:


public function actionCreate()
{
$model=new User;

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);

if(isset($_POST['User']))
{
// CVarDumper::dump($_POST['User']);
$model->attributes=$_POST['User'];
if($model->save()){
# auto login
if(Yii::app()->user->hasState('autoLoginPassword')){
$identity=new UserIdentity($_POST['User']['username'],Yii::app()->user->getState("autoLoginPassword"));
$identity->errorCode = UserIdentity::ERROR_NONE;
$identity->setId($model->user_id);
$identity->username = $model->username;
$identity->authenticate();
Yii::app()->user->login($identity);

$this->redirect(Yii::app()->user->returnUrl);
// $this->redirect(array('view','id'=>$model->user_id));
}
}
}

$this->render('create',array(
'model'=>$model,
));
}

MMSHFE
شنبه 22 شهریور 1393, 08:50 صبح
نه مشکل خاصی نداره چون از همون سیستم لاگین استاندارد استفاده کردین.

desatir7316
یک شنبه 23 شهریور 1393, 18:23 عصر
یه راه قشنگتر




if ($model->save()){
# auto login
$loginForm = new LoginForm;
$loginForm->attributes = $_POST['User'];
if ($loginForm->login()) {
# redirect the user
}
}