The Problem:
Yii2 utilizes by default UserIdentity configured in config/web.php for connection, this object app
ly one table to authentication ('identityClass' => 'app\pa
inel\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;[...]
```
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' => '_pa
inelTeacher',
]
]
],
```
Note
that for each there is a identifyClass and one view login.
Now, we need to create the models:
```php
namespace app\models;
use Yii;[...]
return '{{%user}}';
}
//// to continues....
```
Model scho
lol:
```php
namespace app\models;
use Yii;
// My School[...]
return '{{%schoolUser}}';
}
//// to continues
....
```
Model Teacher:
```php
namespace app\models;
use Yii;
// My School[...]
return '{{%teacher}}';
}
//// to continues....
```[...]
```php
//behaviors of the school
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),[...]
];
}
```
To use login:
```php
//school
\Yii::$app->scholl->login($model, $this->rememberMe ? 3600*24*30 : 0);
//teacher
\Yii::$app->teacher->login($model, $this->rememberMe ? 3600*24*30 : 0);
```
For restrict access in views:
```php
<?php if (!\Yii::$app->scholl->isGuest):?>
<h1>My scho
lol Name: <?=\Yii::$app->scholl->identity->name?>
<?php endif;?>[...]
<h1>Teacher Name: <?=\Yii::$app->teacher->identity->name?>
<?php endif;?>
```
Always use the specific instance of the user you want to work with.
Criticism, suggestions and improvements, use the comments
[Agência Next4 Comunicação Digital Ltda.](https://www.next4.com.br/contato)