a
    DgG                     @   s   d dl mZmZ d dlmZ d dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZ G dd	 d	ZG d
d deZG dd deZG dd deZG dd deZG dd deZdS )    )get_permission_codenameget_user_model)
Permission)ContentType)FieldDoesNotExistImproperlyConfigured)Q)cached_property)resolve_model_stringc                   @   s   e Zd ZdZdZdd Ze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d Zdd Zdd Zdd Zdd  Zd!S )"BasePermissionPolicya  
    A 'permission policy' is an object that handles all decisions about the actions
    users are allowed to perform on a given model. The mechanism by which it does this
    is arbitrary, and may or may not involve the django.contrib.auth Permission model;
    it could be as simple as "allow all users to do everything".

    In this way, admin apps can change their permission-handling logic just by swapping
    to a different policy object, rather than having that logic spread across numerous
    view functions.

    BasePermissionPolicy is an abstract class that all permission policies inherit from.
    The only method that subclasses need to implement is users_with_any_permission;
    all other methods can be derived from that (but in practice, subclasses will probably
    want to override additional methods, either for efficiency or to implement more
    fine-grained permission logic).
     c                 C   s
   || _ d S N)_model_or_nameselfmodel r   _/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/permission_policies/base.py__init__   s    zBasePermissionPolicy.__init__c                 C   s   t | j}| | |S r   )r
   r   check_modelr   r   r   r   r   "   s    

zBasePermissionPolicy.modelc                 C   s   d S r   r   r   r   r   r   r   (   s    z BasePermissionPolicy.check_modelc                 C   s   t  S )a	  
        Return a set of all permissions that the given user has on this model.

        They may be instances of django.contrib.auth.Permission, or custom
        permission objects defined by the policy, which are not necessarily
        model instances.
        )setr   userr   r   r   get_all_permissions_for_user.   s    z1BasePermissionPolicy.get_all_permissions_for_userc                 C   s<   t || jrt|| j}n| |}| jr8t|| j| |S )a(  
        Return a list of all permissions that the given user has on this model,
        using the cache if available and populating the cache if not.

        This can be useful for the other methods to perform efficient queries
        against the set of permissions that the user has.
        )hasattrpermission_cache_namegetattrr   setattr)r   r   Zpermsr   r   r   get_cached_permissions_for_user8   s    
z4BasePermissionPolicy.get_cached_permissions_for_userc                 C   s   ||  |v S )z
        Return whether the given user has permission to perform the given action
        on some or all instances of this model
        )users_with_permissionr   r   actionr   r   r   user_has_permissionL   s    z(BasePermissionPolicy.user_has_permissionc                    s   t  fdd|D S )z
        Return whether the given user has permission to perform any of the given actions
        on some or all instances of this model
        c                 3   s   | ]}  |V  qd S r   r"   .0r!   r   r   r   	<genexpr>X       z?BasePermissionPolicy.user_has_any_permission.<locals>.<genexpr>anyr   r   actionsr   r   r   user_has_any_permissionS   s    z,BasePermissionPolicy.user_has_any_permissionc                 C   s   t d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
        N)NotImplementedErrorr   r+   r   r   r   users_with_any_permission]   s    z.BasePermissionPolicy.users_with_any_permissionc                 C   s   |  |gS )z
        Return a queryset of users who have permission to perform the given action on
        some or all instances of this model
        r/   r   r!   r   r   r   r   d   s    z*BasePermissionPolicy.users_with_permissionc                 C   s   |  ||S )z~
        Return whether the given user has permission to perform the given action on the
        given model instance
        r#   r   r   r!   instancer   r   r    user_has_permission_for_instances   s    z5BasePermissionPolicy.user_has_permission_for_instancec                    s   t  fdd|D S )z
        Return whether the given user has permission to perform any of the given actions
        on the given model instance
        c                 3   s   | ]} | V  qd S r   )r4   r$   r3   r   r   r   r   r&      s   zLBasePermissionPolicy.user_has_any_permission_for_instance.<locals>.<genexpr>r(   r   r   r+   r3   r   r5   r   $user_has_any_permission_for_instancez   s    z9BasePermissionPolicy.user_has_any_permission_for_instancec                 C   s(   |  ||r| jj S | jj 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
        N)r,   r   objectsallnoner*   r   r   r   %instances_user_has_any_permission_for   s    z:BasePermissionPolicy.instances_user_has_any_permission_forc                 C   s   |  ||gS )z
        Return a queryset of all instances of this model for which the given user has
        permission to perform the given action
        )r;   r    r   r   r   !instances_user_has_permission_for   s    z6BasePermissionPolicy.instances_user_has_permission_forc                 C   s
   |  |S )z
        Return a queryset of all users who have permission to perform any of the given
        actions on the given model instance
        r0   )r   r+   r3   r   r   r   &users_with_any_permission_for_instance   s    z;BasePermissionPolicy.users_with_any_permission_for_instancec                 C   s   |  |g|S r   )r=   )r   r!   r3   r   r   r   "users_with_permission_for_instance   s    z7BasePermissionPolicy.users_with_permission_for_instanceN)__name__
