
    jk                     b    d dl Z d dlZd dlmZ d dlmZ  G d d          Z G d d          ZdS )    N)Enforcer)RWLockWritec                   J    e Zd Zd Zed             Zej        d             ZdS )
AtomicBoolc                 D    t          j                    | _        || _        d S N)	threadingLock_lock_valueselfvalues     `/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/casbin/synced_enforcer.py__init__zAtomicBool.__init__   s    ^%%
    c                 R    | j         5  | j        cd d d            S # 1 swxY w Y   d S r   r   r   r   s    r   r   zAtomicBool.value   ss    Z 	 	;	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s     c                 T    | j         5  || _        d d d            d S # 1 swxY w Y   d S r   r   r   s     r   r   zAtomicBool.value    su    Z 	  	 DK	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   !!N)__name__
__module____qualname__r   propertyr   setter r   r   r   r      s[             X \    \     r   r   c                      e Zd ZdZdpdZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d  Z!d! Z"d" Z#d# Z$d$ Z%d% Z&d& Z'd' Z(d( Z)d) Z*d* Z+d+ Z,d, Z-d- Z.d. Z/d/ Z0d0 Z1d1 Z2d2 Z3d3 Z4d4 Z5d5 Z6d6 Z7d7 Z8d8 Z9d9 Z:d: Z;d; Z<d< Z=d= Z>d> Z?d? Z@d@ ZAdA ZBdB ZCdC ZDdD ZEdE ZFdF ZGdG ZHdH ZIdIdJdKZJdIdJdLZKdM ZLdN ZMdO ZNdP ZOdQ ZPdR ZQdS ZRdT ZSdU ZTdqdVZUdW ZVdX ZWdY ZXdZ ZYd[ ZZd\ Z[d] Z\d^ Z]d_ Z^d` Z_da Z`db Zadc Zbdd Zcde Zddf ZedgefdhdifdjZgdk Zhdl Zidm Zjdn Zkdo ZldS )rSyncedEnforcerzpSyncedEnforcer wraps Enforcer and provides synchronized access.
    It's also a drop-in replacement for EnforcerNc                    t          ||          | _        t                      | _        | j                                        | _        | j                                        | _        t          d          | _	        d | _
        d S )NF)r   _er   _rwlock	gen_rlock_rl	gen_wlock_wlr   _auto_loading_auto_loading_thread)r   modeladapters      r   r   zSyncedEnforcer.__init__+   sf    5'**"}}<))++<))++'..$(!!!r   c                     | j         j        S )z0check if SyncedEnforcer is auto loading policies)r&   r   r   s    r   is_auto_loading_runningz&SyncedEnforcer.is_auto_loading_running3   s    !''r   c                 4   |                                  rt          j        |           	 |                                  nC# t          $ r6}| j        j                            t          |                     Y d }~nd }~ww xY w|                                  d S d S r   )	r+   timesleepload_policy	Exceptionr    loggererrorrepr)r   intervales      r   _auto_load_policyz SyncedEnforcer._auto_load_policy7   s    **,, 	.Jx   .  """" . . .$$T!WW--------.	 **,, 	. 	. 	. 	. 	.s   ? 
A?	,A::A?c                     |                                  rdS d| j        _        t          j        | j        |gd          | _        | j                                         dS )zAstarts a thread that will call load_policy every interval secondsNT)targetargsdaemon)r+   r&   r   r	   Threadr6   r'   start)r   r4   s     r   start_auto_load_policyz%SyncedEnforcer.start_auto_load_policy?   sc    '')) 	F#' $-$4D<RZbYclp$q$q$q!!'')))))r   c                 J    |                                  rd| j        _        dS dS )z2stops the thread started by start_auto_load_policyFN)r+   r&   r   r   s    r   stop_auto_load_policyz$SyncedEnforcer.stop_auto_load_policyG   s1    '')) 	-',D$$$	- 	-r   c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS )zgets the current model.N)r#   r    	get_modelr   s    r   rA   zSyncedEnforcer.get_modelL   s    X 	' 	'7$$&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	'   .22c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )zsets the current model.N)r%   r    	set_model)r   ms     r   rD   zSyncedEnforcer.set_modelQ   s    X 	( 	(7$$Q''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(   /33c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS )zreloads the model from the model CONF file.
        Because the policy is attached to a model, so the policy is invalidated and needs to be reloaded by calling LoadPolicy().
        N)r%   r    
load_modelr   s    r   rH   zSyncedEnforcer.load_modelV   s     X 	( 	(7%%''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(rB   c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS )zgets the current role manager.N)r#   r    get_role_managerr   s    r   rJ   zSyncedEnforcer.get_role_manager]       X 	. 	.7++--	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.rB   c                 z    | j         5  | j                            |           d d d            d S # 1 swxY w Y   d S r   )r%   r    set_role_manager)r   rms     r   rM   zSyncedEnforcer.set_role_managerb   s    X 	) 	)G$$R(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)   044c                 x    | j         5  | j                                         ddd           dS # 1 swxY w Y   dS )zgets the current adapter.N)r#   r    get_adapterr   s    r   rQ   zSyncedEnforcer.get_adapterf       X 	" 	"G!!!	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"rF   c                 z    | j         5  | j                            |           ddd           dS # 1 swxY w Y   dS )zsets the current adapter.N)r%   r    set_adapter)r   r)   s     r   rT   zSyncedEnforcer.set_adapterk       X 	) 	)G(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)rO   c                 z    | j         5  | j                            |           ddd           dS # 1 swxY w Y   dS )zsets the current watcher.N)r%   r    set_watcher)r   watchers     r   rW   zSyncedEnforcer.set_watcherp   rU   rO   c                 z    | j         5  | j                            |           ddd           dS # 1 swxY w Y   dS )zsets the current effector.N)r%   r    set_effector)r   efts     r   rZ   zSyncedEnforcer.set_effectoru   s    X 	& 	&G  %%%	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	& 	&rO   c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS )zclears all policy.N)r%   r    clear_policyr   s    r   r]   zSyncedEnforcer.clear_policyz   s    X 	* 	*7''))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*rB   c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS )z&reloads the policy from file/database.N)r%   r    r/   r   s    r   r/   zSyncedEnforcer.load_policy   s    X 	) 	)7&&((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)rB   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )z/ "reloads a filtered policy from file/database.N)r%   r    load_filtered_policy)r   filters     r   r`   z#SyncedEnforcer.load_filtered_policy   s    X 	8 	87//77	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8rF   c                 v    | j         5  | j                                        cd d d            S # 1 swxY w Y   d S r   )r#   r    save_policyr   s    r   rc   zSyncedEnforcer.save_policy   s    X 	) 	)7&&((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)rB   c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS )z0manually rebuild the role inheritance relations.N)r#   r    build_role_linksr   s    r   re   zSyncedEnforcer.build_role_links   rK   rB   c                 b    | j         5   | j        j        | cddd           S # 1 swxY w Y   dS )zdecides whether a "subject" can access a "object" with the operation "action",
        input parameters are usually: (sub, obj, act).
        N)r#   r    enforcer   rvalss     r   rg   zSyncedEnforcer.enforce   s~     X 	+ 	+"47?E*	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+   $((c                 b    | j         5   | j        j        | cddd           S # 1 swxY w Y   dS )zdecides whether a "subject" can access a "object" with the operation "action",
        input parameters are usually: (sub, obj, act).
        return judge result with reason
        N)r#   r    
