a
    DgXP                     @   s   d dl mZmZ d dlmZ d dlmZmZ d dlm	Z	 d dl
mZmZ ddlmZ G dd	 d	ZG d
d deeZG dd deeZG dd deeZdS )    )get_permission_codenameget_user_model)Group)FieldDoesNotExistImproperlyConfigured)Q)
CollectionGroupCollectionPermission   )BaseDjangoAuthPermissionPolicyc                   @   sV   e Zd ZdZdd Zdd ZdddZd	d
 ZdddZdddZ	dd Z
dd ZdS )CollectionPermissionLookupMixinZ_collection_permission_cachec                    s*   fdd|D   fdd |D S )za
        Get a set of the user's GroupCollectionPermission objects for the given actions
        c                    s   h | ]}t | jjqS  )r   
auth_model_meta).0action)selfr   f/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/permission_policies/collections.py	<setcomp>   s   z[CollectionPermissionLookupMixin._get_user_permission_objects_for_actions.<locals>.<setcomp>c                    s   h | ]}|j j v r|qS r   )
permissioncodename)r   Zgroup_permission)permission_codenamesr   r   r      s   )Zget_cached_permissions_for_userr   useractionsr   )r   r   r   (_get_user_permission_objects_for_actions   s    

zHCollectionPermissionLookupMixin._get_user_permission_objects_for_actionsc                 C   s2   |j r|js|jrtj S tjj|dddS )N)Zgroup__userr   
collection)	is_activeZis_anonymousis_superuserr	   objectsnonefilterZselect_related)r   r   r   r   r   get_all_permissions_for_user   s    
z<CollectionPermissionLookupMixin.get_all_permissions_for_userNc                    sD   |j r|jsdS |jrdS | ||} r< fdd|D }t|S )a-  
        Equivalent to user.has_perm(self._get_permission_name(action)) on all listed actions,
        but using GroupCollectionPermission rather than group.permissions.
        If collection is specified, only consider GroupCollectionPermission records
        that apply to that collection.
        FTc                    s(   h | ] }  |js  j|jkr|qS r   )Zis_descendant_ofr   pkZcollection_id)r   r   r   r   r   r   7   s   z>CollectionPermissionLookupMixin._check_perm.<locals>.<setcomp>)r   is_authenticatedr   r   bool)r   r   r   r   Zcollection_permissionsr   r$   r   _check_perm%   s    
z+CollectionPermissionLookupMixin._check_permc                 C   s:   |  ||}tj }|D ]}|tjj|jddO }q|S )z
        Return a queryset of collections on which this user has a GroupCollectionPermission
        record for any of the given actions, either on the collection itself or an ancestor
        TZ	inclusive)r   r   r   r    Zdescendant_ofr   )r   r   r   permissionscollectionspermr   r   r   _collections_with_perm@   s    

z6CollectionPermissionLookupMixin._collections_with_permc                 C   sV   |  |}tjj|d}|dur8|jdd}|j|d}tddtddt|dB @ S )	aP  
        Return a filter expression that will filter a user queryset to those with any
        permissions corresponding to 'actions', via either GroupCollectionPermission
        or superuser privileges.
        If collection is specified, only consider GroupCollectionPermission records
        that apply to that collection.
        )Z&collection_permissions__permission__inNTr(   )Z&collection_permissions__collection__in)r   )r   )Z
groups__in)#_get_permission_objects_for_actionsr   r   r!   Zget_ancestorsr   )r   r   r   r)   groupsr*   r   r   r   _users_with_perm_filterO   s    
z7CollectionPermissionLookupMixin._users_with_perm_filterc                 C   s   t  j| j||d S )a  
        Return a queryset of users with any permissions corresponding to 'actions',
        via either GroupCollectionPermission or superuser privileges.
        If collection is specified, only consider GroupCollectionPermission records
        that apply to that collection.
        r$   )r   r   r!   r/   distinct)r   r   r   r   r   r   _users_with_perme   s    z0CollectionPermissionLookupMixin._users_with_permc                 C   s2   |j r|jrtj S |js&tj S | ||S )
        Return a queryset of all collections in which the given user has
        permission to perform any of the given actions
        )r   r   r   r   allr%   r    r,   r   r   r   r   'collections_user_has_any_permission_fort   s
    

zGCollectionPermissionLookupMixin.collections_user_has_any_permission_forc                 C   s   |  ||gS )z
        Return a queryset of all collections in which the given user has
        permission to perform the given action
        r4   r   r   r   r   r   r   #collections_user_has_permission_for   s    zCCollectionPermissionLookupMixin.collections_user_has_permission_for)N)N)N)__name__
