Difference between #32 and #33 of
Yii v2 snippet guide

Revision #33 has been created by rackycz on Sep 20, 2019, 11:03:31 AM with the memo:

db
« previous (#32) next (#34) »

Changes

Title unchanged

Yii v2 for beginners

Category unchanged

Tutorials

Yii version unchanged

2.0

Tags unchanged

tutorial,beginner,yii2

Content changed

[...]
- When creating the model, check this checkbox: **Enable I18N** ... we will need i18n later
- Note that there already is a model named User in the demo app. You will not need it if you want to use login-via-DB so feel free to check "Overwrite" in GII. The newly created User model will now be usable only for table operations, not for login. But we will enhance it, keep reading.
- The new model will only have 3 methods: tableName(), rules(), attributeLabels()
- In order to use the DB for login, we need to implement IdentityInterface which requires [5 new methods](https://www.yiiframework.com/doc/api/2.0/yii-web-identityinterface). But as I do not need all those methods, in some of them I have commented out their body in the example below.

The final model should look like this:
 
 
```PHP
 
<?php
 
 
namespace app\models;
 
 
use Yii;
 
 
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface {
 
 
    // When user detail is being edited we will only modify attribute password_new
 
    // Why? We dont want to load password-hash from DB and display it to the user
 
    // We only want him to see empty field and if it is filled in, password is changed on background
 
    public $password_new;
 
    public $password_new_repeat;
 
 
    // ----- Default 3 model-methods by GII:
 
    
 
    public static function tableName() {
 
        return 'user';
 
    }
 
 
    public function rules() {
 
        return [
 
            [['username', 'email'], 'required'],
 
            [['username', 'email'], 'string', 'max' => 45],
 
            ['email', 'email'],
 
            [['password', 'authKey'], 'string', 'max' => 60],
 
            [['password_new_repeat', 'password_new'], 'safe'],
 
            ['password_new_repeat', 'compare', 'operator' => '==', 'compareAttribute' => 'password_new'],
 
            ['password', 'setPasswordWhenChanged'],
 
        ];
 
    }
 
 
    public function attributeLabels() {
 
        return [
 
            'id' => Yii::t('app', 'ID'),
 
            'username' => Yii::t('app', 'Username'),
 
            'password' => Yii::t('app', 'Password'),
 
            'password_new' => Yii::t('app', 'New password'),
 
            'password_new_repeat' => Yii::t('app', 'Repeat new password'),
 
            'authKey' => Yii::t('app', 'Auth Key'),
 
            'email' => Yii::t('app', 'Email'),
 
        ];
 
    }
 
 
    // ----- Password validator
 
    
 
    public function setPasswordWhenChanged($attribute_name, $params) {
 
        
 
        if (trim($this->password_new_repeat) === "") {
 
            return true;
 
        }
 
        
 
        if ($this->password_new_repeat === $this->password_new) {
 
            $this->password = sha1($this->password_new_repeat);
 
        }
 
        
 
        return true;
 
    }
 
    
 
    // ----- IdentityInterface methods:
 
 
    public static function findIdentity($id) {
 
        return static::findOne($id);
 
    }
 
 
    public static function findIdentityByAccessToken($token, $type = null) {
 
        return static::findOne(['access_token' => $token]);
 
    }
 
 
    public function getId() {
 
        return $this->id;
 
    }
 
 
    public function getAuthKey() {
 
        return $this->authKey;
 
    }
 
 
    public function validateAuthKey($authKey) {
 
        return $this->authKey === $authKey;
 
    }
 
 
    // ----- Because of default LoginForm:
 
 
    public static function findByUsername($username) {
 
        return static::findOne(['username' => $username]);
 
    }
 
 
    public function validatePassword($password) {
 
        return $this->password === sha1($password);
 
    }
 
 
}
 
 
```
 
 

Login via database + Session
---
... text ...

Access rights
[...]
7 0
4 followers
Viewed: 252 230 times
Version: 2.0
Category: Tutorials
Written by: rackycz
Last updated by: rackycz
Created on: Sep 19, 2019
Last updated: 4 months ago
Update Article

Revisions

View all history