
    j06                     N   d dl mZ d dlZd dlmZ d dlmZmZmZmZ d dlm	Z	m
Z
mZ d dlmZ ej                            d          rd dlmZ  e            Znd d	lmZ  G d
 de          Z G d de          Z G d d          Z G d dej        ej        j                  ZdS )    )contextmanagerN)persist)ColumnIntegerStringBoolean)create_engineor_not_)sessionmakerz1.)declarative_base)DeclarativeBasec                       e Zd ZdS )BaseN)__name__
__module____qualname__     k/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/casbin_sqlalchemy_adapter/adapter.pyr   r      s        r   r   c                   P   e Zd ZdZ eed          Z e ed                    Z e ed                    Z	 e ed                    Z
 e ed                    Z e ed                    Z e ed                    Z e ed                    Zd Zd ZdS )
CasbinRulecasbin_ruleT)primary_key   c                     | j         g}| j        | j        | j        | j        | j        | j        fD ]}| n|                    |           d                    |          S )Nz, )	ptypev0v1v2v3v4v5appendjoin)selfarrvs      r   __str__zCasbinRule.__str__"   s^    zl'47DGTWdgtwG 	 	AyJJqMMMMyy~~r   c                 R    d                     | j        t          |                     S )Nz<CasbinRule {}: "{}">)formatidstrr&   s    r   __repr__zCasbinRule.__repr__*   s     &--dgs4yyAAAr   N)r   r   r   __tablename__r   r   r,   r   r   r   r   r    r!   r"   r#   r)   r/   r   r   r   r   r      s        !M	T	*	*	*BF66#;;E	s		B	s		B	s		B	s		B	s		B	s		B  B B B B Br   r   c                   *    e Zd Zg Zg Zg Zg Zg Zg Zg Z	dS )FilterN)
r   r   r   r   r   r   r    r!   r"   r#   r   r   r   r2   r2   .   s1        E	B	B	B	B	B	BBBr   r2   c            
       h   e Zd ZdZ	 	 	 	 ddZed             Zd Zd Zdd
Z	d Z
d dZd Zd Zd Zd Zd Zd Zdededee         dee         d	df
dZdededeee                  deee                  d	df
dZdeee                  d	eee                  fdZd	eee                  fdZd ZdS )!Adapterz"the interface for Casbin adapters.NFTc                 z   t          |t                    rt          |          | _        n|| _        d | _        |t
          }n|it          |j        t                    sOdt          t                    d}|dt          t          |j                            dz  }t          |          || _        dD ]$}t          ||          st          | d          %|j        t          _        || _        t          | j                  | _        |r$t          j                            | j                   || _        d S )Nz6The type of db_class_softdelete_attribute needs to be z. zAn attribute of type z was given.)r,   r   r   r   r    r!   r"   r#   z# not found in custom DatabaseClass.)bind)
isinstancer-   r	   _enginesoftdelete_attributer   typer   
ValueErrorhasattr	Exceptionmetadatar   	_db_classr   session_local
create_all	_filtered)r&   enginedb_classdb_class_softdelete_attributefilteredcreate_all_modelsmsgattrs           r   __init__zAdapter.__init__;   sP    fc"" 	"(00DLL!DL$(!!HH,8-2GB B8 bsSZ||aaaks48U8Z3[3[/\/\kkkk oo%(ED%	 R R x.. R#t$P$P$PQQQR$-DM!)t|<<< 	3M$$T\222!r   c              #     K   |                                  }	 |V  |                                 n(# t          $ r}|                                 |d}~ww xY w	 |                                 dS # |                                 w xY w)z<Provide a transactional scope around a series of operations.N)r@   commitr=   rollbackclose)r&   sessiones      r   _session_scopezAdapter._session_scopek   s       $$&&	MMMNN 	 	 	G	 
 MMOOOOOGMMOOOOs%   1 A0 
