Files
ctms/backend/app/core/permission_cache.py
T
Cheng Zhou 20ce6bccef 完全移除模块级权限系统,迁移至接口级权限
后端:
- 删除 StudyRolePermission 模型和 project_permissions API 文件
- 重写 project_permissions.py core,移除所有模块级权限函数
- 重写 permission_cache.py,移除模块级权限缓存逻辑
- 新增权限模板功能(PermissionTemplate 模型、API、服务层)
- 新增 permission_templates 数据库迁移
- 迁移 8 个模块(attachments、audit_logs、dashboard、faqs、
  faq_categories、fees_attachments、knowledge_notes、
  material_equipments、overview、subject_histories、subject_pds)
  至接口级权限检查
- 删除所有模块级权限相关测试文件,新增权限模板测试

前端:
- 删除 ProjectPermissions.vue 和 ProjectPermissionsModule.vue
- 重写 projectRoutePermissions.ts,改为基于接口级权限格式
- 更新 store/study.ts、router/index.ts、AuditLogs.vue、Projects.vue
  中的权限 API 调用,从 fetchProjectRolePermissions 改为
  fetchApiEndpointPermissions
- 清理 types/api.ts 中的旧模块级权限类型定义
- 新增 PermissionTemplateSelector.vue 组件
- 更新权限管理页面,移除模块级权限 tab

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-15 09:07:43 +08:00

96 lines
3.1 KiB
Python

"""权限缓存管理器"""
from __future__ import annotations
import time
import uuid
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession
class PermissionCache:
"""权限缓存管理器"""
def __init__(self, default_ttl: int = 300):
self.default_ttl = default_ttl
self._project_permissions_cache: dict[str, tuple[Any, float]] = {}
self._member_role_cache: dict[str, tuple[str | None, float]] = {}
def _is_expired(self, timestamp: float, ttl: int) -> bool:
return time.time() - timestamp > ttl
def _make_project_cache_key(self, study_id: uuid.UUID) -> str:
return f"project_permissions:{study_id}"
def _make_member_cache_key(self, study_id: uuid.UUID, user_id: uuid.UUID) -> str:
return f"member_role:{study_id}:{user_id}"
async def get_member_role(
self,
db: AsyncSession,
study_id: uuid.UUID,
user_id: uuid.UUID,
ttl: int | None = None,
) -> str | None:
"""获取成员角色(带缓存)"""
from app.crud import member as member_crud
if ttl is None:
ttl = self.default_ttl
cache_key = self._make_member_cache_key(study_id, user_id)
if cache_key in self._member_role_cache:
cached_role, timestamp = self._member_role_cache[cache_key]
if not self._is_expired(timestamp, ttl):
return cached_role
membership = await member_crud.get_member(db, study_id, user_id)
role = membership.role_in_study if membership and membership.is_active else None
self._member_role_cache[cache_key] = (role, time.time())
return role
def invalidate_project_permissions(self, study_id: uuid.UUID) -> None:
cache_key = self._make_project_cache_key(study_id)
self._project_permissions_cache.pop(cache_key, None)
def invalidate_member_role(self, study_id: uuid.UUID, user_id: uuid.UUID) -> None:
cache_key = self._make_member_cache_key(study_id, user_id)
self._member_role_cache.pop(cache_key, None)
def invalidate_all_member_roles(self, study_id: uuid.UUID) -> None:
keys_to_delete = [
key for key in self._member_role_cache.keys()
if key.startswith(f"member_role:{study_id}:")
]
for key in keys_to_delete:
del self._member_role_cache[key]
def clear_all(self) -> None:
self._project_permissions_cache.clear()
self._member_role_cache.clear()
def get_cache_stats(self) -> dict[str, Any]:
return {
"project_permissions_count": len(self._project_permissions_cache),
"member_role_count": len(self._member_role_cache),
"total_count": len(self._project_permissions_cache) + len(self._member_role_cache),
}
_permission_cache: PermissionCache | None = None
def get_permission_cache() -> PermissionCache:
global _permission_cache
if _permission_cache is None:
_permission_cache = PermissionCache()
return _permission_cache
def set_permission_cache(cache: PermissionCache) -> None:
global _permission_cache
_permission_cache = cache