PermissionChecker

Trait PermissionChecker 

Source
pub trait PermissionChecker: Send + Sync {
    // Required methods
    fn has_permission<'life0, 'life1, 'life2, 'async_trait>(
        &'life0 self,
        login_id: &'life1 str,
        permission: &'life2 str,
    ) -> Pin<Box<dyn Future<Output = Result<bool, SaTokenError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             Self: 'async_trait;
    fn get_permissions<'life0, 'life1, 'async_trait>(
        &'life0 self,
        login_id: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<Vec<String>, SaTokenError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;

    // Provided methods
    fn has_all_permissions<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        login_id: &'life1 str,
        permissions: &'life2 [&'life3 str],
    ) -> Pin<Box<dyn Future<Output = Result<bool, SaTokenError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             Self: 'async_trait { ... }
    fn has_any_permission<'life0, 'life1, 'life2, 'life3, 'async_trait>(
        &'life0 self,
        login_id: &'life1 str,
        permissions: &'life2 [&'life3 str],
    ) -> Pin<Box<dyn Future<Output = Result<bool, SaTokenError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             'life2: 'async_trait,
             'life3: 'async_trait,
             Self: 'async_trait { ... }
}
Expand description

权限检查器 | Permission Checker

用于检查用户权限的 trait Trait for checking user permissions

§使用示例 | Usage Example

use async_trait::async_trait;
use sa_token_core::PermissionChecker;
 
struct MyPermissionChecker;
 
#[async_trait]
impl PermissionChecker for MyPermissionChecker {
    async fn has_permission(&self, login_id: &str, permission: &str) -> SaTokenResult<bool> {
        // 从数据库查询权限 | Query permission from database
        Ok(true)
    }
     
    async fn get_permissions(&self, login_id: &str) -> SaTokenResult<Vec<String>> {
        // 返回用户所有权限 | Return all user permissions
        Ok(vec!["read".to_string(), "write".to_string()])
    }
}

Required Methods§

Source

fn has_permission<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, login_id: &'life1 str, permission: &'life2 str, ) -> Pin<Box<dyn Future<Output = Result<bool, SaTokenError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, Self: 'async_trait,

检查用户是否拥有指定权限 | Check if User Has Specific Permission

§参数 | Parameters
  • login_id: 登录 ID | Login ID
  • permission: 权限标识(如 “user:read”, “admin:”)| Permission identifier (e.g., “user:read”, “admin:”)
§返回 | Returns
  • Ok(true): 用户拥有该权限 | User has the permission
  • Ok(false): 用户没有该权限 | User doesn’t have the permission
Source

fn get_permissions<'life0, 'life1, 'async_trait>( &'life0 self, login_id: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Vec<String>, SaTokenError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

获取用户的所有权限列表 | Get All User Permissions

§参数 | Parameters
  • login_id: 登录 ID | Login ID
§返回 | Returns

用户的权限列表 | User’s permission list

Provided Methods§

Source

fn has_all_permissions<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, login_id: &'life1 str, permissions: &'life2 [&'life3 str], ) -> Pin<Box<dyn Future<Output = Result<bool, SaTokenError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, Self: 'async_trait,

检查用户是否拥有所有指定权限(AND 逻辑) Check if User Has All Specified Permissions (AND logic)

§参数 | Parameters
  • login_id: 登录 ID | Login ID
  • permissions: 权限列表 | Permission list
§返回 | Returns

只有当用户拥有所有权限时才返回 true Returns true only when user has all permissions

Source

fn has_any_permission<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, login_id: &'life1 str, permissions: &'life2 [&'life3 str], ) -> Pin<Box<dyn Future<Output = Result<bool, SaTokenError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait, Self: 'async_trait,

检查用户是否拥有任一指定权限(OR 逻辑) Check if User Has Any Specified Permission (OR logic)

§参数 | Parameters
  • login_id: 登录 ID | Login ID
  • permissions: 权限列表 | Permission list
§返回 | Returns

只要用户拥有任一权限就返回 true | Returns true if user has any of the permissions

Implementors§