
    j8                     N    d dl mZ d dlmZ d dlmZmZmZ  G d de          ZdS )    )InternalEnforcer)PolicyOp)ACTION_INDEXSUBJECT_INDEXOBJECT_INDEXc                   ,   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 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. Z0d/ Z1d0 Z2d1S )2ManagementEnforcerzA
    ManagementEnforcer = InternalEnforcer + Management API.
    c                 ,    |                      d          S )z=gets the list of subjects that show up in the current policy.p)get_all_named_subjectsselfs    d/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/casbin/management_enforcer.pyget_all_subjectsz#ManagementEnforcer.get_all_subjects   s    **3///    c                 z    | j                             |t                    }| j                             d||          S )zCgets the list of subjects that show up in the current named policy.r   )modelget_field_indexr   get_values_for_field_in_policyr   ptypefield_indexs      r   r   z)ManagementEnforcer.get_all_named_subjects   s3    j00FFz88e[QQQr   c                 ,    |                      d          S )z<gets the list of objects that show up in the current policy.r   )get_all_named_objectsr   s    r   get_all_objectsz"ManagementEnforcer.get_all_objects"       ))#...r   c                 z    | j                             |t                    }| j                             d||          S )zBgets the list of objects that show up in the current named policy.r   )r   r   r   r   r   s      r   r   z(ManagementEnforcer.get_all_named_objects&   3    j00EEz88e[QQQr   c                 ,    |                      d          S )z<gets the list of actions that show up in the current policy.r   )get_all_named_actionsr   s    r   get_all_actionsz"ManagementEnforcer.get_all_actions+   r   r   c                 z    | j                             |t                    }| j                             d||          S )zBgets the list of actions that show up in the current named policy.r   )r   r   r   r   r   s      r   r    z(ManagementEnforcer.get_all_named_actions/   r   r   c                 ,    |                      d          S )z@gets the list of roles that show up in the current named policy.g)get_all_named_rolesr   s    r   get_all_rolesz ManagementEnforcer.get_all_roles4   s    '',,,r   c                 :    | j                             d|d          S )/gets all the authorization rules in the policy.r$      )r   r   r   r   s     r   r%   z&ManagementEnforcer.get_all_named_roles8   s    z88eQGGGr   c                 ,    |                      d          S )r(   r   )get_named_policyr   s    r   
get_policyzManagementEnforcer.get_policy<   s    $$S)))r   c                       | j         d|g|R  S )zOgets all the authorization rules in the policy, field filters can be specified.r   )get_filtered_named_policyr   r   field_valuess      r   get_filtered_policyz&ManagementEnforcer.get_filtered_policy@   s    -t-c;NNNNNr   c                 8    | j                             d|          S )z5gets all the authorization rules in the named policy.r   r   r-   r*   s     r   r,   z#ManagementEnforcer.get_named_policyD       z$$S%000r   c                 ,     | j         j        d||g|R  S )zUgets all the authorization rules in the named policy, field filters can be specified.r   r   r2   r   r   r   r1   s       r   r/   z,ManagementEnforcer.get_filtered_named_policyH   #    -tz-c5+UUUUUr   c                 ,    |                      d          S z2gets all the role inheritance rules in the policy.r$   )get_named_grouping_policyr   s    r   get_grouping_policyz&ManagementEnforcer.get_grouping_policyL   s    --c222r   c                       | j         d|g|R  S zRgets all the role inheritance rules in the policy, field filters can be specified.r$   )"get_filtered_named_grouping_policyr0   s      r   get_filtered_grouping_policyz/ManagementEnforcer.get_filtered_grouping_policyP   s    6t6sKW,WWWWr   c                 8    | j                             d|          S r;   r4   r*   s     r   r<   z,ManagementEnforcer.get_named_grouping_policyT   r5   r   c                 ,     | j         j        d||g|R  S r?   r7   r8   s       r   r@   z5ManagementEnforcer.get_filtered_named_grouping_policyX   r9   r   c                      | j         dg|R  S )z0determines whether an authorization rule exists.r   )has_named_policyr   paramss     r   
has_policyzManagementEnforcer.has_policy\   s    $t$S262222r   c                     t          |          dk    r?t          |d         t                    r$|d         }| j                            d||          S | j                            d|t          |                    S )z5determines whether a named authorization rule exists.r)   r   r   len
isinstancelistr   rH   r   r   rG   	str_slices       r   rE   z#ManagementEnforcer.has_named_policy`   si    v;;!
6!9d ; ;q	I:((eY???z$$S%f>>>r   c                      | j         dg|R  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.
        r   )add_named_policyrF   s     r   
add_policyzManagementEnforcer.add_policyh   s     %t$S262222r   c                 .    |                      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.
        r   )add_named_policiesr   ruless     r   add_policieszManagementEnforcer.add_policiesp   s     &&sE222r   c                     t          |          dk    r;t          |d         t                    r |d         }|                     d||          }n$|                     d|t          |                    }|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.
        r)   r   r   )rK   rL   rM   _add_policy)r   r   rG   rO   
