astrbot_plugin_imagestudio/backend/services/resource_access_service.py

92 行
3.5 KiB
Python

"""Owner-chain authorization for chat-facing ImageStudio resources.
Web administrators may be granted ``can_manage_all_profiles`` explicitly.
Regular AstrBot chat callers must own the profile at the end of the resource
chain; guessed workflow, node, image, or task IDs intentionally use not-found
semantics instead of revealing another user's data.
"""
from __future__ import annotations
from typing import Any
from backend.dto.common import ApiError, ErrorCode
from core.identity import RequestContext
from database.repositories.image_repository import ImageRepository
from database.repositories.job_repository import JobRepository
from database.repositories.profile_repository import ProfileRepository
from database.repositories.workflow_repository import WorkflowRepository
class ResourceAccessService:
def __init__(
self,
profile_repository: ProfileRepository,
workflow_repository: WorkflowRepository,
image_repository: ImageRepository,
job_repository: JobRepository,
):
self.profiles = profile_repository
self.workflows = workflow_repository
self.images = image_repository
self.jobs = job_repository
def require_profile(self, profile_id: str, ctx: RequestContext):
profile = self.profiles.get(profile_id)
if profile is None or profile.status == "deleted" or not self._can_access(profile.owner_user_id, ctx):
raise _not_found("character profile", profile_id)
return profile
def require_workflow(self, workflow_id: str, ctx: RequestContext):
workflow = self.workflows.get_workflow(workflow_id)
if workflow is None:
raise _not_found("workflow", workflow_id)
self.require_profile(workflow.profile_id, ctx)
return workflow
def require_node(self, node_id: str, ctx: RequestContext):
node = self.workflows.get_node(node_id)
if node is None:
raise _not_found("workflow node", node_id)
self.require_workflow(node.workflow_id, ctx)
return node
def require_image(self, image_id: str, ctx: RequestContext):
image = self.images.get(image_id)
if image is None:
raise _not_found("generation image", image_id)
if image.profile_id:
self.require_profile(image.profile_id, ctx)
elif image.workflow_id:
self.require_workflow(image.workflow_id, ctx)
else:
raise _not_found("generation image", image_id)
return image
def require_task(self, task_id: str, ctx: RequestContext):
task = self.jobs.get(task_id)
if task is None:
raise _not_found("generation task", task_id)
if task.profile_id:
self.require_profile(task.profile_id, ctx)
elif task.workflow_id:
self.require_workflow(task.workflow_id, ctx)
elif task.node_id:
self.require_node(task.node_id, ctx)
else:
raise _not_found("generation task", task_id)
return task
@staticmethod
def _can_access(owner_user_id: str | None, ctx: RequestContext) -> bool:
if ctx.has_permission("can_manage_all_profiles") or ctx.has_permission("*"):
return True
return bool(ctx.actor_id and owner_user_id and ctx.actor_id == owner_user_id)
def _not_found(resource: str, resource_id: str) -> ApiError:
return ApiError(
ErrorCode.RESOURCE_NOT_FOUND,
f"{resource} not found",
{"resource_id": str(resource_id)},
)