2014-12-27 2 views
0

내가 데시벨로 완벽한 로그인 시스템을 만들기 위해이 간단한 튜토리얼 http://www.bsourcecode.com/yiiframework2/yii-2-user-login-from-database/Yii2.0 전체 로그인 기본 템플릿

다음과 같은 기본 템플릿을 yii2 사용하고 있습니다. 실제로 모든 것이 정상인 경우에도 로그인 할 수 없으므로 코드가 작동하지 않습니다. 누가이를 재생산하고 무엇이 잘못되었는지 볼 수 있습니까?

아래에서 찾을 수있는 모델보기 및 컨트롤러를 사용하고 있습니다.

<?php 

namespace app\controllers; 

use Yii; 
use app\models\User; 
use yii\data\ActiveDataProvider; 
use yii\web\Controller; 
use yii\web\NotFoundHttpException; 
use yii\filters\VerbFilter; 
use yii\Log; 

/** 
* UserController implements the CRUD actions for User model. 
*/ 
class UserController extends Controller 
{ 
public function behaviors() 
{ 
    return [ 

    'verbs' => [ 
     'class' => VerbFilter::className(), 
     'actions' => [ 
      'logout' => ['post'], 
     ], 
    ], 
]; 
} 

/** 
* Lists all User models. 
* @return mixed 
*/ 
public function actionIndex() 
{ 
    $dataProvider = new ActiveDataProvider([ 
     'query' => User::find(), 
    ]); 

    return $this->render('index', [ 
     'dataProvider' => $dataProvider, 
    ]); 
} 

/** 
* Displays a single User model. 
* @param string $id 
* @return mixed 
*/ 
public function actionView($id) 
{ 
    return $this->render('view', [ 
     'model' => $this->findModel($id), 
    ]); 
} 

/** 
* Creates a new User model. 
* If creation is successful, the browser will be redirected to the 'view' page. 
* @return mixed 
*/ 
public function actionCreate() 
{ 
    $model = new User(); 

    if ($model->load(Yii::$app->request->post()) && $model->save()) { 
     return $this->redirect(['view', 'id' => $model->id]); 
    } else { 
     return $this->render('create', [ 
      'model' => $model, 
     ]); 
    } 
} 

/** 
* Updates an existing User model. 
* If update is successful, the browser will be redirected to the 'view' page. 
* @param string $id 
* @return mixed 
*/ 
public function actionUpdate($id) 
{ 
    $model = $this->findModel($id); 

    if ($model->load(Yii::$app->request->post()) && $model->save()) { 
     return $this->redirect(['view', 'id' => $model->id]); 
    } else { 
     return $this->render('update', [ 
      'model' => $model, 
     ]); 
    } 
} 

/** 
* Deletes an existing User model. 
* If deletion is successful, the browser will be redirected to the 'index' page. 
* @param string $id 
* @return mixed 
*/ 
public function actionDelete($id) 
{ 
    $this->findModel($id)->delete(); 

    return $this->redirect(['index']); 
} 

/** 
* Finds the User model based on its primary key value. 
* If the model is not found, a 404 HTTP exception will be thrown. 
* @param string $id 
* @return User the loaded model 
* @throws NotFoundHttpException if the model cannot be found 
*/ 
protected function findModel($id) 
{ 
    if (($model = User::findOne($id)) !== null) { 
     return $model; 
    } else { 
     throw new NotFoundHttpException('The requested page does not exist.'); 
    } 
} 
} 

User.php

<?php 

namespace app\models; 
use yii\base\NotSupportedException; 
use yii\db\ActiveRecord; 
use yii\helpers\Security; 
use yii\web\IdentityInterface; 
use yii\Log; 
use Yii; 

