
    Xj:                    4   d dl mZ d dlZd dlZd dlmZ d dlmZ d dlmZm	Z	m
Z
mZmZmZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZmZ d dlmZ erd dlmZm Z  d dl!m"Z"m#Z# d dl$m%Z% d dl&m'Z'  G d dejP                        Z) G d de      Z*ddgZ+y)    )annotationsN)cached_property)iscoroutinefunction)TYPE_CHECKINGAny	AwaitableDictListOptionalTypeUnion)StrawberryGraphQLError)+PermissionFailSilentlyRequiresOptionalError)FieldExtension)LocationStrawberrySchemaDirective)StrawberryListStrawberryOptional)await_maybe)GraphQLErrorGraphQLErrorExtensions)AsyncExtensionResolverSyncExtensionResolver)Info)StrawberryFieldc                      e Zd ZU dZdZded<   dZded<   eZded<   dZ	d	ed
<   e
j                  	 	 	 	 	 	 	 	 dd       ZddZedd       Zy)BasePermissionay  Base class for permissions. All permissions should inherit from this class.

    Example:

    ```python
    from strawberry.permission import BasePermission


    class IsAuthenticated(BasePermission):
        message = "User is not authenticated"

        def has_permission(self, source, info, **kwargs):
            return info.context["user"].is_authenticated
    ```
    NzOptional[str]messagez Optional[GraphQLErrorExtensions]error_extensionszType[GraphQLError]error_classzOptional[object]_schema_directivec                    t        d      )zqCheck if the permission should be accepted.

        This method should be overridden by the subclasses.
        z8Permission classes should override has_permission method)NotImplementedError)selfsourceinfokwargss       b/var/www/html/myl_app/scheduler-service/venv/lib/python3.12/site-packages/strawberry/permission.pyhas_permissionzBasePermission.has_permission?   s     "F
 	
    c                    | j                  | j                  xs d      }| j                  r@|j                  st	               |_        |j                  j                  | j                         |)a  Default error raising for permissions.

        This method can be overridden to customize the error raised when the permission is not granted.

        Example:

        ```python
        from strawberry.permission import BasePermission


        class CustomPermissionError(PermissionError):
            pass


        class IsAuthenticated(BasePermission):
            message = "User is not authenticated"

            def has_permission(self, source, info, **kwargs):
                return info.context["user"].is_authenticated

            def on_unauthorized(self) -> None:
                raise CustomPermissionError(self.message)
        ```
         )r    r   r   
