Skip to main content

AddonsBaseController

Trait AddonsBaseController 

Source
pub trait AddonsBaseController: BaseController {
    // Provided methods
    fn allow_all_action(&self) -> Vec<&'static str> { ... }
    fn parse_route_info(&self, uri: &str) -> RouteInfo { ... }
    fn check_login(
        &self,
        route_uri: &str,
        user_is_login: bool,
    ) -> Result<(), String> { ... }
    fn get_token(
        &self,
        authorization: Option<&str>,
    ) -> Result<Option<UserInfo>, String> { ... }
}
Expand description

addons 控制器基础 trait(对齐 PHP addons\BaseController

PHP 继承链:BaseController → SzController → AddonsBaseController → 业务控制器。 Rust 等价:AddonsBaseController: BaseController: SzController(trait 继承链)。

§PHP 对齐

PHP 属性/方法Rust 等价
protected $userhandler 中通过 AddonsBaseController::get_token 获取 UserInfo
protected string $controllerRouteInfo::controller(由 AddonsBaseController::parse_route_info 解析)
protected string $actionRouteInfo::action
protected string $routeUriRouteInfo::route_uri
protected string $groupRouteInfo::group
protected array $allowAllActionAddonsBaseController::allow_all_action
public function initialize()由 handler 显式调用 parse_route_info + get_token + check_login
public function getToken()AddonsBaseController::get_token(占位,JWT 完整实现)
protected function getRouteinfo()AddonsBaseController::parse_route_info
private function checkLogin()AddonsBaseController::check_login

§状态迁移说明

PHP AddonsBaseController::initialize() 在构造函数中自动调用,执行 getRouteinfo() + getToken() + checkLogin()。Rust 控制器无状态, handler 需显式调用这三个方法(顺序:parse_route_info → get_token → check_login)。

Provided Methods§

Source

fn allow_all_action(&self) -> Vec<&'static str>

登录验证白名单(对齐 PHP protected array $allowAllAction

默认包含 /passport/login/task/task/userClerk。 子类可覆盖以添加更多白名单路径。

Source

fn parse_route_info(&self, uri: &str) -> RouteInfo

解析路由信息(对齐 PHP protected function getRouteinfo()

从 URI 路径解析 controller/action/group/route_uri。

§解析规则
  • URI 形如 /controller/action:controller=controller, action=action, group=controller
  • URI 形如 /group/controller/action:controller=group/controller, action=action, group=group/controller
  • URI 形如 /controller:controller=controller, action=“”, group=controller
  • URI 形如 /:所有字段为空字符串
§PHP 对齐(含 bug 复刻)
protected function getRouteinfo(): void {
    $this->controller = toUnderScore(Request()->controller());
    $this->controller = str_replace(".", "/", $this->controller);
    $this->controller = str_replace("_", "", $this->controller);
    $this->action = Request()->action();
    // ⚠️ PHP bug:str_replace 已将 "." 替换为 "/",但 strstr 仍以 "." 为分隔符,
    // 永远返回 false,因此 $this->group === $this->controller(始终相等)。
    $groupstr = strstr($this->controller, '.', true);
    $this->group = $groupstr !== false ? $groupstr : $this->controller;
    $this->routeUri = '/' . $this->controller . '/' . $this->action;
}

PHP bug 复刻说明:Rust 严格对齐 PHP 行为,group 字段始终等于 controller。 经核查 PHP 后端全部源码,$this->group 为只写不读的死字段,bug 不暴露, 但为了 R5(PHP/Rust 行为对比)严格一致性,仍复刻此行为。

注意:PHP 使用 ThinkPHP 的 Request()->controller() 获取控制器名, Rust 直接从 URI 路径解析(已通过路由匹配)。

Source

fn check_login( &self, route_uri: &str, user_is_login: bool, ) -> Result<(), String>

检查登录状态(对齐 PHP private function checkLogin()

§行为
  1. route_uri 在白名单中,返回 Ok(())
  2. user_is_login == true,返回 Ok(())
  3. 否则返回 Err("not_login")
§PHP 对齐
private function checkLogin(): void {
    if (in_array($this->routeUri, $this->allowAllAction)) {
        return;
    }
    if(!empty($this->user)){
        if($this->user['is_login'] == 1){
            return;
        }
    }
    throw new BaseException(['code' => -1, 'msg' => 'not_login']);
}
Source

fn get_token( &self, authorization: Option<&str>, ) -> Result<Option<UserInfo>, String>

获取 token 用户信息(对齐 PHP public function getToken()

§实现说明

使用 sz-orm-auth 的 JwtEncoder 进行 HS256 签名验证与过期检查, 配置项(SZ_JWT_SECRET / SZ_JWT_ISSUER)在启动时从环境变量读取。 核心验证逻辑见 verify_token_with_config,便于单元测试注入配置。

验证流程(对齐 PHP Token::getUserId):

  1. 提取 Authorization header 中的 Bearer token
  2. 通过 JwtEncoder::decode 验证签名 + 过期时间
  3. 验证 iss 字段匹配配置的签发人
  4. 提取 user_id claim 返回 UserInfo
§PHP 对齐
public function getToken(){
    if (!$token = Token::getUserId(request()->header('Authorization'))) {
        if(in_array($this->routeUri, $this->allowAllAction)) {
            return true;
        } else {
            throw new BaseException(['msg' => '缺少必要的参数,请重新登陆!']);
        }
    }
    return $token;
}
§参数
  • authorizationAuthorization 请求头的值(如 "Bearer xxx.yyy.zzz"
§返回
  • Ok(Some(UserInfo)):JWT 验证成功,返回用户信息
  • Ok(None):无 token、token 为空、签名密钥未配置或验证失败 (调用方根据 route_uri 决定是否抛错,对齐 PHP if (!$token) 分支)
  • Err(String):JWT 解析过程中出现异常(如格式错误)

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§