enforce_exrh   s     r   rl   zSyncedEnforcer.enforce_ex   s    
 X 	. 	.%47%u-	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	. 	.rj   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )zybatch_enforce enforce in batches,
        input parameters are usually: [(sub, obj, act), (sub, obj, act), ...].
        N)r#   r    batch_enforcerh   s     r   rn   zSyncedEnforcer.batch_enforce   s     X 	0 	07((//	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0rF   c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS )z=gets the list of subjects that show up in the current policy.N)r#   r    get_all_subjectsr   s    r   rp   zSyncedEnforcer.get_all_subjects   rK   rB   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )zCgets the list of subjects that show up in the current named policy.N)r#   r    get_all_named_subjectsr   ptypes     r   rr   z%SyncedEnforcer.get_all_named_subjects   s    X 	9 	9711%88	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9rF   c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS )z<gets the list of objects that show up in the current policy.N)r#   r    get_all_objectsr   s    r   rv   zSyncedEnforcer.get_all_objects       X 	- 	-7**,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-rB   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )zBgets the list of objects that show up in the current named policy.N)r#   r    get_all_named_objectsrs   s     r   ry   z$SyncedEnforcer.get_all_named_objects       X 	8 	870077	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8rF   c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS )z<gets the list of actions that show up in the current policy.N)r#   r    get_all_actionsr   s    r   r|   zSyncedEnforcer.get_all_actions   rw   rB   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )zBgets the list of actions that show up in the current named policy.N)r#   r    get_all_named_actionsrs   s     r   r~   z$SyncedEnforcer.get_all_named_actions   rz   rF   c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS )z@gets the list of roles that show up in the current named policy.N)r#   r    get_all_rolesr   s    r   r   zSyncedEnforcer.get_all_roles   s    X 	+ 	+7((**	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+rB   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS z/gets all the authorization rules in the policy.N)r#   r    get_all_named_rolesrs   s     r   r   z"SyncedEnforcer.get_all_named_roles   s    X 	6 	67..u55	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6rF   c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS r   )r#   r    