/** 
* This is the model class for table "user". 
* 
* @property string $id 
* @property string $uname 
* @property string $upassw 
* @property string $grade 
* @property string $last_login 
* 
*/ 
class User extends \yii\db\ActiveRecord implements IdentityInterface 
{ 
private $id; 
private $username; 
private $password; 
private $authKey; 
private $accessToken; 



/** 
* @inheritdoc 
*/ 
public static function tableName() 
{ 
    return 'user'; 
} 

/** 
* @inheritdoc 
*/ 
public function rules() 
{ 
    return [ 
     [['uname', 'upassw', 'grade', 'last_login'], 'required'], 
     [['grade'], 'integer'], 
     [['last_login'], 'safe'], 
     [['uname'], 'string', 'max' => 25], 
     [['upassw'], 'string', 'max' => 255] 
    ]; 
} 

/** 
* @inheritdoc 
*/ 
public function attributeLabels() 
{ 
    return [ 
     'id' => 'ID', 
     'uname' => 'Uname', 
     'upassw' => 'Upassw', 
     'grade' => 'Grade', 
     'last_login' => 'Last Login', 
    ]; 
} 

/** 
* @inheritdoc 
*/ 
public static function findIdentity($id) 
{ 
    return static::findOne($id); 

} 

/** 
* @inheritdoc 
*/ 
public static function findIdentityByAccessToken($token, $type = null) 
{ 
    return static::findOne(['access_token' => $token]); 

} 

/** 
* Finds user by username 
* 
* @param string  $username 
* @return static|null 
*/ 
public static function findByUsername($username) 
{ 
     return static::findOne(['uname' => $username]); 


} 



/** 
* @inheritdoc 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* @inheritdoc 
*/ 
public function getAuthKey() 
{ 
    return $this->authKey; 
} 

/** 
* @inheritdoc 
*/ 
public function validateAuthKey($authKey) 
{ 
    return $this->authKey === $authKey; 
} 

/** 
* Validates password 
* 
* @param string $password password to validate 
* @return boolean if password provided is valid for current user 
*/ 
public function validatePassword($password) 
{ 
    Yii::trace("something went wrong".$this->password." and ".$password); 
    return $this->password == sha1($password); 

} 

/** 

* Generates password hash from password and sets it to the model 

* 

* @param string $password 

*/ 

public function setPassword($password) 

{ 

    $this->password_hash = Security::generatePasswordHash($password); 

} 



/** 

* Generates "remember me" authentication key 

*/ 

public function generateAuthKey() 

{ 

    $this->auth_key = Security::generateRandomKey(); 

} 


} 

LoginForm

<?php 

namespace app\models; 

use Yii; 
use yii\base\Model; 
use yii\Log; 

/** 
* LoginForm is the model behind the login form. 
*/ 
class LoginForm extends Model 
{ 
public $username; 
public $password; 
public $rememberMe = true; 

private $_user = false; 


/** 
* @return array the validation rules. 
*/ 
public function rules() 
{ 
    return [ 
     // username and password are both required 
     [['username', 'password'], 'required'], 
     // rememberMe must be a boolean value 
     ['rememberMe', 'boolean'], 
     // password is validated by validatePassword() 
     ['password', 'validatePassword'], 
    ]; 
} 

/** 
* Validates the password. 
* This method serves as the inline validation for password. 
* 
* @param string $attribute the attribute currently being validated 
* @param array $params the additional name-value pairs given in the rule 
*/ 
public function validatePassword($attribute, $params) 
{ 
    if (!$this->hasErrors()) { 
     $user = $this->getUser(); 

     if (!$user || !$user->validatePassword($this->password)) { 
      $this->addError($attribute, 'Username o password non valide:user: '.$this->username.'  Pass:'.$this->password." ->Utente".$user->id); 
     } 


    } 
    else Yii::trace("error19"); 

} 

/** 
* Logs in a user using the provided username and password. 
* @return boolean whether the user is logged in successfully 
*/ 
public function login() 
{ 
    if ($this->validate()) { 
     return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0); 
    } else { 
     return false; 
    } 
} 

/** 
* Finds user by [[username]] 
* 
* @return User|null 
*/ 
public function getUser() 
{ 
    if ($this->_user === false) { 
     Yii::trace("username_login_requested".$this->username.var_dump($this->_user)); 

     $this->_user = User::findByUsername($this->username); 

    } 

    return $this->_user; 
} 
} 