__module____qualname____doc__r   r   r	   r   r   r   r   r"   r,   r/   r   r4   r7   r;   r<   r=   r>   r   r   r   r   r      s$   




r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )BlanketPermissionPolicyzv
    A permission policy that gives everyone (including anonymous users)
    full permission over the given model
    c                 C   s   dS NTr   r    r   r   r   r"      s    z+BlanketPermissionPolicy.user_has_permissionc                 C   s   dS rD   r   r*   r   r   r   r,      s    z/BlanketPermissionPolicy.user_has_any_permissionc                 C   s   t  jjddS NT	is_activer   r8   filterr.   r   r   r   r/      s    	z1BlanketPermissionPolicy.users_with_any_permissionc                 C   s   t  jjddS rE   rH   r1   r   r   r   r      s    z-BlanketPermissionPolicy.users_with_permissionNr?   r@   rA   rB   r"   r,   r/   r   r   r   r   r   rC      s
   rC   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )"AuthenticationOnlyPermissionPolicyzp
    A permission policy that gives all active authenticated users
    full permission over the given model
    c                 C   s   |j o
|jS r   is_authenticatedrG   r    r   r   r   r"      s    z6AuthenticationOnlyPermissionPolicy.user_has_permissionc                 C   s   |j o
|jS r   rL   r*   r   r   r   r,      s    z:AuthenticationOnlyPermissionPolicy.user_has_any_permissionc                 C   s   t  jjddS rE   rH   r.   r   r   r   r/      s    z<AuthenticationOnlyPermissionPolicy.users_with_any_permissionc                 C   s   t  jjddS rE   rH   r1   r   r   r   r      s    z8AuthenticationOnlyPermissionPolicy.users_with_permissionNrJ   r   r   r   r   rK      s
   rK   c                       sz   e Zd ZdZd fdd	Zedd Zedd Zed	d
 Zedd Z	dd Z
dd Zdd Zdd Zdd Z  ZS )BaseDjangoAuthPermissionPolicyz
    Extends BasePermissionPolicy with helper methods useful for policies that need to
    perform lookups against the django.contrib.auth permission model
    Nc                    s   t  | |p|| _d S r   )superr   _auth_model_or_name)r   r   
auth_model	__class__r   r   r      s    z'BaseDjangoAuthPermissionPolicy.__init__c                 C   s
   t | jS r   )r
   rP   r   r   r   r   rQ      s    z)BaseDjangoAuthPermissionPolicy.auth_modelc                 C   s
   | j jjS r   )rQ   _meta	app_labelrT   r   r   r   rV      s    z(BaseDjangoAuthPermissionPolicy.app_labelc                 C   s
   | j jjS r   )rQ   rU   
