o
    h                     @   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.wagtailadmin_home)headersgetr   wagtail.adminr   error_r   )requestr    r   R/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/admin/auth.pypermission_denied   s
   r   c                        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 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                        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   has_permr   permission_namer   r   r   ;      
z!permission_required.<locals>.testr%   )r+   r   r   r*   r   permission_required4   s   r.   c                     r&   )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-   )r1   r   r   r0   r   any_permission_requiredB   s   r2   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)selfr4   r   r   r   __init__X   r,   z PermissionPolicyChecker.__init__c                        fdd}t |S )Nc                       j |  S r   )r4   user_has_permissionr)   actionr5   r   r   r   \      z-PermissionPolicyChecker.require.<locals>.testr-   )r5   r;   r   r   r:   r   require[      zPermissionPolicyChecker.requirec                    r7   )Nc                    r8   r   )r4   user_has_any_permissionr)   actionsr5   r   r   r   b   r<   z1PermissionPolicyChecker.require_any.<locals>.testr-   )r5   rA   r   r   r@   r   require_anya   r>   z#PermissionPolicyChecker.require_anyN)__name__
__module____qualname____doc__r6   r=   rB   r   r   r   r   r3   R   s
    r3   c                 C   s   t | h dS )z\
    Check if a user has any permission to add, edit, or otherwise manage any
    page.
    >   addlockchangeunlockpublishbulk_delete)r   r?   r)   r   r   r   user_has_any_page_permissionh   s   rM   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_loginWAGTAILADMIN_LOGIN_URLwagtailadmin_login)	login_url)	r   r   r   django.contrib.auth.viewsrN   getattrr   r   get_full_path)r   auth_redirect_to_loginrQ   r   r   r   reject_requestr   s   
rV   c                    r   )Nc              
      s  | j }|jr
t| S |dgrzd  t|dr#|j  |j ntj	t
k t|dV  rSt  | g|R i |}W d    n1 sMw   Y  n| g|R i |}t|dru|j fdd}t|||_|W  d    W  d    W S 1 sw   Y  W d    n1 sw   Y  W n ty   | jddkr t|  Y S w | jddkst| td	 t| S )
Nzwagtailadmin.access_adminwagtail_userprofiler)   renderc              	      sr   t +  r%t   W  d    W  d    S 1 s w   Y   W  d    S 1 s2w   Y  d S r   )override_tzr   )responsepreferred_languagerX   	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   is_anonymousrV   	has_permshasattrrW   get_preferred_languageget_current_time_zoner   	TIME_ZONErY   r   r   rX   types
MethodTyper   r   r   r   r   r   r   )r   r   r   r   rZ   r^   r#   r[   r   decorated_view   sB   


	Nz,require_admin_access.<locals>.decorated_viewr   )r    rg   r   r#   r   require_admin_access   s   8rh   )re   	functoolsr   django.confr   django.core.exceptionsr   django.shortcutsr   django.urlsr   django.utils.timezoner   rY   django.utils.translationr	   r   r   r   wagtail.log_actionsr   wagtail.permissionsr   r   r%   r.   r2   r3   rM   rV   rh   r   r   r   r   <module>   s(    
