How to login from different tables in Yii2

You are viewing revision #1 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#2) »

The Problem: Yii2 utilizes by default UserIdentity configured in config/web.php for connection, this object appy one table to authentication ('identityClass' => 'app\painel\models\User'). How to authentication from diferent tables? Solution: Create instances in web.php to uses UserIdentify. eg: `php $user = \Yii::$app->user;
$school = \Yii::$app->school; $teacher = \Yii::$app->teacher; `

My config/web.php `php

 'user' => [
        'class'=>'yii\web\User',
        'identityClass' => 'app\models\User',
        'enableAutoLogin' => false,
        'authTimeout' => 60*30,
        'loginUrl' => ['dashboard/login'],
        'identityCookie' => [
            'name' => '_panelUser',
        ]
    ],
    'school'=>[
        'class'=>'yii\web\User',
        'identityClass' => 'app\models\SchoolUser',
        'enableAutoLogin' => false,
        'authTimeout' => 60*30,
        'loginUrl' => ['dashboard-school/login'],
        'identityCookie' => [
            'name' => '_panelSchool',
        ]
    ],
    'teacher'=>[
        'class'=>'yii\web\User',
        'identityClass' => 'app\models\TeacherUser',
        'enableAutoLogin' => false,
        'authTimeout' => 60*30,
        'loginUrl' => ['dashboard-teacher/login'],
        'identityCookie' => [
            'name' => '_painelTeacher',
        ]
    ],
Note for each there is a identifyClass and one view login.
Now, we need to create the models:
```php
namespace app\models;
use Yii;
// My user
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
    public static function tableName()
    {
        return '{{%user}}';
    }
    //// to continues....

Model scholl: `php namespace app\models; use Yii; // My School class SchoolUser' extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface {

public static function tableName()
{
    return '{{%schoolUser}}';
}
//// to continues....
Model Teacher:

```php
namespace app\models;
use Yii;
// My School
class TeacherUser'' extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
    public static function tableName()
    {
        return '{{%teacher}}';
    }
    //// to continues....

Now In my example I want to have controllers for each type of access, without generating conflicts between them:

In Behavior of the controller i have defined for dashboard-school, teacher and user, the user object representing the authentication status or the ID of the user application component.

//behaviors of the school
public function behaviors()
{

    return [
        'access' => [
            'class' => AccessControl::className(),
            'user'=>'school', // this user object defined in web.php
            'rules' => [
                [
                    'allow' => true,
                    'roles' => ['@'],
                ],
                [
                    'allow' => true,
                    'actions' => ['login'],                    
 'roles' => ['?'],

                ],
            ],
        ]
    ];
}

To use login: `php //school \Yii::$app->scholl->login($model, $this->rememberMe ? 36002430 : 0);

//teacher \Yii::$app->teacher->login($model, $this->rememberMe ? 36002430 : 0);

For restrict access in views:

```php
<?php if (!\Yii::$app->scholl->isGuest):?>
<h1>My scholl Name: <?=\Yii::$app->scholl->identity->name?>
<?php endif;?>

<?php if (!\Yii::$app->teacher->isGuest):?>
<h1>Teacher Name: <?=\Yii::$app->teacher->identity->name?>
<?php endif;?>

Always use the specific instance of the user you want to work with.

3 0
3 followers
Viewed: 5 899 times
Version: 2.0
Category: How-tos
Written by: AndroideLP
Last updated by: samdark
Created on: Apr 3, 2018
Last updated: 2 months ago
Update Article

Revisions

View all history

Related Articles