Answer the question
In order to leave comments, you need to log in
Why does authorization in Yii2 through the database not work?
I'm trying to implement user authorization on the site in YII2 template -advanced
Form
<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
<input type="hidden" name="<?=Yii::$app->request->csrfParam; ?>" value="<?=Yii::$app->request->getCsrfToken(); ?>" />
<?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>
<?= $form->field($model, 'pass')->passwordInput() ?>
<div style="color:#999;margin:1em 0">
If you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>.
</div>
<div class="form-group">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
<?php ActiveForm::end(); ?>
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new UserIdentity();
if ($model->load(Yii::$app->request->post())
&& $model->login()) {
return $this->goBack();
}
return $this->render('login', [
'model' => $model,
]);
}
<?php
namespace frontend\models;
use Yii;
/**
* This is the model class for table "User".
*
* @property string $username
* @property string $email
* @property string $phone
* @property string $ava
* @property integer $id
* @property string $auth_key
* @property string $token
* @property string $status
* @property string $pass
*/
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
const DEFAULT_STATUS = 0;
const DEFAULT_ROLE = 'user';
/**
* @inheritdoc
*/
public static function tableName()
{
return 'User';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['username', 'email', 'phone', 'ava', 'id', 'auth_key', 'token', 'status', 'pass'], 'required'],
[['username', 'email', 'phone', 'ava', 'status', 'pass'], 'string'],
[['id'], 'integer'],
[['auth_key', 'token'], 'string', 'max' => 32],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'username' => 'Username',
'email' => 'Email',
'phone' => 'Phone',
'ava' => 'Ava',
'id' => 'ID',
'auth_key' => 'Auth Key',
'token' => 'Token',
'status' => 'Status',
'pass' => 'Pass',
];
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
public function validatePassword($password)
{
return \Yii::$app->security->validatePassword($password, $this->password);
}
public function getId()
{
return $this->id;
}
public static function findIdentityByAccessToken($token, $type = null)
{
}
public function getAuthKey()
{
}
public function validateAuthKey($authKey)
{
}
public function login()
{
if ($this->validate()) {
return Yii::$app->UserIdentity->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
}
return false;
}
}</spoiler>
<?php
namespace frontend\models;
class UserIdentity extends User implements \yii\web\IdentityInterface
{
private $pass;
public static function findIdentity($id)
{
return static::findOne($id);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['token' => $token]);
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->pass === md5($password);
}
}
Answer the question
In order to leave comments, you need to log in
In the validation rules, you check for extra attributes and require them to be filled in.
In fact, you are only passing username and pass !
That's why the form is updated and no errors are visible, since there are no fields in the form for the remaining attributes being checked.
Remove the excess and continue testing.
ps
Change this line There is a special method
in yii2 for encryption .
slo_nik , skrinshoter.ru/s/180218/TQjFept7?a script errors from YII JS
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question