rule_addeds        r   rQ   z#ManagementEnforcer.add_named_policyx   sm     v;;!
6!9d ; ;q	I))#ui@@JJ))#ud6llCCJr   c                 0    |                      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.r   )_add_policiesr   r   rV   s      r   rT   z%ManagementEnforcer.add_named_policies   s    
 !!#ue444r   c                 0    |                      d||          S )z6updates an authorization rule from the current policy.r   )update_named_policy)r   old_rulenew_rules      r   update_policyz ManagementEnforcer.update_policy   s    ''Xx@@@r   c                 0    |                      d||          S )z4updates authorization rules from the current policy.r   )update_named_policies)r   	old_rules	new_ruless      r   update_policiesz"ManagementEnforcer.update_policies   s    ))#y)DDDr   c                 2    |                      d|||          S )z<updates an authorization rule from the current named policy.r   )_update_policy)r   r   r`   ra   s       r   r_   z&ManagementEnforcer.update_named_policy   s    ""3xBBBr   c                 2    |                      d|||          S )z:updates authorization rules from the current named policy.r   )_update_policies)r   r   re   rf   s       r   rd   z(ManagementEnforcer.update_named_policies   s    $$S%IFFFr   c                 "     | j         d||g|R  S )z>update_filtered_policies deletes old rules and adds new rules.r   )update_filtered_named_policies)r   rf   r   r1   s       r   update_filtered_policiesz+ManagementEnforcer.update_filtered_policies   s"    2t23	;^Q]^^^^r   c                 $     | j         d|||g|R  S )zDupdate_filtered_named_policies deletes old rules and adds new rules.r   )_update_filtered_policies)r   r   rf   r   r1   s        r   rm   z1ManagementEnforcer.update_filtered_named_policies   s$    -t-c5)[`S_````r   c                      | j         dg|R  S )z6removes an authorization rule from the current policy.r   )remove_named_policyrF   s     r   remove_policyz ManagementEnforcer.remove_policy   s    't'5f5555r   c                 .    |                      d|          S )z4removes authorization rules from the current policy.r   )remove_named_policiesrU   s     r   remove_policiesz"ManagementEnforcer.remove_policies   s    ))#u555r   c                       | j         d|g|R  S )zVremoves an authorization rule from the current policy, field filters can be specified.r   )remove_filtered_named_policyr0   s      r   remove_filtered_policyz)ManagementEnforcer.remove_filtered_policy   s    0t0kQLQQQQr   c                     t          |          dk    r;t          |d         t                    r |d         }|                     d||          }n$|                     d|t          |                    }|S )z<removes an authorization rule from the current named policy.r)   r   r   )rK   rL   rM   _remove_policy)r   r   rG   rO   rule_removeds        r   rr   z&ManagementEnforcer.remove_named_policy   sm     v;;!
6!9d ; ;q	I..sE9EELL..sE4<<HHLr   c                 0    |                      d||          S )z:removes authorization rules from the current named policy.r   )_remove_policiesr]   s      r   ru   z(ManagementEnforcer.remove_named_policies   s    $$S%777r   c                 "     | j         d||g|R  S )z\removes an authorization rule from the current named policy, field filters can be specified.r   )_remove_filtered_policyr8   s       r   rx   z/ManagementEnforcer.remove_filtered_named_policy   s!    +t+CSlSSSSr   c                      | j         dg|R  S )z2determines whether a role inheritance rule exists.r$   )has_named_grouping_policyrF   s     r   has_grouping_policyz&ManagementEnforcer.has_grouping_policy   s     .t-c;F;;;;r   c                     t          |          dk    r?t          |d         t                    r$|d         }| j                            d||          S | j                            d|t          |                    S )z8determines whether a named role inheritance rule exists.r)   r   r$   rJ   rN   s       r   r   z,ManagementEnforcer.has_named_grouping_policy   sk     v;;!
6!9d ; ;q	I:((eY???z$$S%f>>>r   c                      | j         dg|R  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.
        r$   )add_named_grouping_policyrF   s     r   add_grouping_policyz&ManagementEnforcer.add_grouping_policy   s     .t-c;F;;;;r   c                 .    |                      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.
        r$   )add_named_grouping_policiesrU   s     r   add_grouping_policiesz(ManagementEnforcer.add_grouping_policies   s     //U;;;r   c                 H   g }t          |          dk    rPt          |d         t                    r5|d         }|                     d||          }|                    |           nF|                     d|t          |                    }|                    t          |                     | j        ro| j                            | j        |         t          j
        d||           || j        v r3| j                            | j        |         t          j
        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.
        r)   r   r$   )rK   rL   rM   rY   appendauto_build_role_linksr   build_incremental_role_linksrm_mapr   
