
    j%                     ,    d dl Z dZ G d d          ZdS )    N,c                       e 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S )Policyc                 F    t          j        d          | _        i | _        d S )Nzcasbin.policy)logging	getLoggerloggermodelselfs    ]/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/casbin/model/policy.py__init__zPolicy.__init__   s    '88


    c                 6    | j                             |          S N)r
   get)r   items     r   __getitem__zPolicy.__getitem__   s    z~~d###r   c                     || j         |<   d S r   )r
   )r   keyvalues      r   __setitem__zPolicy.__setitem__   s    
3r   c                 4    | j                                         S r   )r
   keysr   s    r   r   zPolicy.keys   s    z   r   c                 4    | j                                         S r   )r
   valuesr   s    r   r   zPolicy.values"   s    z  """r   c                 4    | j                                         S r   )r
   itemsr   s    r   r   zPolicy.items%   s    z!!!r   c                     d|                                  vrdS | d                                         D ]1\  }}|                    |          }|r|                    |           2dS )zinitializes the roles in RBAC.gN)r   r   r   build_role_links)r   rm_mapptypeastrms        r   r!   zPolicy.build_role_links(   su     diikk!!Fs)//++ 	) 	)JE3E""B )$$R(((	) 	)r   c                 v    |dk    r2| |                              |                              |||           d S d S Nr    )r   build_incremental_role_links)r   r%   opsecr#   ruless         r   r(   z#Policy.build_incremental_role_links3   s@    #::IMM%  ==b"eLLLLL :r   c                 r    |dk    r0| |                              |                              |||          S d S r'   )r   (build_incremental_conditional_role_links)r   cond_rmr)   r*   r#   r+   s         r   r-   z/Policy.build_incremental_conditional_role_links7   s;    #::9==''PPQXZ\^cdddtr   c                     d|                                  vrd S |                                  | d                                         D ]1\  }}|                    |          }|r|                    |           2d S r'   )r   print_policyr   r   build_conditional_role_links)r   cond_rm_mapr#   r$   r.   s        r   r1   z#Policy.build_conditional_role_links<   s    diikk!!Fs)//++ 	: 	:JE3!ooe,,G :00999	: 	:r   c           	      $   | j                             d           dD ]r}||                                 vr| |                                         D ]>\  }}| j                             d                    ||j        |j                             ?sdS )zLog using infozPolicy:pr    z{} : {} : {}N)r	   infor   r   formatr   policy)r   r*   r   r$   s       r   r0   zPolicy.print_policyE   s     	### 	T 	TC$))++%% IOO-- T TS  !6!6sCIsz!R!RSSSST		T 	Tr   c                     dD ]I}||                                  vr| |                                          D ]}g | |         |         _        JdS )zclears all current policy.r4   Nr   r8   )r   r*   r   s      r   clear_policyzPolicy.clear_policyP   sh      	+ 	+C$))++%%Cy~~'' + +(*S	#%%+		+ 	+r   c                 (    | |         |         j         S )zgets all rules in a policy.r8   )r   r*   r#   s      r   
get_policyzPolicy.get_policyZ   s     Cy&&r   c                 F    fd| |         |         j         D             S )z0gets rules based on field filters from a policy.c                 h    g | ]-t          fd t                    D                       +.S )c              3      K   | ]?\  }}t          |          r ||z                      p|d k    p|z            |k    V  @dS  N)callable.0ir   field_indexrules      r   	<genexpr>z8Policy.get_filtered_policy.<locals>.<listcomp>.<genexpr>d   s{        Au %AUU4a+@%A%AuuPR{GtVZ[fij[jVkotVt     r   )all	enumerate)rF   rI   rH   field_valuess    @r   
<listcomp>z.Policy.get_filtered_policy.<locals>.<listcomp>a   sm     
 
 
      ), 7 7    

 
 
r   r=   )r   r*   r#   rH   rM   s      ``r   get_filtered_policyzPolicy.get_filtered_policy_   s@    
 
 
 
 
S	%(/
 
 
 	
r   c                 t    ||                                  vrdS || |         vrdS || |         |         j        v S )z9determines whether a model has the specified policy rule.Fr:   r   r*   r#   rI   s       r   
has_policyzPolicy.has_policyj   sE    diikk!!5S	!!5tCy'...r   c                 N   | |         |         }|                      |||          s|j                            |           ndS |dk    r$|j        dk    r	 t	          ||j                           }t          |j                  dz
  }t          |dd          D ]}	 t	          |j        |dz
           |j                           }n&# t          $ r}t          |           Y d}~nd}~ww xY w||k    r3|j        |         }	|j        |dz
           |j        |<   |	|j        |dz
  <    ||j	        t                              |          <   n&# t          $ r}t          |           Y d}~nd}~ww xY wt          |j                  dz
  |j	        t                              |          <   dS )z adds a policy rule to the model.Fr5   r      NT)rR   r8   appendpriority_indexintlenrange	Exceptionprint
policy_mapDEFAULT_SEPjoin)
r   r*   r#   rI   	assertion
idx_insertrG   idxetmps
             r   
add_policyzPolicy.add_policys   s   Ie$	sE400 	##D))))5#::)2a77 i&>!?@@
	())A-q!R  A!!)"21q5"9):R"STT$ ! ! !a! Z'''.q1.7.>q1u.E	(+25	(Q//?@	$[%5%5d%;%;<<   a 8;9;K7L7Lq7P	[--d334tsD   AE (CE 
