
    Bh/                     f   d dl 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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mZ d dlmZ d dlmZ d dlmZ  e	       Zd Z d Z!d Z"ed        Z#d Z$d Z%d Z&d Z'd Z(d Z)d Z*d Z+d Z,e*d        Z-e*d        Z.d$dZ/d$dZ0d  Z1d! Z2d" Z3d# Z4y)%    N)defaultdict)contextmanager)	lru_cachewraps)local)get_permission_codenameget_user_model)Group)Q)ROOT_USER_LEVELSCRIPT_USERNAME)NoPermissionsException)GlobalPagePermissionPagePermission)available_attrs)get_cms_setting)get_clean_usernamec                     | t         _        y)z`
    Assigns current user from request to thread_locals, used by
    CurrentUserMiddleware.
    N)_thread_localsuserr   s    E/home/dcms/DCMS/lib/python3.12/site-packages/cms/utils/permissions.pyset_current_userr      s    
 N    c                  $    t        t        dd      S )z'
    Returns current user, or None
    r   N)getattrr    r   r   get_current_userr      s     >6400r   c                  <    t               } | st        S t        |       S N)r   r   r   )current_users    r   get_current_user_namer"   %   s    #%Ll++r   c              #   T   K   t               }t        |        d t        |       yw)z9
    Changes the current user just within a context.
    N)r   r   )r   old_users     r   r!   r!   -   s#     
  !HT	Xs   &(c                 R    | j                   }|j                  dz   t        ||      z   S )N.)_meta	app_labelr   )modelactionoptss      r   get_model_permission_codenamer,   8   s'    ;;D>>C"9&$"GGGr   c                    | j                   sy| j                  ryt        t        |      }| j	                  |      syt        d      syt        j                  j                  | |j                        j                         }|S )NFTr*   
PERMISSION)
is_authenticatedis_superuserr,   r   has_permr   objectsget_with_change_permissionspkexists)r   siter*   codenamer2   s        r   _has_global_permissionr9   =   so      ,-A&QH=="<( 			$	$T477	3		  Or   c                     t        | |d      S )Naddr.   r9   r   r7   s     r   user_can_add_global_permissionsr>   U   s    !$U;;r   c                     t        | |d      S )Nchanger.   r<   r=   s     r   "user_can_change_global_permissionsrA   Y       !$X>>r   c                     t        | |d      S )Ndeleter.   r<   r=   s     r   "user_can_delete_global_permissionsrE   ]   rB   r   c                    | j                   st        | j                  st        d      st        S t
        j                  j                  | |j                        j                         }|rt        S 	 t        j                  j                  | |      j                  d      j                  d      d   }|j                  j                  S # t        $ r t        w xY w)a  
    Returns highest user level from the page/permission hierarchy on which
    user haves can_change_permission. Also takes look into user groups. Higher
    level equals to lower number. Users on top of hierarchy have level 0. Level
    is the same like page.depth attribute.

    Example:
                              A,W                    level 0
                            /                              user    B,GroupE           level 1
                        /                           C,X     D,Y,W                  level 2

        Users A, W have user level 0. GroupE and all his users have user level 1
        If user D is a member of GroupE, his user level will be 1, otherwise is
        2.

    r/   page
page__pathr   )r0   r   r1   r   r   r   r3   r4   r5   r6   r   select_relatedorder_by
IndexErrorrG   depth)r   r7   has_global_perms
permissions       r   get_user_permission_levelrO   a   s    &   $$ = 			$	$T477	3		  
%W((t4^F#Xl#

 ??     %$$%s   1AC Cc                 R     t         t                      fd       } |_        |S )N)assignedc                     dj                   z  }t        | |      s t        d             }t        | ||        t	        | |      | g|i |S )N_djangocms_cached_func_%s)maxsize)__name__hasattrr   setattrr   )r   argskwargsfunc_cache_namecached_funcfuncs        r   r[   z cached_func.<locals>.cached_func   sV    5Et_-1)D1$7KD/;7-wt_-dDTDVDDr   )r   r   without_cache)r\   r[   s   ` r   r[   r[      s3    
4/$/0E 1E !%Kr   c                 T    d|j                   z  }t        | |      rt        | |       y y )NrS   )rU   rV   delattr)r   r\   rZ   s      r   clear_func_cacher`      s)    1DMMAOt_%o& &r   c                 D    t        | t               t        | t               y)zC
    Clear all python lru caches used by the permission system
    N)r`   get_global_actions_for_userget_page_actions_for_userr   s    r   clear_permission_lru_cachesrd      s     T67T45r   c                     t               }t        j                  j                  | |j                        }|j                         D ]!  }|j                  |j                                # |S r    )setr   r3   get_with_siter5   iteratorupdateget_configured_actions)r   r7   actionsglobal_permsglobal_perms        r   rb   rb      sZ    eG		tTWW	%  $,,. ={99;<=Nr   c                 `   t        t              }t        j                  j	                  |       j                  d      j                  |      }|j                         D ]M  }|j                  |j                  j                  f}|j                         D ]  }||   j                  |        O |S )NrG   )
page__site)r   listr   r3   	with_userrI   filterrh   grant_onrG   pathrj   append)r   r7   rk   page_permissionspermpermission_tupler*   s          r   rc   rc      s    $G 			4			4	   !))+ 5==$))..8113 	5FFO""#34	55 Nr   c                 Z    |rt        | |      }||v S t         j                  | |      }||v S r    )rb   r]   )r   r7   r*   	use_cacherk   s        r   has_global_permissionr{      s=    -dD9 W .;;D$GWr   c                     dd l }ddlm} ddlm}  |j
                  d|d       ddd	d