__module____qualname__Zpermission_cache_namer   r"   r'   r,   r/   r1   r4   r7   r   r   r   r   r      s   



r   c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )CollectionPermissionPolicyaQ  
    A permission policy for objects that are assigned locations in the Collection tree.
    Permissions may be defined at any node of the hierarchy, through the
    GroupCollectionPermission model, and propagate downwards. These permissions are
    applied to objects according to the standard django.contrib.auth permission model.
    c                 C   s   |  ||gS z
        Return whether the given user has permission to perform the given action
        on some or all instances of this model
        r'   r6   r   r   r   user_has_permission   s    z.CollectionPermissionPolicy.user_has_permissionc                 C   s   |  ||S )z
        Return whether the given user has permission to perform any of the given actions
        on some or all instances of this model
        r=   r   r   r   r   user_has_any_permission   s    z2CollectionPermissionPolicy.user_has_any_permissionc                 C   s
   |  |S z
        Return a queryset of users who have permission to perform any of the given actions
        on some or all instances of this model
        r1   r   r   r   r   r   users_with_any_permission   s    z4CollectionPermissionPolicy.users_with_any_permissionc                 C   s   | j ||g|jdS z~
        Return whether the given user has permission to perform the given action on the
        given model instance
        r$   r'   r   r   r   r   instancer   r   r    user_has_permission_for_instance   s    z;CollectionPermissionPolicy.user_has_permission_for_instancec                 C   s   | j |||jdS z
        Return whether the given user has permission to perform any of the given actions
        on the given model instance
        r$   rE   r   r   r   rG   r   r   r   $user_has_any_permission_for_instance   s    z?CollectionPermissionPolicy.user_has_any_permission_for_instancec                 C   sJ   |j r|js| jj S |jr*| jj S | jjjt| 	||dS dS )z
        Return a queryset of all instances of this model for which the given user has
        permission to perform any of the given actions
        Zcollection__inN)
r   r%   modelr   r    r   r3   r!   listr,   r   r   r   r   %instances_user_has_any_permission_for   s    z@CollectionPermissionPolicy.instances_user_has_any_permission_forc                 C   s   | j ||jdS z
        Return a queryset of all users who have permission to perform any of the given
        actions on the given model instance
        r$   )r1   r   r   r   rG   r   r   r   &users_with_any_permission_for_instance   s    zACollectionPermissionPolicy.users_with_any_permission_for_instanceN)r8   r9   r:   __doc__r>   r?   rC   rH   rK   rO   rR   r   r   r   r   r;      s   r;   c                       sf   e Zd ZdZd fdd	Z fddZdd	 Zd
d Zdd Zdd Z	dd Z
dd Zdd Z  ZS )#CollectionOwnershipPermissionPolicya|  
    A permission policy for objects that are assigned locations in the Collection tree.
    Permissions may be defined at any node of the hierarchy, through the
    GroupCollectionPermission model, and propagate downwards. These permissions are
    applied to objects according to the 'ownership' permission model
    (see permission_policies.base.OwnershipPermissionPolicy)
    Nownerc                    s   t  j||d || _d S )N)r   )super__init__owner_field_name)r   rM   r   rX   	__class__r   r   rW      s    z,CollectionOwnershipPermissionPolicy.__init__c                    sH   t  | z|j| j W n$ tyB   td|| jf Y n0 d S )Nz%s has no field named '%s'. To use this model with CollectionOwnershipPermissionPolicy, you must specify a valid field name as owner_field_name.)rV   check_modelr   	get_fieldrX   r   r   )r   rM   rY   r   r   r[      s    z/CollectionOwnershipPermissionPolicy.check_modelc                 C   s\   |dkr|  |dgS |dkr,|  |dgS |dks<|dkrL|  |ddgS |joV|jS d S )Naddchoosechangedelete)r'   r   r   r6   r   r   r   r>      s    z7CollectionOwnershipPermissionPolicy.user_has_permissionc                 C   sT   t |h d@ }d|v r"|d d|v r4|d |sJt jjdddS | |S )N>   r_   r]   r^   r`   r_   r]   Tr   r   )setr]   r   r   r!   r1   )r   r   known_actionsr   r   r   rC      s    

z=CollectionOwnershipPermissionPolicy.users_with_any_permissionc                 C   s   |  ||g|S N)rK   rF   r   r   r   rH     s    zDCollectionOwnershipPermissionPolicy.user_has_permission_for_instancec                 C   sj   t |h d@ }d|v r"|d d|v rDt|| j|krD|d |rZ| j|||jdS |jod|jS d S )N>   r_   r]   r^   r`   r_   r]   r$   )rb   r]   getattrrX   r'   r   r   r   )r   r   r   rG   rc   r   r   r   rK     s    

