D
D
des1roer2015-06-25 12:16:06
Yii
des1roer, 2015-06-25 12:16:06

Yii 2 user authorization from database?

Perhaps it makes sense to use the built-in functionality.
here are the signal models

public function signup()
    {
        if ($this->validate()) {
            $user = new User();
            $user->username = $this->username;
            $user->email = $this->email;
            $user->setPassword($this->password);
            $user->generateAuthKey();
            if ($user->save()) {
                return $user;
            }
        }
        return null;
    }

but in the table the user has two fields password_hash and auth_key
but on the basis of what does he guess to create a hash and put the password in another field?
---
in general, I want the minimum at all - the ability to visually add users and establish their roles
more or less so that I figured it out. I try to repeat.
1. We make the email in the database optional.
2. create model in backend based on user table
3. create crud in backend
4. change crite to sign
yii2rbac\backend\views\user\create.php
<?php

use yii\helpers\Html;
use frontend\models\SignupForm;

/* @var $this yii\web\View */
/* @var $model backend\models\MyUser */

$this->title = 'Create My User';
$this->params['breadcrumbs'][] = ['label' => 'My Users', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="my-user-create">

    <h1><?= Html::encode($this->title) ?></h1>

    <?php
    $model = new SignupForm();
    if ($model->load(Yii::$app->request->post()))
    {
        if ($user = $model->signup())
        {
            if (Yii::$app->getUser()->login($user))
            {
                return $this->goHome();
            }
        }
    }

    echo $this->render('_signup', [
        'model' => $model,
    ])
    ?>

</div>

yii2rbac\backend\views\user\_signup.php
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;

/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */

$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;

?>
<div class="site-signup">
    <h1><?= Html::encode($this->title) ?></h1>

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

    <div class="row">
        <div class="col-lg-5">
            <?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>
                <?= $form->field($model, 'username') ?>
               
                <?= $form->field($model, 'password')->passwordInput() ?>
                <div class="form-group">
                    <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
                </div>
            <?php ActiveForm::end(); ?>
        </div>
    </div>
</div>

but
nothing \yii2rbac\backend\models\MyUser.php
public function signup()
    {
        if ($this->validate()) {
            $user = new User();
            $user->username = $this->username;
            //$user->email = $this->email;
            $this->password_hash = Yii::$app->security->generatePasswordHash($password);
            $this->auth_key = Yii::$app->security->generateRandomString();
            //$user->setPassword($this->password);
            //$user->generateAuthKey();
            if ($user->save()) {
                return $user;
            }
        }

        return null;
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
des1roer, 2015-06-26
@des1roer

the salvation of the drowning is the work of the drowning themselves

R
Rikcon, 2015-06-25
@Rikcon

What does it mean to create a hash and a password in another field?
$user->setPassword($this->password); This is what creates a hash and puts it in password_hash.
$user->generateAuthKey(); This is what generates AuthKey (this is for the Remember Me checkbox, it still seems to put it in the cookie for the user)
Or did I not understand the question?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question