Policy_addcond_rm_map(build_incremental_conditional_role_links)r   r   rG   rV   rO   rZ   s         r   r   z,ManagementEnforcer.add_named_grouping_policy   s    v;;!
6!9d ; ;q	I))#ui@@JLL####))#ud6llCCJLLf&&&% 	J33DK4FH[]`bginooo(((
CC$U+X-@#ue   r   c                     |                      d||          }| j        r3| j                            | j        |         t
          j        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.r$   )r\   r   r   r   r   r   r   )r   r   rV   rules_addeds       r   r   z.ManagementEnforcer.add_named_grouping_policies   sX    
 ((eU;;% 	pJ33DK4FH[]`bginooor   c                      | j         dg|R  S )z8removes a role inheritance rule from the current policy.r$   )remove_named_grouping_policyrF   s     r   remove_grouping_policyz)ManagementEnforcer.remove_grouping_policy  s    0t0>v>>>>r   c                 .    |                      d|          S )z7removes role inheritance rules from the current policy.r$   )remove_named_grouping_policiesrU   s     r   remove_grouping_policiesz+ManagementEnforcer.remove_grouping_policies
  s    223>>>r   c                       | j         d|g|R  S )zXremoves a role inheritance rule from the current policy, field filters can be specified.r$   )%remove_filtered_named_grouping_policyr0   s      r   remove_filtered_grouping_policyz2ManagementEnforcer.remove_filtered_grouping_policy  s    9t9#{Z\ZZZZr   c                    g }t          |          dk    rPt          |d         t                    r5|d         }|                     d||          }|                    |           nF|                     d|t          |                    }|                    t          |                     | j        r5|r3| j                            | j        |         t          j
        d||           |S )z>removes a role inheritance rule from the current named policy.r)   r   r$   )rK   rL   rM   r{   r   r   r   r   r   r   Policy_remove)r   r   rG   rV   rO   r|   s         r   r   z/ManagementEnforcer.remove_named_grouping_policy  s     v;;!
6!9d ; ;q	I..sE9EELLL####..sE4<<HHLLLf&&&% 	s, 	sJ33DK4FH^`cejlqrrrr   c                     |                      d||          }| j        r5|r3| j                            | j        |         t
          j        d||           |S )z=removes role inheritance rules from the current named policy.r$   )r~   r   r   r   r   r   r   )r   r   rV   rules_removeds       r   r   z1ManagementEnforcer.remove_named_grouping_policies"  s^    --c5%@@% 	s- 	sJ33DK4FH^`cejlqrrrr   c                      | j         d||g|R  }| j        r5|r3| j                            | j        |         t
          j        d||           |S )z^removes a role inheritance rule from the current named policy, field filters can be specified.r$   )'_remove_filtered_policy_returns_effectsr   r   r   r   r   r   )r   r   r   r1   r|   s        r   r   z8ManagementEnforcer.remove_filtered_named_grouping_policy+  sk    CtCCP[k^jkkk% 	, 	J33E"H$:C   r   c                 <    | j                             ||           dS )zadds a customized function.N)fmadd_function)r   namefuncs      r   r   zManagementEnforcer.add_function5  s     T4(((((r   N)3__name__
__module____qualname____doc__r   r   r   r   r!   r    r&   r%   r-   r2   r,   r/   r=   rA   r<   r@   rH   rE   rR   rW   rQ   rT   rb   rg   r_   rd   rn   rm   rs   rv   ry   rr   ru   rx   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   r	   r	      s        0 0 0R R R
/ / /R R R
/ / /R R R
- - -H H H* * *O O O1 1 1V V V3 3 3X X X1 1 1V V V3 3 3? ? ?3 3 33 3 3  5 5 5A A AE E EC C CG G G_ _ _a a a6 6 66 6 6R R R	 	 	8 8 8T T T< < <
? ? ?< < << < <  0	 	 	? ? ?? ? ?[ [ [       ) ) ) ) )r   r	   N)	casbin.internal_enforcerr   casbin.model.policy_opr   casbin.constant.constantsr   r   r   r	   r   r   r   <module>r      s    6 5 5 5 5 5 + + + + + + O O O O O O O O O Oc) c) c) c) c)) c) c) c) c) c)r   