AAAA0 0Bc                 6   |                                  5 }|                    | j                  }|                     |          }|                                }|D ]$}t          j        t          |          |           %	 ddd           dS # 1 swxY w Y   dS )(loads all policy rules from the storage.N)rQ   queryr?   _softdelete_queryallr   load_policy_liner-   )r&   modelrO   rT   lineslines         r   load_policyzAdapter.load_policyx   s      "" 	;gMM$.11E**511EIIKKE ; ;(TE::::;		; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   A+BBBc                     | j         S N)rB   r.   s    r   is_filteredzAdapter.is_filtered   s
    ~r   returnc                 n   |                                  5 }|                    | j                  }|                     |          }|                     ||          }|                                }|D ]$}t          j        t          |          |           %d| _	        ddd           dS # 1 swxY w Y   dS )rS   TN)
rQ   rT   r?   rU   filter_queryrV   r   rW   r-   rB   )r&   rX   filterrO   rT   filtersrZ   s          r   load_filtered_policyzAdapter.load_filtered_policy   s      "" 	"gMM$.11E**511E''v66GkkmmG ; ;(TE::::!DN	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   BB**B.1B.c           	         dD ]l}t          t          ||                    dk    rI|                    t          | j        |                              t          ||                              }m|                    | j        j                  S )N)r   r   r   r    r!   r"   r#   r   )lengetattrrb   r?   in_order_byr,   )r&   querydbrb   rI   s       r   ra   zAdapter.filter_query   s    A 	 	D764(())A--!..DND1155gfd6K6KLL   1222r   c                 Z   |                      |          }t          |          D ])\  }}t          |d                    |          |           *|r|                    |           d S |                                 5 }|                    |           d d d            d S # 1 swxY w Y   d S )N)r   v{})r?   	enumeratesetattrr+   addrQ   )r&   r   rulerO   rZ   ir(   s          r   _save_policy_linezAdapter._save_policy_line   s    ~~E~**dOO 	. 	.DAqD%,,q//1---- 	"KK$$&& "'D!!!" " " " " " " " " " " " " " " " " "s   =B  B$'B$c                 `   | j         |                                 5 }|                    | j                  }|                                 dD ]e}||j                                        vr|j        |                                         D ]'\  }}|j        D ]}| 	                    |||           (f	 ddd           n# 1 swxY w Y   dS |                                 5 }|                    | j                  }| 
                    |          }|                                }dD ]}||j                                        vr|j        |                                         D ]\  }}|j        D ]}|                    | j        j        |k              }	t          |          D ]F\  }
}t          | j        d                    |
                    }|	                    ||k              }	G|	                                dk    r| 	                    |||           |D ]u}|j        }|d         }|j        |j        |j        |j        |j        |j        g}d |D             }|                    |||          st3          || j         j        d           v	 ddd           n# 1 swxY w Y   dS )z&saves all policy rules to the storage.N)pg)rO   Trl   r   c                     g | ]}||S r]   r   ).0elements     r   
<listcomp>z'Adapter.save_policy.<locals>.<listcomp>   s    WWWG7CVCVCVCVr   )r9   rQ   rT   r?   deleterX   keysitemspolicyrr   rU   rV   rb   r   rm   rg   r+   countr   r   r    r!   r"   r#   
has_policyrn   name)r&   rX   rO   rT   secr   astrp   lines_before_changesra   indexvaluev_valuerZ   fields_with_Nones                  r   save_policyzAdapter.save_policy   s    $,$$&& Q'dn55% Q QC%+"2"2"4"444 &+k#&6&<&<&>&> Q Q
s$'J Q QD 225$2PPPPQQQQ Q Q Q Q Q Q Q Q Q Q Q Q Q Q 4   "" $	HgMM$.11E**511E $)99;;  " Q Qek..0000"'+c"2"8"8":": 	Q 	QJE3 #
 Q Q',||DN4HE4Q'R'R,5dOO Q QLE5&-dnell5>Q>Q&R&RG+7+>+>w%?O+P+PLL'--//144 225$2PPPQ	Q - H H