get_policyr   s    r   r   zSyncedEnforcer.get_policy   s    X 	( 	(7%%''	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(rB   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )zOgets all the authorization rules in the policy, field filters can be specified.N)r#   r    get_filtered_policyr   field_indexfield_valuess      r   r   z"SyncedEnforcer.get_filtered_policy   s    X 	K 	K.47.{J\JJJ	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K   (,,c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )z5gets all the authorization rules in the named policy.N)r#   r    get_named_policyrs   s     r   r   zSyncedEnforcer.get_named_policy   s    X 	3 	37++E22	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3rF   c                 l    | j         5   | j        j        ||g|R  cddd           S # 1 swxY w Y   dS )zUgets all the authorization rules in the named policy, field filters can be specified.N)r#   r    get_filtered_named_policyr   rt   r   r   s       r   r   z(SyncedEnforcer.get_filtered_named_policy   s    X 	X 	X4474UKW,WWW	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X   )--c                 v    | j         5  | j                                        cddd           S # 1 swxY w Y   dS z2gets all the role inheritance rules in the policy.N)r#   r    get_grouping_policyr   s    r   r   z"SyncedEnforcer.get_grouping_policy   s    X 	1 	17..00	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1 	1rB   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS zRgets all the role inheritance rules in the policy, field filters can be specified.N)r#   r    get_filtered_grouping_policyr   s      r   r   z+SyncedEnforcer.get_filtered_grouping_policy   s    X 	T 	T7477SlSSS	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	T 	Tr   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS r   )r#   r    get_named_grouping_policyrs   s     r   r   z(SyncedEnforcer.get_named_grouping_policy   s    X 	< 	<744U;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<rF   c                 l    | j         5   | j        j        ||g|R  cddd           S # 1 swxY w Y   dS r   )r#   r    "get_filtered_named_grouping_policyr   s       r   r   z1SyncedEnforcer.get_filtered_named_grouping_policy   s    X 	a 	a=47=e[`S_```	a 	a 	a 	a 	a 	a 	a 	a 	a 	a 	a 	a 	a 	a 	a 	a 	a 	ar   c                 b    | j         5   | j        j        | cddd           S # 1 swxY w Y   dS )z0determines whether an authorization rule exists.N)r#   r    
has_policyr   paramss     r   r   zSyncedEnforcer.has_policy   s}    X 	/ 	/%47%v.	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/rj   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )z5determines whether a named authorization rule exists.N)r#   r    has_named_policyr   rt   r   s      r   r   zSyncedEnforcer.has_named_policy   s    X 	< 	<+47+E;F;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<r   c                 b    | j         5   | j        j        | cddd           S # 1 swxY w Y   dS )zadds an authorization rule to the current policy.
        If the rule already exists, the function returns false and the rule will not be added.
        Otherwise the function returns true by adding the new rule.
        N)r%   r    
