Hi,
I am using Yii 2 basic application template on localhost. I have two tables
- admin
- employee
My need is how to login from different tables in Yii2. I referred the link https://www.yiiframework.com/wiki/864/how-to-login-from-different-tables-in-yii2.
So in my config I have foll:
'admin' => [
'class'=>'yii\web\User',
'identityClass' => 'app\models\Admin',
'enableSession' => true,
],
'employee' => [
'class'=>'yii\web\User',
'identityClass' => 'app\models\Employee',
'enableSession' => true,
],
Is this configuration correct? I have correctly implemented Identity Interface methods in both Admin and Employee models as follows:
- Admin model
<?php
namespace app\models;
use Yii;
use app\models\Admin;
use yii\web\IdentityInterface;
class Admin extends \yii\db\ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return 'admin';
}
public function rules()
{
return [
[['Name', 'username', 'password', 'Status'], 'required'],
[['Name'], 'string', 'max' => 25],
[['username', 'password', 'Status'], 'string', 'max' => 20],
[['username'], 'unique'],
];
}
public function attributeLabels()
{
return [
'AdminId' => 'Admin ID',
'Name' => 'Name',
'username' => 'Username',
'password' => 'Password',
'Status' => 'Status',
];
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException();
}
public function getId()
{
return $this->AdminId;
}
public function getAuthKey()
{
throw new NotSupportedException();
}
public function validateAuthKey($authKey)
{
throw new NotSupportedException();
}
public static function findByUsername($username){
return self::findOne(['username'=>\yii\helpers\Html::encode($username)]);
}
public function validatePassword($password){
return $this->password === \yii\helpers\Html::encode($password);
}
}
- Employee model
<?php
namespace app\models;
use Yii;
use app\models\Employee;
use yii\web\IdentityInterface;
class Employee extends \yii\db\ActiveRecord implements IdentityInterface
{
public static function tableName()
{
return 'employee';
}
public function rules()
{
return [
[['FirstName', 'LastName', 'Age', 'username', 'password'], 'required'],
[['Age'], 'integer'],
[['FirstName', 'LastName'], 'string', 'max' => 20],
[['username', 'password'], 'string', 'max' => 25],
[['username'], 'unique'],
];
}
public function attributeLabels()
{
return [
'EmpId' => 'Emp ID',
'FirstName' => 'First Name',
'LastName' => 'Last Name',
'Age' => 'Age',
'username' => 'Username',
'password' => 'Password',
];
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException();
}
public function getId()
{
return $this->EmpId;
}
public function getAuthKey()
{
throw new NotSupportedException();
}
public function validateAuthKey($authKey)
{
throw new NotSupportedException();
}
public static function findByUsername($username){
return self::findOne(['username'=>\yii\helpers\Html::encode($username)]);
}
public function validatePassword($password){
return $this->password === \yii\helpers\Html::encode($password);
}
}
Now I require that from one login form if the entered username and password are correct and from admin table, then message should be displayed as Logged in from Admin table.
If username and password are correct and from employee table, then message should be displayed as Logged In from Employee table.
What changes should I make in LoginForm model. I have the below LoginForm Model
<?php
namespace app\models;
use Yii;
use yii\base\Model;
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
public function rules()
{
return [
[['username', 'password'], 'required'],
['rememberMe', 'boolean'],
['password', 'validatePassword'],
];
}
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getAdmin();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
public function login()
{
if ($this->validate()) {
return Yii::$app->admin->login($this->getAdmin(), $this->rememberMe ? 3600*24*30 : 0);
}
return false;
}
public function getAdmin()
{
if ($this->_user === false) {
$this->_user = Admin::findByUsername($this->username);
}
return $this->_user;
}
}
After doing this all, I get the foll error as
# Invalid Configuration – [yii\base\InvalidConfigException](http://www.yiiframework.com/doc-2.0/yii-base-invalidconfigexception.html)
## User::identityClass must be set.