extensionsdictupdate)r$   errors     r(   on_unauthorizedzBasePermission.on_unauthorizedK   sX    4   !34  ###'6 ##D$9$9:r*   c                d      j                   s G  fdd      } |        _          j                   S )Nc                      e Zd Z eW  j                  j                   W  j                  j                   ej                  gg       Zy)6BasePermission.schema_directive.<locals>.AutoDirectiveN)__name__
__module____qualname__r   	__class__r   FIELD_DEFINITION__strawberry_directive__)r$   s   r(   AutoDirectiver4   s   s7    +DNN++NN++../	,(r*   r;   )r!   )r$   r;   s   ` r(   schema_directivezBasePermission.schema_directiveo   s.    %%  &3_D"%%%r*   )r%   r   r&   r   r'   r   returnzUnion[bool, Awaitable[bool]])r=   None)r=   object)r5   r6   r7   __doc__r   __annotations__r   r   r    r!   abcabstractmethodr)   r1   propertyr<    r*   r(   r   r   &   s      "G]!9=6=&<K#<*.'.	
	
!%	
14	
	%	
 	
"H & &r*   r   c                      e Zd ZdZ	 	 d		 	 	 	 	 	 	 d
dZddZddZ	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZe	dd       Z
y)PermissionExtensionaD  Handles permissions for a field.

    Instantiate this as a field extension with all of the permissions you want to apply.

    Note:
        Currently, this is automatically added to the field, when using field.permission_classes

    This is deprecated behaviour, please manually add the extension to field.extensions
    c                <    || _         || _        d| _        || _        y)aS  Initialize the permission extension.

        Args:
            permissions: List of permissions to apply.
            fail_silently: If True, return None or [] instead of raising an exception.
                This is only valid for optional or list fields.
            use_directives: If True, add schema directives to the field.
        FN)permissionsfail_silentlyreturn_empty_listuse_directives)r$   rI   rL   rJ   s       r(   __init__zPermissionExtension.__init__   s$     '*!&,r*   c                x   | j                   r,|j                  j                  d | j                  D               | j                  rvt        |j                  t              r-t        |j                  j                  t              rd| _
        yyt        |j                  t              rd| _
        yt        |      }|y)zVApplies all of the permission directives to the schema and sets up silent permissions.c              3  N   K   | ]  }|j                   s|j                     y wN)r<   ).0ps     r(   	<genexpr>z,PermissionExtension.apply.<locals>.<genexpr>   s#      $'(@R@R""$s   %%TN)rL   
directivesextendrI   rJ   
isinstancetyper   of_typer   rK   r   )r$   fielderrrors      r(   applyzPermissionExtension.apply   s    ## $,0,<,<$  %**&89ejj00.A-1D* BEJJ7)-&DUK r*   c                Z    | j                   r| j                  rg S d S |j                         S rP   )rJ   rK   r1   )r$   
permissions     r(   _on_unauthorizedz$PermissionExtension._on_unauthorized   s-    //29T9))++r*   c                    | j                   D ])  } |j                  ||fi |r| j                  |      c S   |||fi |S )zKChecks if the permission should be accepted and raises an exception if not.)rI   r)   r^   )r$   next_r%   r&   r'   r]   s         r(   resolvezPermissionExtension.resolve   sU     ** 	9J,:,,VTDVD,,Z88	9 VT,V,,r*   c                   K   | j                   D ]<  }t         |j                  ||fi |       d {   }|r+| j                  |      c S   |||fi |}t	        j
                  |      r|S | d {   S 7 F7 wrP   )rI   r   r)   r^   inspect
isasyncgen)r$   r`   r%   r&   r'   r]   r)   nexts           r(   resolve_asyncz!PermissionExtension.resolve_async   s      ** 	9J#.)
))&$A&A$ N ",,Z88	9 VT,V,d#Kz s&   0A=A9A=:A=4A;5A=;A=c                    | j                   D cg c]  }t        |j                        rd }}t        |      dk(  S c c}w )zWhether this extension can be resolved synchronously or not.

        The Permission extension always supports async checking using await_maybe,
        but only supports sync checking if there are no async permissions.
        Tr   )rI   r   r)   len)r$   r]   async_permissionss      r(   supports_syncz!PermissionExtension.supports_sync   sN     #..
":#<#<= 
 

 $%**
s   =N)TF)rI   zList[BasePermission]rL   boolrJ   rk   r=   r>   )rY   r   r=   r>   )r]   r   r=   r   )
r`   r   r%   r   r&   r   r'   Dict[str, Any]r=   r   )
r`   r   r%   r   r&   r   r'   rl   r=   r   )r=   rk   )r5   r6   r7   r@   rM   r[   r^   ra   rf   r   rj   rE   r*   r(   rG   rG      s      $#	-)- - 	-
 
-&",
-$- - 	-
 !- 
-%  	
 ! 
& + +r*   rG   ),
__future__r   rB   rc   	functoolsr   r   typingr   r   r   r	   r
   r   r   r   strawberry.exceptionsr   @strawberry.exceptions.permission_fail_silently_requires_optionalr   strawberry.extensionsr   strawberry.schema_directiver   r   strawberry.types.baser   r   strawberry.utils.await_mayber   graphqlr   r   %strawberry.extensions.field_extensionr   r   strawberry.typesr   strawberry.types.fieldr   ABCr   rG   __all__rE   r*   r(   <module>r|      s    " 
  % '	 	 	 9 1 K D 4< &6W&SWW W&t`+. `+H r*   