zHCollectionOwnershipPermissionPolicy.user_has_any_permission_for_instancec                 C   s   t |ddh@ }d|v r"|d |jr:|jr:| jj S |jsL| jj S |r| 	||}t
|d}d|v r|t
| 	|dgdt
f i | j|i@ O }| jj|S | jj S d S )Nr_   r^   r`   rL   r]   )rb   r]   r   r   rM   r   r3   r%   r    r,   r   rX   r!   )r   r   r   rc   r*   Zperm_filterr   r   r   rO   $  s$    


zICollectionOwnershipPermissionPolicy.instances_user_has_any_permission_forc                 C   s   t |ddh@ }d|v r"|d | j||jd}d|v rrt|| j}|d urr| j|dh|jdrr|t|jdO }|rt	 j
| S t	 j
jdddS d S )	Nr^   r_   r`   r$   r]   )r#   Tra   )rb   r]   r/   r   re   rX   r'   r   r#   r   r   r!   r0   )r   r   rG   rc   Zfilter_exprrU   r   r   r   rR   L  s    

zJCollectionOwnershipPermissionPolicy.users_with_any_permission_for_instancec                 C   sx   t |h d@ }d|v r"|d d|v r4|d |jrJ|jrJtj S |jsZtj S |rj| 	||S tj S dS )r2   >   r_   r]   r^   r`   r_   r]   N)
rb   r]   r   r   r   r   r3   r%   r    r,   )r   r   r   rc   r   r   r   r4   g  s    



zKCollectionOwnershipPermissionPolicy.collections_user_has_any_permission_for)NrU   )r8   r9   r:   rS   rW   r[   r>   rC   rH   rK   rO   rR   r4   __classcell__r   r   rY   r   rT      s   (rT   c                   @   sT   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdS )$CollectionManagementPermissionPolicyc                 C   s   |  |g }tjj|j |ddd}|rt|d d dt|d d d@ }|dd D ]$}|t|d dt|d d@ B }qbtj |S tj	 S dS )	a  
        Return a queryset of collections descended from a collection on which this user has
        a GroupCollectionPermission record for this action. Used for actions, like edit and
        delete where the user cannot modify the collection where they are granted permission.
        )Zgroup_permissions__group__inZgroup_permissions__permissionpathdepthr   )Zpath__startswith)Z	depth__gtr
   N)
r-   firstr   r   r!   r.   r3   valuesr   r    )r   r   r   r   Zcollection_rootsZcollection_path_filterr   r   r   r   _descendants_with_perm  s*    
z;CollectionManagementPermissionPolicy._descendants_with_permc                 C   s   |  ||gS r<   )r?   r6   r   r   r   r>     s    z8CollectionManagementPermissionPolicy.user_has_permissionc                 C   s   |  ||S )z
        Return whether the given user has permission to perform any of the given actions
        on some or all instances of this model.
        r=   r   r   r   r   r?     s    z<CollectionManagementPermissionPolicy.user_has_any_permissionc                 C   s
   |  |S r@   rA   rB   r   r   r   rC     s    z>CollectionManagementPermissionPolicy.users_with_any_permissionc                 C   s   | j ||g|dS rD   r=   rF   r   r   r   rH     s    zECollectionManagementPermissionPolicy.user_has_permission_for_instancec                 C   s   | j |||dS rI   r=   rJ   r   r   r   rK     s    zICollectionManagementPermissionPolicy.user_has_any_permission_for_instancec                 C   s   | j ||dS rP   rA   rQ   r   r   r   rR     s    zKCollectionManagementPermissionPolicy.users_with_any_permission_for_instancec                 C   sh   |j r2|jr2|dkr&tjjdd S tj S n2|jsBtj S |dkrV| ||S | 	||gS d S )Nr`   r
   )ri   )
r   r   r   r   excluder3   r%   r    rl   r,   r6   r   r   r   !instances_user_has_permission_for  s    
zFCollectionManagementPermissionPolicy.instances_user_has_permission_forc                 C   s   |  ||S rd   r5   r   r   r   r   rO     s    zJCollectionManagementPermissionPolicy.instances_user_has_any_permission_forN)r8   r9   r:   rl   r>   r?   rC   rH   rK   rR   rn   rO   r   r   r   r   rg     s   "rg   N)Zdjango.contrib.authr   r   Zdjango.contrib.auth.modelsr   Zdjango.core.exceptionsr   r   Zdjango.db.modelsr   Zwagtail.modelsr   r	   baser   r   r;   rT   rg   r   r   r   r   <module>   s"    
D
 9