add_policyr   s     r   r   zSyncedEnforcer.add_policy  s    
 X 	/ 	/%47%v.	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/rj   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )zadds an authorization rule to the current named policy.
        If the rule already exists, the function returns false and the rule will not be added.
        Otherwise the function returns true by adding the new rule.
        N)r%   r    add_named_policyr   s      r   r   zSyncedEnforcer.add_named_policy
  s    
 X 	< 	<+47+E;F;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<r   c                 b    | j         5   | j        j        | cddd           S # 1 swxY w Y   dS )z6removes an authorization rule from the current policy.N)r%   r    remove_policyr   s     r   r   zSyncedEnforcer.remove_policy  s}    X 	2 	2(47(&1	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2rj   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )zVremoves an authorization rule from the current policy, field filters can be specified.N)r%   r    remove_filtered_policyr   s      r   r   z%SyncedEnforcer.remove_filtered_policy  s    X 	N 	N1471+MMMM	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	N 	Nr   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )z<removes an authorization rule from the current named policy.N)r%   r    remove_named_policyr   s      r   r   z"SyncedEnforcer.remove_named_policy  s    X 	? 	?.47.u>v>>>	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?r   c                 l    | j         5   | j        j        ||g|R  cddd           S # 1 swxY w Y   dS )z\removes an authorization rule from the current named policy, field filters can be specified.N)r%   r    remove_filtered_named_policyr   s       r   r   z+SyncedEnforcer.remove_filtered_named_policy!  s    X 	[ 	[7477{Z\ZZZ	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[r   c                 b    | j         5   | j        j        | cddd           S # 1 swxY w Y   dS )z2determines whether a role inheritance rule exists.N)r#   r    has_grouping_policyr   s     r   r   z"SyncedEnforcer.has_grouping_policy&  s}    X 	8 	8.47.7	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8rj   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )z8determines whether a named role inheritance rule exists.N)r#   r    has_named_grouping_policyr   s      r   r   z(SyncedEnforcer.has_named_grouping_policy+  s    X 	E 	E4474UDVDDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Er   c                 b    | j         5   | j        j        | cddd           S # 1 swxY w Y   dS )zadds a role inheritance rule to the current policy.
        If the rule already exists, the function returns false and the rule will not be added.
        Otherwise the function returns true by adding the new rule.
        N)r%   r    add_grouping_policyr   s     r   r   z"SyncedEnforcer.add_grouping_policy0  s    
 X 	8 	8.47.7	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8rj   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )zadds a named role inheritance rule to the current policy.
        If the rule already exists, the function returns false and the rule will not be added.
        Otherwise the function returns true by adding the new rule.
        N)r%   r    add_named_grouping_policyr   s      r   r   z(SyncedEnforcer.add_named_grouping_policy8  s    
 X 	E 	E4474UDVDDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	Er   c                 b    | j         5   | j        j        | cddd           S # 1 swxY w Y   dS )z8removes a role inheritance rule from the current policy.N)r%   r    remove_grouping_policyr   s     r   r   z%SyncedEnforcer.remove_grouping_policy@  s}    X 	; 	;14716:	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;rj   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )zXremoves a role inheritance rule from the current policy, field filters can be specified.N)r%   r    remove_filtered_grouping_policyr   s      r   r   z.SyncedEnforcer.remove_filtered_grouping_policyE  s    X 	W 	W:47:;VVVV	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	Wr   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )z>removes a role inheritance rule from the current named policy.N)r%   r    remove_named_grouping_policyr   s      r   r   z+SyncedEnforcer.remove_named_grouping_policyJ  s    X 	H 	H7477GGGG	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	Hr   c                 l    | j         5   | j        j        ||g|R  cddd           S # 1 swxY w Y   dS )z^removes a role inheritance rule from the current named policy, field filters can be specified.N)r%   r    %remove_filtered_named_grouping_policyr   s       r   r   z4SyncedEnforcer.remove_filtered_named_grouping_policyO  s    X 	d 	d@47@cVbccc	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	dr   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )zadds a customized function.N)r%   r    add_function)r   namefuncs      r   r   zSyncedEnforcer.add_functionT  s    X 	4 	47''d33	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4rO   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )zgets the roles that a user has.N)r#   r    get_roles_for_userr   r   s     r   r   z!SyncedEnforcer.get_roles_for_user[      X 	4 	47--d33	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4rF   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )zgets the users that has a role.N)r#   r    get_users_for_roler   s     r   r   z!SyncedEnforcer.get_users_for_role`  r   rF   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )z%determines whether a user has a role.N)r#   r    has_role_for_user)r   r   roles      r   r   z SyncedEnforcer.has_role_for_usere  s    X 	9 	97,,T488	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9rO   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )zt
        adds a role for a user.
        Returns false if the user already has the role (aka not affected).
        N)r%   r    add_role_for_userr   userr   s      r   r   z SyncedEnforcer.add_role_for_userj  s    
 X 	9 	97,,T488	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9rO   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )zy
        deletes a role for a user.
        Returns false if the user does not have the role (aka not affected).
        N)r%   r    delete_role_for_userr   s      r   r   z#SyncedEnforcer.delete_role_for_userr  s    
 X 	< 	<7//d;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<rO   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )z}
        deletes all roles for a user.
        Returns false if the user does not have any roles (aka not affected).
        N)r%   r    delete_roles_for_userr   r   s     r   r   z$SyncedEnforcer.delete_roles_for_userz  s    
 X 	7 	770066	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7rF   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )zf
        deletes a user.
        Returns false if the user does not exist (aka not affected).
        N)r%   r    delete_userr   s     r   r   zSyncedEnforcer.delete_user      
 X 	- 	-7&&t,,	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	- 	-rF   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )zf
        deletes a role.
        Returns false if the role does not exist (aka not affected).
        N)r%   r    delete_role)r   r   s     r   r   zSyncedEnforcer.delete_role  r   rF   c                 b    | j         5   | j        j        | cddd           S # 1 swxY w Y   dS )zr
        deletes a permission.
        Returns false if the permission does not exist (aka not affected).
        N)r%   r    delete_permissionr   
