多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
> YII2默认生成的登录案例里用的是user表,只是给出一个案例。那么如果我们要用到后台管理员登录、商家中心登录呢? > 那么我们需要另外建各自的表,如admin表、seller表等。下面我以管理员后台登录为例进行讲解。 [TOC] #### 1.创建admin表 > 参考yii自带的用户表结构(自带的用户表参考console\migrations\xxx_init.php文件)创建如下数据表 ~~~ CREATE TABLE `admin` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL, `auth_key` VARCHAR(32) COLLATE utf8_unicode_ci NOT NULL, `password_hash` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL, `password_reset_token` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL, `email` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL, `status` SMALLINT(6) NOT NULL DEFAULT '10', `created_at` INT(11) NOT NULL, `updated_at` INT(11) NOT NULL, `access_token` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `username` (`username`), UNIQUE KEY `email` (`email`), UNIQUE KEY `password_reset_token` (`password_reset_token`) ) ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ~~~ #### 2.拷贝LoginForm >打开common\models\LoginForm.php文件,找到getUser方法,这里的User类使用的还是common\models\User类,改为Admin类。 ~~~ protected function getUser() { if ($this->_user === null) { $this->_user = Admin::findByUsername($this->username); } return $this->_user; } ~~~ #### 3.拷贝User类,并改为Admin类 >打开backend\models\Admin.php文件,该类至少需要实现接口 yii\web\IdentityInterface 几个抽象方法才可以,可以从common\models\User.php里拷贝相应代码。 ~~~ <?php namespace backend\models; use Yii; use yii\web\IdentityInterface; /** * This is the model class for table "user_backend". * * @property integer $id * @property string $username * @property string $auth_key * @property string $password_hash * @property string $email * @property string $created_at * @property string $updated_at */ class Admin extends \yii\db\ActiveRecord implements IdentityInterface { const STATUS_DELETED = 0; const STATUS_ACTIVE = 10; // 其他gii生成的代码,因为我们并未对其进行过改动,因此这里省略,下面只补充我们实现的几个抽象方法 /** * @inheritdoc * 根据admin表的主键(id)获取用户 */ public static function findIdentity($id) { return static::findOne(['id' => $id]); } /** * @inheritdoc * 根据access_token获取用户,我们暂时先不实现 */ public static function findIdentityByAccessToken($token, $type = null) { return static::findOne(['access_token' => $token]); //throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); } /** * Finds user by username * * @param string $username * @return static|null */ public static function findByUsername($username) { return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); } /** * Finds user by password reset token * * @param string $token password reset token * @return static|null */ public static function findByPasswordResetToken($token) { if (!static::isPasswordResetTokenValid($token)) { return null; } return static::findOne([ 'password_reset_token' => $token, 'status' => self::STATUS_ACTIVE, ]); } /** * Finds out if password reset token is valid * * @param string $token password reset token * @return boolean */ public static function isPasswordResetTokenValid($token) { if (empty($token)) { return false; } $timestamp = (int) substr($token, strrpos($token, '_') + 1); $expire = Yii::$app->params['user.passwordResetTokenExpire']; return $timestamp + $expire >= time(); } /** * @inheritdoc * 用以标识 Yii::$app->user->id 的返回值 */ public function getId() { return $this->getPrimaryKey(); } /** * @inheritdoc * 获取auth_key */ public function getAuthKey() { return $this->auth_key; } /** * @inheritdoc * 验证auth_key */ public function validateAuthKey($authKey) { return $this->getAuthKey() === $authKey; } /** * Validates password * * @param string $password password to validate * @return boolean if password provided is valid for current user */ public function validatePassword($password) { return Yii::$app->security->validatePassword($password, $this->password_hash); } /** * Generates password hash from password and sets it to the model * * @param string $password */ public function setPassword($password) { $this->password_hash = Yii::$app->security->generatePasswordHash($password); } /** * Generates "remember me" authentication key */ public function generateAuthKey() { $this->auth_key = Yii::$app->security->generateRandomString(); } /** * Generates new password reset token */ public function generatePasswordResetToken() { $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time(); } /** * Removes password reset token */ public function removePasswordResetToken() { $this->password_reset_token = null; } } ~~~ #### 4.修改配置文件,identityClass指向Admin类 >打开backend\config\main.php, 找到components user这里,修改backend\models\Admin类为我们的认证类 ~~~ 'components' => [ 'user' => [ 'identityClass' => 'backend\models\Admin', 'enableAutoLogin' => true, ], ], ~~~