|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | +from django.contrib.auth.models import AnonymousUser |
| 4 | +from django.http import HttpRequest |
| 5 | + |
| 6 | + |
| 7 | +class BasePermission(ABC): |
| 8 | + """ |
| 9 | + Base class for implementing custom permission checks on agents. |
| 10 | +
|
| 11 | + Subclass this to create your own permission logic for controlling |
| 12 | + who can execute agents in your application. |
| 13 | + """ |
| 14 | + |
| 15 | + @abstractmethod |
| 16 | + def has_permission(self, request: HttpRequest, agent_slug: str, **kwargs) -> bool: |
| 17 | + """ |
| 18 | + Check if the request has permission to execute the agent. |
| 19 | +
|
| 20 | + Args: |
| 21 | + request: The Django HTTP request object |
| 22 | + agent_slug: The slug of the agent being executed |
| 23 | + **kwargs: Additional context that may be useful for permission checks |
| 24 | +
|
| 25 | + Returns: |
| 26 | + bool: True if permission is granted, False otherwise |
| 27 | + """ |
| 28 | + pass |
| 29 | + |
| 30 | + def get_permission_denied_message( |
| 31 | + self, request: HttpRequest, agent_slug: str |
| 32 | + ) -> str: |
| 33 | + """ |
| 34 | + Return a custom message when permission is denied. |
| 35 | +
|
| 36 | + Override this method to provide specific error messages. |
| 37 | +
|
| 38 | + Args: |
| 39 | + request: The Django HTTP request object |
| 40 | + agent_slug: The slug of the agent being executed |
| 41 | +
|
| 42 | + Returns: |
| 43 | + str: The permission denied message |
| 44 | + """ |
| 45 | + return f"You do not have permission to execute agent '{agent_slug}'" |
| 46 | + |
| 47 | + |
| 48 | +class AllowAny(BasePermission): |
| 49 | + """ |
| 50 | + Permission that allows all requests. |
| 51 | +
|
| 52 | + This is the default behavior if no permission is specified. |
| 53 | + """ |
| 54 | + |
| 55 | + def has_permission(self, request: HttpRequest, agent_slug: str, **kwargs) -> bool: |
| 56 | + return True |
| 57 | + |
| 58 | + |
| 59 | +class IsAuthenticated(BasePermission): |
| 60 | + """ |
| 61 | + Permission that requires the user to be authenticated. |
| 62 | + """ |
| 63 | + |
| 64 | + def has_permission(self, request: HttpRequest, agent_slug: str, **kwargs) -> bool: |
| 65 | + return bool(request.user and request.user.is_authenticated) |
| 66 | + |
| 67 | + def get_permission_denied_message( |
| 68 | + self, request: HttpRequest, agent_slug: str |
| 69 | + ) -> str: |
| 70 | + return "Authentication required to execute this agent" |
| 71 | + |
| 72 | + |
| 73 | +class DjangoPermission(BasePermission): |
| 74 | + """ |
| 75 | + Permission that uses Django's built-in permission system. |
| 76 | +
|
| 77 | + This checks for a specific Django permission before allowing agent execution. |
| 78 | + The permission should be in the format 'app_label.permission_codename'. |
| 79 | +
|
| 80 | + Args: |
| 81 | + permission: The Django permission string (e.g., 'myapp.can_use_agent') |
| 82 | + """ |
| 83 | + |
| 84 | + def __init__(self, permission: str): |
| 85 | + if "." not in permission: |
| 86 | + raise ValueError( |
| 87 | + "Permission must be in format 'app_label.permission_codename', " |
| 88 | + f"got '{permission}'" |
| 89 | + ) |
| 90 | + self.permission = permission |
| 91 | + |
| 92 | + def has_permission(self, request: HttpRequest, agent_slug: str, **kwargs) -> bool: |
| 93 | + if not request.user or isinstance(request.user, AnonymousUser): |
| 94 | + return False |
| 95 | + return request.user.has_perm(self.permission) |
| 96 | + |
| 97 | + def get_permission_denied_message( |
| 98 | + self, request: HttpRequest, agent_slug: str |
| 99 | + ) -> str: |
| 100 | + return f"You do not have the required permission '{self.permission}' to execute agent '{agent_slug}'" |
| 101 | + |
| 102 | + |
| 103 | +class CompositePermission(BasePermission): |
| 104 | + """ |
| 105 | + Permission that combines multiple permissions |
| 106 | +
|
| 107 | + Args: |
| 108 | + permissions: List of permissions |
| 109 | + require_all: If True, all permissions must pass. If False, any permission can pass. |
| 110 | + """ |
| 111 | + |
| 112 | + def __init__(self, permissions: list[BasePermission], require_all: bool = True): |
| 113 | + if not permissions: |
| 114 | + raise ValueError("At least one permission must be provided") |
| 115 | + self.permissions = permissions |
| 116 | + self.require_all = require_all |
| 117 | + |
| 118 | + def has_permission(self, request: HttpRequest, agent_slug: str, **kwargs) -> bool: |
| 119 | + if self.require_all: |
| 120 | + return all( |
| 121 | + permission.has_permission(request, agent_slug, **kwargs) |
| 122 | + for permission in self.permissions |
| 123 | + ) |
| 124 | + else: |
| 125 | + return any( |
| 126 | + permission.has_permission(request, agent_slug, **kwargs) |
| 127 | + for permission in self.permissions |
| 128 | + ) |
| 129 | + |
| 130 | + def get_permission_denied_message( |
| 131 | + self, request: HttpRequest, agent_slug: str |
| 132 | + ) -> str: |
| 133 | + for permission in self.permissions: |
| 134 | + if not permission.has_permission(request, agent_slug): |
| 135 | + return permission.get_permission_denied_message(request, agent_slug) |
| 136 | + return f"You do not have permission to execute agent '{agent_slug}'" |
0 commit comments