C(C#E #C((AE 
E+E&&E+c                     |D ]}|                      |||          r dS |D ](}| |         |         j                            |           )dS )zadds policy rules to the model.FT)rR   r8   rV   r   r*   r#   r+   rI   s        r   add_policieszPolicy.add_policies   sp      	 	DsE400 uu  	1 	1DIe#**40000tr   c                 h   ||                                  vrdS || |         vrdS | |         |         }||j        v r|j                            |          }ndS d|j        v rF|j                            d          }||         ||         k    r||j        |<   nt	          d          ||j        |<   dS )z$update a policy rule from the model.F
p_priority5New rule should have the same priority with old rule.T)r   r8   indextokensr[   )r   r*   r#   old_rulenew_ruler$   
rule_indexrW   s           r   update_policyzPolicy.update_policy   s     diikk!!5S	!!53isz!!))(33JJ53:%% Z--l;;N'8N+CCC)1
:&& WXXX%-CJz"tr   c                 @   ||                                  vrdS || |         vrdS t          |          t          |          k    rdS | |         |         }g }|D ];}||j        v r.|                    |j                            |                     9 dS d|j        v r^|j                            d          }t          |||          D ]1\  }	}}
||         |
|         k    r|
|j        |	<   #t          d          n"t          |||          D ]\  }	}}
|
|j        |	<   dS )z#update policy rules from the model.Frj   rk   T)r   rY   r8   rV   rl   rm   zipr[   )r   r*   r#   	old_rules	new_rulesr$   old_rules_indexrn   rW   rb   ro   s              r   update_policieszPolicy.update_policies   s\    diikk!!5S	!!5y>>S^^++53i! 	 	H3:%%&&sz'7'7'A'ABBBBuu3:%% Z--l;;N+.	9+U+U ] ]'XxN+x/GGG&.CJsOO#$[\\\	] ,/	9+U+U + +'Xx"*
3tr   c                     |                      |||          sdS | |         |         j                            |           || |         |         j        vS )z%removes a policy rule from the model.FrR   r8   removerQ   s       r   remove_policyzPolicy.remove_policy   sV    sE400 	5S	%&&t,,,49U+222r   c                     |D ]Z}|                      |||          s dS | |         |         j                            |           || |         |         j        v r dS [dS )z3RemovePolicies removes policy rules from the model.FTry   rg   s        r   remove_policieszPolicy.remove_policies   s|      	 	D??3t44 uuIe#**4000tCy'...uu / tr   c                     g }|D ]E}|                      |||          r,|                    |           |                     |||           F|S r   )rR   rV   r{   )r   r*   r#   r+   effectedrI   s         r   remove_policies_with_effectedz$Policy.remove_policies_with_effected   s^     	5 	5DsE400 5%%%""3t444r   c                 ~   g }g }t          |          dk    rg S ||                                 vrg S || |         vrg S | |         |         j        D ]Vt          fdt	          |          D                       r|                               A|                               W|| |         |         _        |S )zt
        remove_filtered_policy_returns_effects removes policy rules based on field filters from the model.
        r   c              3   H   K   | ]\  }}|d k    p|z            |k    V  dS rB    rE   s      r   rJ   z@Policy.remove_filtered_policy_returns_effects.<locals>.<genexpr>  >      iiXQ5B;@${Q"75"@iiiiiir   )rY   r   r8   rK   rL   rV   )r   r*   r#   rH   rM   rd   effectsrI   s      `   @r   &remove_filtered_policy_returns_effectsz-Policy.remove_filtered_policy_returns_effects   s     |!!Idiikk!!IS	!!IIe$+ 	! 	!DiiiiiQZ[gQhQhiiiii !t$$$$

4    "%S	%r   c                 .   g }d}||                                  vr|S || |         vr|S | |         |         j        D ]Ct          fdt          |          D                       rd}.|                               D|| |         |         _        |S )z;removes policy rules based on field filters from the model.Fc              3   H   K   | ]\  }}|d k    p|z            |k    V  dS rB   r   rE   s      r   rJ   z0Policy.remove_filtered_policy.<locals>.<genexpr>  r   r   T)r   r8   rK   rL   rV   )r   r*   r#   rH   rM   rd   resrI   s      `   @r   remove_filtered_policyzPolicy.remove_filtered_policy  s    diikk!!JS	!!JIe$+ 	! 	!DiiiiiQZ[gQhQhiiiii !

4    "%S	%
r   c                     g }||                                  vr|S || |         vr|S | |         |         j        D ]#}||         }||vr|                    |           $|S )zUgets all values for a field for all rules in a policy, duplicated values are removed.)r   r8   rV   )r   r*   r#   rH   r   rI   r   s          r   get_values_for_field_in_policyz%Policy.get_values_for_field_in_policy%  s{    diikk!!MS	!!MIe$+ 	% 	%D%EF""e$$$r   N)__name__
__module____qualname__r   r   r   r   r   r   r!   r(   r-   r1   r0   r;   r>   rO   rR   re   rh   rq   rw   r{   r}   r   r   r   r   r   r   r   r   r      s         $ $ $     ! ! !# # #" " "	) 	) 	)M M M  
: : :	T 	T 	T+ + +' ' '
	
 	
 	
/ / /     D
 
 
  4  @3 3 3
 
 
    0  (    r   r   )r   r^   r   r   r   r   <module>r      sQ    ^ ^ ^ ^ ^ ^ ^ ^ ^ ^r   