
    j
                     x    d Z ddlmZ ddlmZmZ  G d dej                        Z G d dej                        Zy)	z{
Custom permissions for Aimantis dashboard.

Provides structure-level access control to prevent
cross-tenant data leakage.
    )permissions)	StructureStructureUserc                       e Zd ZdZd Zy)HasStructureAccessz
    Permission class to check if user has access to a specific structure.
    
    Supports:
    - Structure owners (user in owned_structures)
    - Structure users (via StructureUser relationship)
    - Superusers (full access)
    c                    |j                   r|j                   j                  sy|j                   j                  ry|j                  j	                  d      }|sy	 t        |      }t        j                  j                  ||j                         j                         ryt        j                  j                  ||j                         j                         ryy# t        t        f$ r Y yw xY w)a
  
        Check if the authenticated user has access to the requested structure.
        
        Args:
            request: HTTP request
            view: View being accessed
            
        Returns:
            True if user has access, False otherwise
        FT	structure)iduser)structure_idr   )r   is_authenticatedis_superuserquery_paramsgetint
ValueError	TypeErrorr   objectsfilterexistsr   )selfrequestviewr   s       !/backend/dashboard/permissions.pyhas_permissionz!HasStructureAccess.has_permission   s     ||7<<#@#@ <<$$ ++//< 	|,L
 ## $ 
 &(    ''% ( 
 &(  % I& 		s   C C,+C,N__name__
__module____qualname____doc__r        r   r   r      s    .r"   r   c                       e Zd ZdZd Zy)IsStaffOrReadOnlyz
    Custom permission to allow staff full access, others read-only.
    
    Prepares for future staff-specific dashboard features.
    c                     |j                   t        j                  v ry|j                  xr |j                  j                  S )z
        Check permission based on user role and request method.
        
        Args:
            request: HTTP request
            view: View being accessed
            
        Returns:
            True if allowed, False otherwise
        T)methodr   SAFE_METHODSr   is_staff)r   r   r   s      r   r   z IsStaffOrReadOnly.has_permissionO   s4     >>[555 ||5 5 55r"   Nr   r!   r"   r   r$   r$   H   s    6r"   r$   N)	r    rest_frameworkr   structures.modelsr   r   BasePermissionr   r$   r!   r"   r   <module>r,      s7    ' 6833 8v622 6r"   