SiteController

<?php 

namespace app\controllers; 

use Yii; 
use yii\filters\AccessControl; 
use yii\web\Controller; 
use yii\filters\VerbFilter; 
use app\models\LoginForm; 
use app\models\ContactForm; 

class SiteController extends Controller 
{ 
public function behaviors() 
{ 
    return [ 

     'verbs' => [ 
      'class' => VerbFilter::className(), 
      'actions' => [ 
       'logout' => ['post'], 
      ], 
     ], 
    ]; 
} 

public function actions() 
{ 
    return [ 
     'error' => [ 
      'class' => 'yii\web\ErrorAction', 
     ], 
     'captcha' => [ 
      'class' => 'yii\captcha\CaptchaAction', 
      'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, 
     ], 
    ]; 
} 

public function actionIndex() 
{ 
    return $this->render('index'); 
} 

public function actionLogin() 
{ 
    if (!\Yii::$app->user->isGuest) { 
     return $this->goHome(); 
    } 

    $model = new LoginForm(); 
    if ($model->load(Yii::$app->request->post()) && $model->login()) { 
     return $this->goHome(); 
    } else { 
     return $this->render('login', [ 
      'model' => $model, 
     ]); 
    } 
} 

public function actionLogout() 
{ 
    Yii::$app->user->logout(); 

    return $this->goHome(); 
} 

public function actionContact() 
{ 
    $model = new ContactForm(); 
    if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) { 
     Yii::$app->session->setFlash('contactFormSubmitted'); 

     return $this->refresh(); 
    } else { 
     return $this->render('contact', [ 
      'model' => $model, 
     ]); 
    } 
} 

public function actionAbout() 
{ 
    return $this->render('about'); 
} 


public function actionStart() 
{ 
    return $this->render('start'); 
} 
} 

login.php

<?php 
use yii\helpers\Html; 
use yii\bootstrap\ActiveForm; 





/* @var $this yii\web\View */ 
/* @var $form yii\bootstrap\ActiveForm */ 
/* @var $model app\models\LoginForm */ 

$this->title = 'Login'; 
$this->params['breadcrumbs'][] = $this->title; 
?> 
<div class="site-login"> 
    <h1><?= Html::encode($this->title) ?></h1> 

    <p>Please fill out the following fields to login:</p> 

    <?php $form = ActiveForm::begin([ 
     'id' => 'login-form', 
     'options' => ['class' => 'form-horizontal'], 
     'fieldConfig' => [ 
      'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>", 
     'labelOptions' => ['class' => 'col-lg-1 control-label'], 
    ], 
]); ?> 

<?= $form->field($model, 'username') ?> 

<?= $form->field($model, 'password')->passwordInput() ?> 

<?= $form->field($model, 'rememberMe', [ 
    'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>", 
])->checkbox() ?> 

<div class="form-group"> 
    <div class="col-lg-offset-1 col-lg-11"> 
     <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button'])   ?> 
    </div> 
</div> 

<?php ActiveForm::end(); ?> 
<?php 
var_dump(Yii::$app->user->identity); 
echo "<br>"; 
var_dump($_SESSION); 
var_dump(Yii::getVersion()); 
?> 

</div> 

감사합니다. 난 당신이 설정이 서로 다른 알고리즘을 사용하는 생각

public function validatePassword($password) 
{ 
    return $this->password == sha1($password); 

:이

public function setPassword($password) 
{ 
    $this->password_hash = Security::generatePasswordHash($password); 

} 

같은 암호를 설정하고이 같은 암호를 확인하기 때문에

답변

2

음 이유 중 하나 일 수 있습니다 확인. 나는 이것을 좋아한다.

$this->password_hash = Yii::$app->getSecurity()->generatePasswordHash($password); 

/** 
    * @param string $password password to validate 
    * @return bool if password provided is valid for current user 
    */ 
    public function validatePassword($password) 
    { 
     return Yii::$app->getSecurity()->validatePassword($password, $this->password); 
    }