permissions     r   r   z SyncedEnforcer.delete_permission  s    
 X 	: 	:,47,j9	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:rj   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )z
        adds a permission for a user or role.
        Returns false if the user or role already has the permission (aka not affected).
        N)r%   r    add_permission_for_userr   r   r   s      r   r   z&SyncedEnforcer.add_permission_for_user  s    
 X 	F 	F24724E*EEE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	Fr   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )z
        deletes a permission for a user or role.
        Returns false if the user or role does not have the permission (aka not affected).
        N)r%   r    delete_permission_for_userr   s      r   r   z)SyncedEnforcer.delete_permission_for_user  s    
 X 	I 	I5475dHZHHH	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	Ir   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )z
        deletes permissions for a user or role.
        Returns false if the user or role does not have any permissions (aka not affected).
        N)r%   r    delete_permissions_for_userr   s     r   r   z*SyncedEnforcer.delete_permissions_for_user  s    
 X 	= 	=766t<<	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=rF   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )z6
        gets permissions for a user or role.
        N)r#   r    get_permissions_for_userr   s     r   r   z'SyncedEnforcer.get_permissions_for_user  s     X 	: 	:733D99	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	:rF   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )z=
        determines whether a user has a permission.
        N)r#   r    has_permission_for_userr   s      r   r   z&SyncedEnforcer.has_permission_for_user  s     X 	F 	F24724E*EEE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	Fr   c                 j    | j         5   | j        j        |g|R  cddd           S # 1 swxY w Y   dS )a  
        gets implicit roles that a user has.
        Compared to get_roles_for_user(), this function retrieves indirect roles besides direct roles.
        For example:
        g, alice, role:admin
        g, role:admin, role:user

        get_roles_for_user("alice") can only get: ["role:admin"].
        But get_implicit_roles_for_user("alice") will get: ["role:admin", "role:user"].
        N)r#   r    get_implicit_roles_for_userr   r   domains      r   r   z*SyncedEnforcer.get_implicit_roles_for_user  s     X 	F 	F6476tEfEEE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	Fr   T)filter_policy_domc                p    | j         5   | j        j        |g|R d|icddd           S # 1 swxY w Y   dS )a  
        gets implicit permissions for a user or role.
        Compared to get_permissions_for_user(), this function retrieves permissions for inherited roles.
        For example:
        p, admin, data1, read
        p, alice, data2, read
        g, alice, admin

        get_permissions_for_user("alice") can only get: [["alice", "data2", "read"]].
        But get_implicit_permissions_for_user("alice") will get: [["admin", "data1", "read"], ["alice", "data2", "read"]].
        r   N)r#   r    !get_implicit_permissions_for_user)r   r   r   r   s       r   r   z0SyncedEnforcer.get_implicit_permissions_for_user  s     X 	q 	q<47<TpFppp^opp	q 	q 	q 	q 	q 	q 	q 	q 	q 	q 	q 	q 	q 	q 	q 	q 	q 	q   +//c                r    | j         5   | j        j        ||g|R d|icddd           S # 1 swxY w Y   dS )a  
        gets implicit permissions for a user or role by named policy.
        Compared to get_permissions_for_user(), this function retrieves permissions for inherited roles.
        For example:
        p, admin, data1, read
        p, alice, data2, read
        g, alice, admin

        get_permissions_for_user("alice") can only get: [["alice", "data2", "read"]].
        But get_implicit_permissions_for_user("alice") will get: [["admin", "data1", "read"], ["alice", "data2", "read"]].
        r   N)r#   r    'get_named_implicit_permissions_for_user)r   rt   r   r   r   s        r   r   z6SyncedEnforcer.get_named_implicit_permissions_for_user  s     X 	 	B47Bt$  8I 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	   ,00c                 b    | j         5   | j        j        | cddd           S # 1 swxY w Y   dS )aJ  
        gets implicit users for a permission.
        For example:
        p, admin, data1, read
        p, bob, data1, read
        g, alice, admin

        get_implicit_users_for_permission("data1", "read") will get: ["alice", "bob"].
        Note: only users will be returned, roles (2nd arg in "g") will be excluded.
        N)r#   r    !get_implicit_users_for_permissionr   s     r   r   z0SyncedEnforcer.get_implicit_users_for_permission  s     X 	J 	J<47<jI	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	Jrj   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )z/gets the roles that a user has inside a domain.N)r#   r    get_roles_for_user_in_domainr   s      r   r   z+SyncedEnforcer.get_roles_for_user_in_domain      X 	F 	F777fEE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	FrO   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )z/gets the users that has a role inside a domain.N)r#   r    get_users_for_role_in_domainr   s      r   r   z+SyncedEnforcer.get_users_for_role_in_domain  r   rO   c                 ~    	 | j         5  | j                            |||          cddd           S # 1 swxY w Y   dS )z'adds a role for a user inside a domain.N)r%   r    add_role_for_user_in_domainr   r   r   r   s       r   r   z*SyncedEnforcer.add_role_for_user_in_domain  s    PX 	K 	K766tT6JJ	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K 	K   266c                 ~    	 | j         5  | j                            |||          cddd           S # 1 swxY w Y   dS )z*deletes a role for a user inside a domain.N)r%   r    delete_roles_for_user_in_domainr   s       r   r   z.SyncedEnforcer.delete_roles_for_user_in_domain  s    SX 	O 	O7::4vNN	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	Or   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )z2gets permissions for a user or role inside domain.N)r#   r    "get_permissions_for_user_in_domain)r   r   r   s      r   r  z1SyncedEnforcer.get_permissions_for_user_in_domain  s    X 	L 	L7==dFKK	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	L 	LrO   c                 |    | j         5  | j                            |||          cddd           S # 1 swxY w Y   dS )zBgets permissions for a user or role by named policy inside domain.N)r#   r    (get_named_permissions_for_user_in_domain)r   rt   r   r   s       r   r  z7SyncedEnforcer.get_named_permissions_for_user_in_domain  s    X 	Y 	Y7CCE4QWXX	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y 	Y   155c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )z[controls whether to rebuild the role inheritance relations when a role is added or deleted.N)r%   r    enable_auto_build_role_links)r   auto_build_role_linkss     r   r  z+SyncedEnforcer.enable_auto_build_role_links  s    X 	O 	O7778MNN	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	OrF   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )z`controls whether to save a policy rule automatically to the adapter when it is added or removed.N)r%   r    enable_auto_save)r   	auto_saves     r   r
  zSyncedEnforcer.enable_auto_save!  s    X 	7 	77++I66	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7rF   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )zchanges the enforcing state of Casbin,
        when Casbin is disabled, all access will be allowed by the Enforce() function.
        N)r%   r    enable_enforce)r   enableds     r   r  zSyncedEnforcer.enable_enforce&  s     X 	3 	37))'22	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3 	3rF   c                 |    | j         5  | j                            ||           ddd           dS # 1 swxY w Y   dS )z=add_named_matching_func add MatchingFunc by ptype RoleManagerN)r%   r    add_named_matching_funcr   rt   fns      r   r  z&SyncedEnforcer.add_named_matching_func-  s    X 	7 	7G++E2666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7r  c                     | j         5  | j                            ||||           ddd           dS # 1 swxY w Y   dS )zuAdd condition function fn for Link userName->roleName,
        when fn returns true, Link is valid, otherwise invalidN)r%   r    add_named_link_condition_func)r   rt   r   r   r  s        r   r  z,SyncedEnforcer.add_named_link_condition_func2  s     X 	I 	IG11%tRHHH	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	Is   377c                 |    | j         5  | j                            ||           ddd           dS # 1 swxY w Y   dS )zGadd_named_domain_matching_func add MatchingFunc by ptype to RoleManagerN)r%   r    add_named_domain_matching_funcr  s      r   r  z-SyncedEnforcer.add_named_domain_matching_func8  s    X 	> 	>G225"===	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>r  c                     | j         5  | j                            |||||           ddd           dS # 1 swxY w Y   dS )zAdd condition function fn for Link userName-> {roleName, domain},
        when fn returns true, Link is valid, otherwise invalidN)r%   r    $add_named_domain_link_condition_func)r   rt   r   r   r   r  s         r   r  z3SyncedEnforcer.add_named_domain_link_condition_func=  s     X 	X 	XG88dFTVWWW	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	X 	Xs   488c                 r    | j         5   | j        j        ||||g|R   ddd           dS # 1 swxY w Y   dS )zVSets the parameters of the condition function fn for Link userName->{roleName, domain}N)r%   r    +set_named_domain_link_condition_func_params)r   rt   r   r   r   r   s         r   r  z:SyncedEnforcer.set_named_domain_link_condition_func_paramsC  s    X 	d 	d?DG?tTSYc\bcccc	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	d 	dr   c                 p    | j         5   | j        j        |||g|R   ddd           dS # 1 swxY w Y   dS )zLSets the parameters of the condition function fn for Link userName->roleNameN)r%   r    $set_named_link_condition_func_params)r   rt   r   r   r   s        r   r  z3SyncedEnforcer.set_named_link_condition_func_paramsH  s    X 	U 	U8DG8dTVTTTT	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	Ur   c                 x    | j         5  | j                                         ddd           dS # 1 swxY w Y   dS )z4returns true if the loaded policy has been filtered.N)r#   r    is_filteredr   s    r   r  zSyncedEnforcer.is_filteredM  rR   rF   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )a  adds authorization rules to the current policy.

        If the rule already exists, the function returns false for the corresponding rule and the rule will not be added.
        Otherwise the function returns true for the corresponding rule by adding the new rule.
        N)r%   r    add_policiesr   ruless     r   r   zSyncedEnforcer.add_policiesR  s     X 	/ 	/7''..	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/rF   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )a
  adds authorization rules to the current named policy.

        If the rule already exists, the function returns false for the corresponding rule and the rule will not be added.
        Otherwise the function returns true for the corresponding by adding the new rule.N)r%   r    add_named_policiesr   rt   r"  s      r   r$  z!SyncedEnforcer.add_named_policies[  s    
 X 	< 	<7--eU;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	<rO   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )z4removes authorization rules from the current policy.N)r%   r    remove_policiesr!  s     r   r'  zSyncedEnforcer.remove_policiesc  s    X 	2 	27**511	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2rF   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )z:removes authorization rules from the current named policy.N)r%   r    remove_named_policiesr%  s      r   r)  z$SyncedEnforcer.remove_named_policiesh  s    X 	? 	?700>>	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	?rO   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )a#  adds role inheritance rules to the current policy.

        If the rule already exists, the function returns false for the corresponding policy rule and the rule will not be added.
        Otherwise the function returns true for the corresponding policy rule by adding the new rule.
        N)r%   r    add_grouping_policiesr!  s     r   r+  z$SyncedEnforcer.add_grouping_policiesm  s     X 	8 	870077	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8 	8rF   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )a"   "adds named role inheritance rules to the current policy.

        If the rule already exists, the function returns false for the corresponding policy rule and the rule will not be added.
        Otherwise the function returns true for the corresponding policy rule by adding the new rule.N)r%   r    add_named_grouping_policiesr%  s      r   r-  z*SyncedEnforcer.add_named_grouping_policiesv  s    
 X 	E 	E766ueDD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	ErO   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )z7removes role inheritance rules from the current policy.N)r%   r    remove_grouping_policiesr!  s     r   r/  z'SyncedEnforcer.remove_grouping_policies~  s    X 	; 	;733E::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;rF   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )z=removes role inheritance rules from the current named policy.N)r%   r    remove_named_grouping_policiesr%  s      r   r1  z-SyncedEnforcer.remove_named_grouping_policies  s    X 	H 	H799%GG	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	H 	HrO   c                     |                                                      |                                 |d||           d S )Ng)rA   build_incremental_role_linksrJ   )r   oprt   r"  s       r   r4  z+SyncedEnforcer.build_incremental_role_links  s=    55d6K6K6M6MrSVX]_deeeeer   suffixreturnEnforceContextc                 6    | j                             |          S r   )r    new_enforce_context)r   r6  s     r   r:  z"SyncedEnforcer.new_enforce_context  s    w**6222r   c                 B    | j         j                            ||          S )z!gets the index of the field name.)r    r(   get_field_index)r   rt   fields      r   r<  zSyncedEnforcer.get_field_index  s    w},,UE:::r   c                 J    | j         j        d         |         }||j        |<   dS )z!sets the index of the field name.pN)r    r(   field_index_map)r   rt   r=  index	assertions        r   set_field_indexzSyncedEnforcer.set_field_index  s(    GM#&u-	+0	!%(((r   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )zgets all roles associated with the domain.
        note: Not applicable to Domains with inheritance relationship  (implicit roles)N)r#   r    get_all_roles_by_domain)r   r   s     r   rE  z&SyncedEnforcer.get_all_roles_by_domain  s     X 	; 	;7226::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;rF   c                 x    | j         5  | j                            |          cddd           S # 1 swxY w Y   dS )a  gets implicit user based on resource.
        for example:
            p, alice, data1, read
            p, bob, data2, write
            p, data2_admin, data2, read
            p, data2_admin, data2, write
            g, alice, data2_admin
        get_implicit_users_for_resource("data2") will return [[bob data2 write] [alice data2 read] [alice data2 write]]
        get_implicit_users_for_resource("data1") will return [[alice data1 read]]
        Note: only users will be returned, roles (2nd arg in "g") will be excluded.N)r#   r    get_implicit_users_for_resource)r   resources     r   rG  z.SyncedEnforcer.get_implicit_users_for_resource  s     X 	E 	E7::8DD	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	ErF   c                 z    | j         5  | j                            ||          cddd           S # 1 swxY w Y   dS )ztget implicit user based on resource and domain.
        Compared to GetImplicitUsersForResource, domain is supportedN)r#   r    )get_implicit_users_for_resource_by_domain)r   rH  r   s      r   rJ  z8SyncedEnforcer.get_implicit_users_for_resource_by_domain  s     X 	W 	W7DDXvVV	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	W 	WrO   )NN)T)mr   r   r   __doc__r   r+   r6   r=   r?   rA   rD   rH   rJ   rM   rQ   rT   rW   rZ   r]   r/   r`   rc   re   rg   rl   rn   rp   rr   rv   ry   r|   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r
  r  r  r  r  r  r  r  r  r   r$  r'  r)  r+  r-  r/  r1  r4  strr:  r<  rC  rE  rG  rJ  r   r   r   r   r   &   s       4 4) ) ) )( ( (. . .* * *- - -