model_namerT   r   r   r   rW      s    z)BaseDjangoAuthPermissionPolicy.model_namec                 C   s   t j| jS r   )r   r8   Zget_for_modelrQ   rT   r   r   r   _content_type   s    z,BaseDjangoAuthPermissionPolicy._content_typec                    s    fdd|D S )Nc                    s   h | ]}t | jjqS r   )r   r   rU   r$   rT   r   r   	<setcomp>   r'   zKBaseDjangoAuthPermissionPolicy._get_permission_codenames.<locals>.<setcomp>r   r.   r   rT   r   _get_permission_codenames   s    z8BaseDjangoAuthPermissionPolicy._get_permission_codenamesc                 C   s   d | jt|| jjS )z
        Get the full app-label-qualified permission name (as required by
        user.has_perm(...) ) for the given action on this model
        z{}.{})formatrV   r   r   rU   r1   r   r   r   _get_permission_name   s    z3BaseDjangoAuthPermissionPolicy._get_permission_namec                 C   s   t jj| j| |dS )zP
        Get a queryset of the Permission objects for the given actions
        content_typeZcodename__in)r   r8   rI   rX   rZ   r.   r   r   r   #_get_permission_objects_for_actions   s    zBBaseDjangoAuthPermissionPolicy._get_permission_objects_for_actionsc                 C   s:   t jj| j|d}tddt|dB t|dB tdd@ S )a  
        Given a list of permission codenames, return a filter expression which
        will find all users which have any of those permissions - either
        through group permissions, user permissions, or implicitly through
        being a superuser.
        r]   T)is_superuser)Zuser_permissions__in)Zgroups__permissions__inrF   )r   r8   rI   rX   r   )r   permission_codenamesZpermissionsr   r   r   /_get_users_with_any_permission_codenames_filter  s    zNBaseDjangoAuthPermissionPolicy._get_users_with_any_permission_codenames_filterc                 C   s   |  |}t j| S )z
        Given a list of permission codenames, return a queryset of users which
        have any of those permissions - either through group permissions, user
        permissions, or implicitly through being a superuser.
        )rb   r   r8   rI   distinct)r   ra   filter_exprr   r   r   (_get_users_with_any_permission_codenames  s    zGBaseDjangoAuthPermissionPolicy._get_users_with_any_permission_codenames)N)r?   r@   rA   rB   r   r	   rQ   rV   rW   rX   rZ   r\   r_   rb   re   __classcell__r   r   rR   r   rN      s   





	rN   c                   @   s    e Zd ZdZdd Zdd ZdS )ModelPermissionPolicyz
    A permission policy that enforces permissions at the model level, by consulting
    the standard django.contrib.auth permission model directly
    c                 C   s   | | |S r   )has_permr\   r    r   r   r   r"   &  s    z)ModelPermissionPolicy.user_has_permissionc                 C   s   |  | |S r   )re   rZ   r.   r   r   r   r/   )  s    z/ModelPermissionPolicy.users_with_any_permissionN)r?   r@   rA   rB   r"   r/   r   r   r   r   rg      s   rg   c                       s^   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  ZS )OwnershipPermissionPolicya  
    A permission policy for objects that support a concept of 'ownership', where
    the owner is typically the user who created the object.

    This policy piggybacks off 'add' and 'change' permissions defined through the
    django.contrib.auth Permission model, as follows:

    * any user with 'add' permission can create instances, and ALSO edit instances
    that they own
    * any user with 'change' permission can edit instances regardless of ownership
    * ability to edit also implies ability to delete

    Besides 'add', 'change' and 'delete', no other actions are recognised or permitted
    (unless the user is an active superuser, in which case they can do everything).
    Nownerc                    s   t  j||d || _d S )N)rQ   )rO   r   owner_field_name)r   r   rQ   rk   rR   r   r   r   @  s    z"OwnershipPermissionPolicy.__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 OwnershipPermissionPolicy, you must specify a valid field name as owner_field_name.)rO   r   rU   	get_fieldrk   r   r   r   rR   r   r   r   D  s    z%OwnershipPermissionPolicy.check_modelc                 C   sX   |dkr| | dS |dks(|dkrH| | dpF| | dS |joR|jS d S )Naddchangedelete)rh   r\   rG   r`   r    r   r   r   r"   Q  s    	z-OwnershipPermissionPolicy.user_has_permissionc                 C   sR   d|v sd|v r |  ddh}n(d|v r6|  dh}nt jjdddS | |S )Nrn   ro   rm   TrG   r`   )rZ   r   r8   rI   re   )r   r+   ra   r   r   r   r/   `  s    z3OwnershipPermissionPolicy.users_with_any_permissionc                 C   s   |  ||g|S r   )r7   r2   r   r   r   r4   n  s    z:OwnershipPermissionPolicy.user_has_permission_for_instancec                 C   s^   d|v sd|v rN| | dr$dS | | drHt|| j|krHdS dS n|joX|jS d S )Nrn   ro   Trm   F)rh   r\   r   rk   rG   r`   r6   r   r   r   r7   q  s    z>OwnershipPermissionPolicy.user_has_any_permission_for_instancec                 C   s   |j r|jr| jj S d|v s(d|v r||| drD| jj S || drn| jjjf i | j|iS | jj	 S n| jj	 S d S )Nrn   ro   rm   )
rG   r`   r   r8   r9   rh   r\   rI   rk   r:   r*   r   r   r   r;     s    z?OwnershipPermissionPolicy.instances_user_has_any_permission_forc                 C   s~   d|v sd|v rh|  | dh}t|| j}|d urV|| drV|t|jdB }t j	
| S t j	j
dddS d S )Nrn   ro   rm   )pkTrp   )rb   rZ   r   rk   rh   r\   r   rq   r   r8   rI   rc   )r   r+   r3   rd   rj   r   r   r   r=     s    
z@OwnershipPermissionPolicy.users_with_any_permission_for_instance)Nrj   )r?   r@   rA   rB   r   r   r"   r/   r4   r7   r;   r=   rf   r   r   rR   r   ri   /  s   ri   N)Zdjango.contrib.authr   r   Zdjango.contrib.auth.modelsr   Z"django.contrib.contenttypes.modelsr   Zdjango.core.exceptionsr   r   Zdjango.db.modelsr   Zdjango.utils.functionalr	   Zwagtail.coreutilsr
   r   rC   rK   rN   rg   ri   r   r   r   r   <module>   s    R