U
    {ha                     @   s   d dl Z d dlmZ 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 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   Z/var/www/dating/data/www/fatepal.com/env/lib/python3.8/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| f||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_required5   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   I   s    
z%any_permission_required.<locals>.testr(   )r,   r   r   r+   r   any_permission_requiredC   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__Y   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   c   s    z1PermissionPolicyChecker.require_any.<locals>.testr(   )r0   r7   r   r   r6   r   require_anyb   s    z#PermissionPolicyChecker.require_anyN)__name__
__module____qualname____doc__r1   r4   r8   r   r   r   r   r.   S   s   r.   c              	   C   s   t | ddddddhS )z\
    Check if a user has any permission to add, edit, or otherwise manage any
    page.
    addZchangepublishZbulk_deletelockunlock)r   r5   r%   r   r   r   user_has_any_page_permissioni   s     rA   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.viewsrB   getattrr   r   Zget_full_path)r   Zauth_redirect_to_loginrC   r   r   r   reject_requests   s      rE   c                    s    fdd}|S )Nc                    sd  | j }|jrt| S |dgr:zd  t|drR|j  t  |j	 nt
jt t|d  rt  | f||}W 5 Q R X n| f||}t|dr|j fdd}t|||_|W  5 Q R  W  5 Q R  W S Q R X W 5 Q R X W n2 tk
r8   | jddkr, t|  Y S X | jddks\t| td	 t| S )
Nzwagtailadmin.access_adminwagtail_userprofiler%   renderc                    sX   t F  r<t    W  5 Q R  W  5 Q R  S Q R X  W  5 Q R  S Q R X d S r   )override_tzr   )responseZpreferred_languagerG   Z	time_zoner   r   overridden_render   s
    

$zGrequire_admin_access.<locals>.decorated_view.<locals>.overridden_renderr   r   z.You do not have permission to access the admin)r   Zis_anonymousrE   Z	has_permshasattrrF   Zget_preferred_languagel18nZset_languageZget_current_time_zoner   Z	TIME_ZONErH   r   r   rG   types
MethodTyper   r   r   r   r   r   r   )r   r   r   r   rI   rK   r!   rJ   r   decorated_view   s:    



	2z,require_admin_access.<locals>.decorated_viewr   )r   rP   r   r!   r   require_admin_access   s    9rQ   ) rN   	functoolsr   rM   Zdjango.confr   Zdjango.core.exceptionsr   Zdjango.shortcutsr   Zdjango.urlsr   Zdjango.utils.timezoner   rH   Zdjango.utils.translationr	   r   r   r   Zwagtail.log_actionsr   Zwagtail.permissionsr   r   r#   r)   r-   r.   rA   rE   rQ   r   r   r   r   <module>   s(   
