fastAdmin 框架api模块认证问题

fastAdmin 自带三个模块分别是:admin模块,api模块,index前台模块

admin模块使用app\admin\library\Auth.php作为认证基类,在app\common\controller\Backend.php中被定义

api模块和index模块使用app\common\library\Auth.php作为认证基类,在app\common\controller\Api.php和Frontend.php中被定义

那么就会出现一个问题,api应用和index应用只能认证会员,admin应用只能认证管理员。对于一下简单应用,我想直接已管理员身份调用api,就需要重写api模块认证基类。

配置app\common\controller\Api.php

//重写的api认证类
namespace app\common\controller;

use think\Hook;
use think\Lang;
use think\Loader;
use think\Request;
use think\Response;
use think\Route;
use think\Validate;
use app\common\library\AdminAuth;
use think\exception\ValidateException;
use think\exception\HttpResponseException;

/**
 * API控制器基类
 */
class Api{ 
 
   /**
     * 初始化操作
     * @access protected
     */
    protected function _initialize(){
        /*
         * 初始化认证基类 修改为AdminAuth
         */
        $this->auth = AdminAuth::instance();
    }
}

实现app\common\controller

// 辅助app\common\controller\Auth.php
// 修改init() 和 direct()方法
namespace app\common\library;

use think\Db;
use think\Hook;
use fast\Random;
use think\Config;
use think\Request;
use think\Validate;
use think\Exception;
use app\admin\model\Admin;

/**
 * Auth认证类
 */
class AdminAuth{
  
    /**
     * 根据Token初始化
     *
     * @param string $token Token
     * @return boolean
     */
    public function init($token){
        if ($this->_logined) {
            return true;
        }
        if ($this->_error) {
            return false;
        }
        $data = Token::get($token);
        if (!$data) {
            return false;
        }
        $user_id = intval($data['user_id']);
        
        if ($user_id > 0) {
           // 修改下面代码
            $user = Admin::get($user_id);
            if (!$user) {
                $this->setError('Account not exist');
                return false;
            }
            if ($user['status'] != 'normal') {
                $this->setError('Account is locked');
                return false;
            }
            $this->_user = $user;
            $this->_logined = true;
            $this->_token = $token;
            //初始化成功的事件
            Hook::listen("user_init_successed", $this->_user);
            return true;
        } else {
            $this->setError('You are not logged in');
            return false;
        }
    }

    /**
     * 直接登录账号
     * @param int $user_id
     * @return boolean
     */
    public function direct($user_id){
        $user = Admin::get($user_id);
        if ($user) {
            Db::startTrans();
            try {
                $ip = request()->ip();
                $time = time();
                // 注释下面代码
                // if ($user->logintime < \fast\Date::unixtime('day')) {
                //     $user->successions = $user->logintime < \fast\Date::unixtime('day', -1) ? 1 : $user->successions + 1;
                //     $user->maxsuccessions = max($user->successions, $user->maxsuccessions);
                // }
                // $user->prevtime = $user->logintime;
                // //记录本次登录的IP和时间
                // $user->loginip = $ip;
                // $user->logintime = $time;
                //重置登录失败次数
                $user->loginfailure = 0;
                $user->save();
                $this->_user = $user;
                $this->_token = Random::uuid();
                Token::set($this->_token, $user->id, $this->keeptime);
                $this->_logined = true;
                //登录成功的事件
                Hook::listen("user_login_successed", $this->_user);
                Db::commit();
            } catch (Exception $e) {
                Db::rollback();
                $this->setError($e->getMessage());
                return false;
            }
            return true;
        } else {
            return false;
        }
    }
}
This entry was posted in php. Bookmark the permalink.

发表回复