AhGGGGGG$  XW/?WWW''UD99 HD$";"@$GGGH-$	H $	H $	H $	H $	H $	H $	H $	H $	H $	H $	H $	H $	H $	H $	HL ts%   BC  CC!F5J##J'*J'c                 2    |                      ||           dS )z"adds a policy rule to the storage.Nrr   )r&   r   r   rp   s       r   
add_policyzAdapter.add_policy   s    ud+++++r   c                 <    |D ]}|                      ||           dS )z#adds a policy rules to the storage.Nr   )r&   r   r   rulesrp   s        r   add_policieszAdapter.add_policies   s4     	0 	0D""5$////	0 	0r   c           
      *   |                                  5 }|                    | j                  }|                     |          }|                    | j        j        |k              }t          |          D ]D\  }}|                    t          | j        d                    |                    |k              }E| j	        |
                                }n|                    | j	        di          }ddd           n# 1 swxY w Y   |dk    rdndS )z'removes a policy rule from the storage.rl   NTr   F)rQ   rT   r?   rU   rb   r   rm   rg   r+   r9   rz   update)	r&   r   r   rp   rO   rT   rq   r(   rs	            r   remove_policyzAdapter.remove_policy   sF     "" 
	DgMM$.11E**511ELL!5!>??E!$ T T1WT^U\\!__%M%MQR%RSS(0LLNNLL$";T!BCC
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 
	D 1uutt%'s   CD  DDc                     |sdS                                   5 }|                     j                  }                     |          }|                     j        j        |k              }t          | }t          |          D ]4\  }|                    t           fd|D                                 }5 j	        |
                                 n|                     j	        di           ddd           dS # 1 swxY w Y   dS )z%remove policy rules from the storage.Nc              3   p   K   | ]0}t          j        d                                         |k    V  1dS )rl   N)rg   r?   r+   )rw   r(   rq   r&   s     r   	<genexpr>z*Adapter.remove_policies.<locals>.<genexpr>   s>      TT!Q@@AETTTTTTr   T)rQ   rT   r?   rU   rb   r   ziprm   r
   r9   rz   r   )r&   r   r   r   rO   rT   rp   rq   s   `      @r   remove_policieszAdapter.remove_policies   sh    	F  "" 	@gMM$.11E**511ELL!5!>??EKE$U++  4TTTTTtTTTTT  (0d7>???	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s   CD  DDc           	         |                                  5 }|                    | j                  }|                     |          }|                    | j        j        |k              }d|cxk    rdk    sn 	 ddd           dS d|t          |          z   cxk    rdk    sn 	 ddd           dS t          |          D ]O\  }}|dk    rDt          | j        d	                    ||z                       }	|                    |	|k              }P| j
        |                                }
n|                    | j
        d	i          }
ddd           n# 1 swxY w Y   |
dk    rd	ndS )
ztremoves policy rules that match the filter from the storage.
        This is part of the Auto-Save feature.
        r      NF       rl   T)rQ   rT   r?   rU   rb   r   rf   rm   rg   r+   r9   rz   r   )r&   r   r   field_indexfield_valuesrO   rT   rq   r(   r   r   s              r   remove_filtered_policyzAdapter.remove_filtered_policy  s      "" 	DgMM$.11E**511ELL!5!>??E))))))))	D 	D 	D 	D 	D 	D 	D 	D s<'8'88====A====	D 	D 	D 	D 	D 	D 	D 	D ",// 7 7177%dnell;QR?6S6STTG!LLA66E(0LLNNLL$";T!BCC#	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D 	D& 1uutt%'s   A#E E2BEEEr   r   old_rulenew_rulec           	         |                                  5 }|                    | j                  }|                     |          }|                    | j        j        |k              }t          |          D ]F\  }}t          | j        d                    |                    }	|                    |	|k              }Gt          |          t          |          k    r|n|}
|
                                }t          t          |
                    D ]?}|t          |          k     rt          d| d| d           ,t          d| d           @	 ddd           dS # 1 swxY w Y   dS )a+  
        Update the old_rule with the new_rule in the database (storage).

        :param sec: section type
        :param ptype: policy type
        :param old_rule: the old rule that needs to be modified
        :param new_rule: the new rule to replace the old rule

        :return: None
        rl   zold_rule_line.vz = new_rule[]z = NoneN)rQ   rT   r?   rU   rb   r   rm   rg   r+   rf   onerangeexec)r&   r   r   r   r   rO   rT   r   r   r   longest_ruleold_rule_lines               r   update_policyzAdapter.update_policy!  s      "" 	;gMM$.11E**511ELL!5!>??E !*( 3 3 7 7u!$.%,,u2E2EFFW%566 (+8}}s8}}'D'D88(L!IIKKM s<0011 ; ;3x==((F5FFeFFFGGGG95999::::	;	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	;s   D=E  E$'E$	old_rules	new_rulesc                     t          t          |                    D ]&}|                     ||||         ||                    'dS )a1  
        Update the old_rules with the new_rules in the database (storage).

        :param sec: section type
        :param ptype: policy type
        :param old_rules: the old rules that need to be modified
        :param new_rules: the new rules to replace the old rules

        :return: None
        N)r   rf   r   )r&   r   r   r   r   rq   s         r   update_policieszAdapter.update_policiesD  sT    " s9~~&& 	G 	GAsE9Q<1FFFF	G 	Gr   c                    t                      }||_        t          t          |                    D ]<}||k    r4||t          |          z   k     rt	          |d| |||z
                      < |                     ||           dS )zMupdate_filtered_policies updates all the policies on the basis of the filter.r(   N)r2   r   r   rf   rn   _update_filtered_policies)r&   r   r   r   r   r   rb   rq   s           r   update_filtered_policiesz Adapter.update_filtered_policiesX  s    
  s<(()) 	 	AaAc,6G6G(G$G$GAa+o)FGGGG&&y&99999r   c                    |                                  5 }|                    | j                  }|                     |          }|                    | j        j        |j        k              }|                     ||          }|                                }|                     d|j        |           | 	                    d|j        |           |cddd           S # 1 swxY w Y   dS )zN_update_filtered_policies updates all the policies on the basis of the filter.rt   N)
rQ   rT   r?   rU   rb   r   ra   rV   r   r   )r&   r   rb   rO   rT   filtered_queryr   s          r   r   z!Adapter._update_filtered_policiesi  s      "" 	g MM$.11E**511ELL!5!EFFE!..uf==N&**,,I   flI>>> c6<;;; '	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   B;CC!$C!c                 f    |}| j         '|                    t          | j                             }|S r]   )r9   wherer   )r&   rT   query_softdeletes      r   rU   zAdapter._softdelete_query  s6     $0/55d4;T6U6UVVr   )NNFT)r_   Nr]   )r   r   r   __doc__rJ   r   rQ   r[   r^   rd   ra   rr   r   r   r   r   r   r   r-   listr   r   r   r   rU   r   r   r   r4   r4   8   s       ,,
 &*." ." ." ."` 
 
 ^
; ; ;  
" 
" 
" 
"3 3 3" " " "8 8 8t, , ,0 0 0
( ( ( @ @ @&( ( (2!;!;"!;.23i!;CG9!;	!; !; !; !;FGG G S	?	G
 S	?G 
G G G G(:%)$s)_:	d3i: : : :"d49o    0         r   r4   )
contextlibr   
sqlalchemycasbinr   r   r   r   r   r	   r
   r   sqlalchemy.ormr   __version__
startswithr   r   r   r   r2   r4   adaptersUpdateAdapterr   r   r   <module>r      s   % % % % % %           7 7 7 7 7 7 7 7 7 7 7 7 / / / / / / / / / / ' ' ' ' ' ' $$T** 	//////DD /.....       B B B B B B B B0       M  M  M  M  M gow/= M  M  M  M  M r   