' ' '
( ( (
( ( (. . .
) ) )" " "
) ) )
) ) )
& & &
* * *
) ) )
8 8 8
) ) ). . .
+ + +. . .0 0 0. . .
9 9 9
- - -
8 8 8
- - -
8 8 8
+ + +
6 6 6
( ( (
K K K
3 3 3
X X X
1 1 1
T T T
< < <
a a a
/ / /
< < <
/ / /< < <2 2 2
N N N
? ? ?
[ [ [
8 8 8
E E E
8 8 8E E E; ; ;
W W W
H H H
d d d
4 4 44 4 4
4 4 4
9 9 9
9 9 9< < <7 7 7- - -- - -: : :F F FI I I= = =: : :F F FF F F RV q q q q q _c     "J J JF F F
F F F
K K KO O OL L L
Y Y Y
O O O
7 7 7
3 3 3 37 7 7
I I I> > >
X X Xd d d
U U U
" " "
/ / /< < <2 2 2
? ? ?
8 8 8E E E; ; ;
H H H
f f f3# 32B 3 3 3 3; ; ;1 1 1
; ; ;E E EW W W W Wr   r   )r	   r-   casbin.enforcerr   casbin.util.rwlockr   r   r   r   r   r   <module>rO     s         $ $ $ $ $ $ * * * * * *                I
W I
W I
W I
W I
W I
W I
W I
W I
W I
Wr   