a
    Dg                     @   s   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZ d dlmZ dd Zdd Zdd Zdd ZG dd dZdd Zdd Zdd ZdS )    Nwraps)settings)PermissionDenied)redirect)reverse)override)gettextmessages)
LogContext)page_permission_policyc                 C   s8   | j ddkrtddlm} || td tdS )z.Return a standard 'permission denied' responsex-requested-withXMLHttpRequestr   r
   z6Sorry, you do not have permission to access this area.Zwagtailadmin_home)headersgetr   wagtail.adminr   error_r   )requestr    r   Q/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/admin/auth.pypermission_denied   s
    r   c                    s    fdd}|S )z
    Given a test function that takes a user object and returns a boolean,
    return a view decorator that denies access to the user if the test returns false.
    c                    s   t   fdd}|S )Nc                    s,    | j r | g|R i |S t| S d S N)userr   )r   argskwargs)test	view_funcr   r   wrapped_view_func&   s    
z>user_passes_test.<locals>.decorator.<locals>.wrapped_view_funcr   )r   r   r   r   r   	decorator"   s    z#user_passes_test.<locals>.decoratorr   )r   r"   r   r    r   user_passes_test   s    r#   c                    s    fdd}t |S )a  
    Replacement for django.contrib.auth.decorators.permission_required which returns a
    more meaningful 'permission denied' response than just redirecting to the login page.
    (The latter doesn't work anyway because Wagtail doesn't define LOGIN_URL...)
    c                    s
   |   S r   Zhas_permr   permission_namer   r   r   ;   s    z!permission_required.<locals>.testr#   )r'   r   r   r&   r   permission_required4   s    r)   c                     s    fdd}t |S )z
    Decorator that accepts a list of permission names, and allows the user
    to pass if they have *any* of the permissions in the list
    c                    s    D ]}|  |r dS qdS )NTFr$   )r   permpermsr   r   r   H   s    
z%any_permission_required.<locals>.testr(   )r,   r   r   r+   r   any_permission_requiredB   s    r-   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	PermissionPolicyCheckerz
    Provides a view decorator that enforces the given permission policy,
    returning the wagtailadmin 'permission denied' response if permission not granted
    c                 C   s
   || _ d S r   )policy)selfr/   r   r   r   __init__X   s    z PermissionPolicyChecker.__init__c                    s    fdd}t |S )Nc                    s   j |  S r   )r/   Zuser_has_permissionr%   actionr0   r   r   r   \   s    z-PermissionPolicyChecker.require.<locals>.testr(   )r0   r3   r   r   r2   r   require[   s    zPermissionPolicyChecker.requirec                    s    fdd}t |S )Nc                    s   j |  S r   )r/   user_has_any_permissionr%   actionsr0   r   r   r   b   s    z1PermissionPolicyChecker.require_any.<locals>.testr(   )r0   r7   r   r   r6   r   require_anya   s    z#PermissionPolicyChecker.require_anyN)__name__
__module____qualname____doc__r1   r4   r8   r   r   r   r   r.   R   s   r.   c                 C   s   t | h dS )z\
    Check if a user has any permission to add, edit, or otherwise manage any
    page.
    >   lockpublishZunlockaddZchangeZbulk_delete)r   r5   r%   r   r   r   user_has_any_page_permissionh   s    r@   c                 C   s@   | j ddkrtddlm} ttdtd}||  |dS )Nr   r   r   )redirect_to_loginZWAGTAILADMIN_LOGIN_URLZwagtailadmin_login)	login_url)	r   r   r   Zdjango.contrib.auth.viewsrA   getattrr   r   Zget_full_path)r   Zauth_redirect_to_loginrB   r   r   r   reject_requestr   s    
rD   c                    s    fdd}|S )Nc              
      s  | j }|jrt| S |dgrvz d  t|drJ|j  |j ntj	t
 t|d  rt & | g|R i |}W d    q1 s0    Y  n| g|R i |}t|dr|j fdd}t|||_|W  d    W  d    W S 1 s0    Y  W d    n1 s80    Y  W n0 tyt   | jddkrh t|  Y S 0 | jddkst| td	 t| S )
Nzwagtailadmin.access_adminwagtail_userprofiler%   renderc              	      sr   t V  rJt $  W  d    W  d    S 1 s@0    Y   W  d    S 1 sd0    Y  d S r   )override_tzr   )responseZpreferred_languagerF   Z	time_zoner   r   overridden_render   s
    

2zGrequire_admin_access.<locals>.decorated_view.<locals>.overridden_renderr   r   z.You do not have permission to access the admin)r   Zis_anonymousrD   Z	has_permshasattrrE   Zget_preferred_languageZget_current_time_zoner   Z	TIME_ZONErG   r   r   rF   types
MethodTyper   r   r   r   r   r   r   )r   r   r   r   rH   rJ   r!   rI   r   decorated_view   s8    

6
	Xz,require_admin_access.<locals>.decorated_viewr   )r   rN   r   r!   r   require_admin_access   s    8rO   )rL   	functoolsr   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.shortcutsr   Zdjango.urlsr   Zdjango.utils.timezoner   rG   Zdjango.utils.translationr	   r   r   r   Zwagtail.log_actionsr   Zwagtail.permissionsr   r   r#   r)   r-   r.   r@   rD   rO   r   r   r   r   <module>   s&   