ddd}||v r||   } ||| ||j                  d|      S )Nr   RemovedInDjangoCMS51Warning)has_generic_permissionzahas_page_permission is deprecated. Use cms.utils.page_permissions.has_generic_permission instead.   
stacklevelchange_pageadd_page	move_pagepublish_pagedelete_page	view_page)r@   r;   movepublishrD   viewF)r7   check_globalrz   )warningscms.utils.compat.warningsr~   cms.utils.page_permissionsr   warnr7   )r   rG   r*   rz   r   r~   r   
action_maps           r   has_page_permissionr      sn    EAHMM S-!=
  !J F#!$f499SXdmnnr   c                 d   ddl m} 	 t        | |      }|t        k(  r"t	               j
                  j                         S dd
lm} t               } || |d      D ]  }| ||      j                  d      z  } t	               j
                  j                         j                  t        d      |t        |      z  z  t        |       t        d      z  z        }|j                  | j                        j                  | j                  	      }|S # t        $ r t	               j
                  j                         j                  t        d      t        |       z  t        d      z        }|j                  | j                        j                  | j                  	      }|cY S w xY w)a#  
    Returns users queryset, containing all subordinate users to given user
    including users created by given user and not assigned to any page.

    Not assigned users must be returned, because they shouldn't get lost, and
    user should still have possibility to see them.

    Only users created_by given user which are on the same, or lover level are
    returned.

    If user haves global permissions or is a superuser, then he can see all the
    users.

    This function is currently used in PagePermissionInlineAdminForm for limit
    users in permission combobox.

    Example:
                              A,W                    level 0
                            /                              user    B,GroupE           level 1
                Z       /                           C,X     D,Y,W                  level 2

        Rules: W was created by user, Z was created by user, but is not assigned
        to any page.

        Will return [user, C, X, D, Y, Z]. W was created by user, but is also
        assigned to higher level.
    r   "get_change_permissions_perm_tuplesT)is_staff)pageuser__created_byN)pagepermission__page)r5   )groups__user__pkPermissionTupleFr   r    pagepermission__page__depth__gte)r   r   rO   r   r	   r3   distinctrr   r   excluder5   r   all
cms.modelsr   
allow_list)r   r7   r   
user_levelqsr   r   
perm_tuples           r   get_subordinate_usersr      s~   < N
.tT:
 _$''++--*J8tRWX U
oj1<<=STT
U 
		!	!	*	*	,	3	3	4JGG	
 4(1$+GG	

B 
tww		'	'	'	ABI7 "  %%..077tqd;;aUY>ZZ
 ZZ477Z#++TWW+E	s   D BF/.F/c                 <   ddl m} 	 t        | |      }|t        k(  rt        j
                  j                         S ddlm} t               } || |d      D ]  }| ||      j                  d	      z  } t        j
                  j                         j                  |t        |
      z  t        |       t        d      z  z        S # t        $ rI t        j
                  j                  t        |       t        d      z        j                         }|cY S w xY w)z`
    Similar to get_subordinate_users, but returns queryset of Groups instead
    of Users.
    r   r   )pageusergroup__created_byT)pagepermission__page__isnullr   Fr   r   r   )r   r   rO   r   r
   r3   rr   r   r   r   r   r   r   r   )r   r7   r   r   groupsr   r   r   s           r   get_subordinate_groupsr   0  s	   
 N.tT:
" _$}}  ""*J8tRWX U
oj1<<=STT
U ==!!#**JGG-t0TT	
 1 " 
 WVD1ASW4XX XZ 	 s   C	 	ADDc                    ddl m} t        j                  d|d       t	        t
              }t        d      s|S | s|S i }| D ]2  }|j                         r|j                  |        |||j                  <   4 t        j                  j                  |d      }|D ]b  }t        j                  j                  j                  |||j                             |j#                         D ]  }||   j%                  |        d |S )	z2
    Load all view restrictions for the pages
    r   r}   z%get_view_restrictions will be removedr   r   r/   T)page__incan_view)r   r~   r   r   r   rp   r   is_root_set_hierarchyr5   r   r3   rr   rG   fieldset_cached_valuepage_id_get_page_idsru   )pagesr~   restricted_pagespages_by_idrG   rv   rw   r   s           r   get_view_restrictionsr   Z  s   
 FMM9-!= #4(<(K $<<>&#DGG$
 &--44 5 
 ! 3!!224T\\9RS))+ 	3GW%,,T2	3	3 r   c                     ddl m} 	 |j                  |      }t        |j                  |      }| j                  |      S # t        $ r Y yw xY w)z
    Checks that a user has permissions for the plugin-type given to perform
    the action defined in permission_type
    permission_type should be 'add', 'change' or 'delete'.
    r   )plugin_poolr.   T)cms.plugin_poolr   
get_pluginr,   r)   r2   KeyError)r   plugin_typepermission_typer   plugin_classr8   s         r   has_plugin_permissionr     sW     ,
"--k:0"
 }}X&&  s   8A 	AA)T)5r   collectionsr   
contextlibr   	functoolsr   r   	threadingr   django.contrib.authr   r	   django.contrib.auth.modelsr
   django.db.modelsr   cms.constantsr   r   cms.exceptionsr   r   r   r   cms.utils.compat.djr   cms.utils.confr   cms.utils.pager   r   r   r   r"   r!   r,   r9   r>   rA   rE   rO   r[   r`   rd   rb   rc   r{   r   r   r   r   r   r   r   r   <module>r      s     # % &  G ,  : 1 ; / * - 1,  H
0<??.!b'6 
 
  $o0=@'T$Nr   