o
    j                     @   s  d Z ddlZzddlmZ W n ey   ddlmZ Y nw ddlmZ ddlm	Z	 ddl
mZmZmZmZ dd	lmZ dd
lmZ ddlmZ ddlmZmZmZ ddlmZmZmZ ddlmZmZ ddl m!Z!m"Z"m#Z# ddl$m%Z% ddl&m'Z' ddl(m)Z) ddl
m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5 ddl6m7Z7 ddl8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z> G dd de?Z@G dd de?ZAG dd deAZBG dd deAZCdS )
    N)OrderedDict   )linesep)deepcopy)STRING_TYPESSEQUENCE_TYPES
MODIFY_ADDMODIFY_REPLACE   )WritableAttribute)	ObjectDef)AttrDef)LDAPKeyErrorLDAPCursorErrorLDAPCursorAttributeError)check_json_dictformat_jsonprepare_for_stream)operation_to_ldifadd_ldif_header)safe_dnsafe_rdnto_dn)to_stdout_encoding)CaseInsensitiveWithAliasDict)get_config_parameter)STATUS_VIRTUALSTATUS_WRITABLESTATUS_PENDING_CHANGESSTATUS_COMMITTEDSTATUS_DELETEDSTATUS_INITSTATUS_READY_FOR_DELETIONSTATUS_READY_FOR_MOVINGSTATUS_READY_FOR_RENAMINGSTATUS_MANDATORY_MISSINGSTATUSESINITIAL_STATUSES)RESULT_SUCCESS)loglog_enabledERRORBASICPROTOCOLEXTENDEDc                   @   sD   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	dd Z
dS )
EntryStatezTContains data on the status of the entry. Does not pollute the Entry __dict__.

    c                 C   sd   || _ d | _d | _t| _t | _t | _d | _|| _	d | _
d | _t | _|jr-|j| _d S d | _d S N)dn_initial_status_tor"   statusr   
attributesraw_attributesresponsecursororigin	read_timer   changes
definitionselfr2   r9    r@   /root/aizidognhua/tmp/workspace/projects/ec89d86c-575f-41c9-af57-ac45cbdbf775/venv/lib/python3.10/site-packages/ldap3/abstract/entry.py__init__:   s   
zEntryState.__init__c                 C   s  | j r| jd urdt| j d | j| jkr| jd nd | j d | jr*| j nd t }|ddt	| j
  t 7 }|d| jjrNdt	| jjnd	 t 7 }|d
dt	| jj  t 7 }|d| jrldnd	 t 7 }|d| jr|| jjjnd	 t 7 }|S t| S )NDN:  - STATUS: ,   - READ TIME: <never>zattributes: zobject def: z<None>zattr defs: z
response: presentzcursor: )__dict__r2   r   r3   r5   r;   	isoformatr   joinsortedr6   keysr=   _object_class_attributesr8   r9   	__class____name__object__repr__)r?   rr@   r@   rA   rT   K   s   N *" 
zEntryState.__repr__c                 C      |   S r1   rT   r?   r@   r@   rA   __str__W      zEntryState.__str__c                 C   s   t | j}d |d< |S )Nr9   )dictrJ   )r?   cpyr@   r@   rA   __getstate__Z   s   
zEntryState.__getstate__c                 C   s   dd t dD }|tvr"dt| }ttrttd||  t||tv r)|| _|| _	|t
kr3t| _|tkr:t| _| j	tksI| j	tkrs| jtkru| jjD ])}| jj| jrr| |vrr|| jvsg| j| jrr|| jvrrt| _	 d S qMd S d S d S )Nc                 S      g | ]}|  qS r@   lower.0vr@   r@   rA   
<listcomp>`       z)EntryState.set_status.<locals>.<listcomp>*IGNORED_MANDATORY_ATTRIBUTES_IN_OBJECT_DEFzinvalid entry status %s for <%s>)r   r'   strr+   r,   r*   r   r(   r3   r5   r!   r   r    r   r   r=   rP   	mandatoryr`   r6   virtualr<   r&   )r?   r5   /conf_ignored_mandatory_attributes_in_object_deferror_messageattrr@   r@   rA   
set_status_   s,    zEntryState.set_statusc                 C      | j S r1   )r7   rX   r@   r@   rA   entry_raw_attributest      zEntryState.entry_raw_attributesN)rR   
__module____qualname____doc__rB   rT   rY   r]   rn   propertyrp   r@   r@   r@   rA   r0   5   s    r0   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zedd Zedd Zedd Zedd Zedd Zd d! Zed"d# Zed$d% Zed&d' Zed(d) Zed*d+ Zd4d0d1Zd5d2d3Zd/S )6	EntryBasea  The Entry object contains a single LDAP entry.
    Attributes can be accessed either by sequence, by assignment
    or as dictionary keys. Keys are not case sensitive.

    The Entry object is read only

    - The DN is retrieved by entry_dn
    - The cursor reference is in _cursor
    - Raw attributes values are retrieved with _raw_attributes and the _raw_attribute() methods
    c                 C   s   t ||| _d S r1   )r0   _stater>   r@   r@   rA   rB      s   zEntryBase.__init__c                 C   s   | j rd| jd urddt| j d | jj| jkr| jjd nd | j d | jr,| j nd t }| jj	rbt
| jj	D ]&}| jj	| sSt| jj	| dra| jj	| jra|dt| jj	|  t 7 }q;|S t| S )	NrC   rD   rE   rF   rG   rH   r<   z    )rJ   entry_dnr   rw   r3   entry_statusentry_read_timerK   r   r6   rM   hasattrr<   reprrS   rT   )r?   rU   rm   r@   r@   rA   rT      s   R,
zEntryBase.__repr__c                 C   rV   r1   rW   rX   r@   r@   rA   rY      rZ   zEntryBase.__str__c                 c   s"    | j jD ]	}| j j| V  qd S r1   )rw   r6   )r?   	attributer@   r@   rA   __iter__   s   zEntryBase.__iter__c                 C   s&   z|  | W dS  ty   Y dS w )NTF)__getitem__r   )r?   itemr@   r@   rA   __contains__   s   
zEntryBase.__contains__c                 C   s  t |tr|dkrt| |S d|  }d }| jj	 D ]}|| kr,|} nq |sB| jj
 D ]}|| krA|} nq5|sY| jj	 D ]}|d | krX|} nqJ|sp| jj
 D ]}|d | kro|} nqa|s| jj	 D ]}|d | v r|} nqx|s| jj
 D ]}|d | v r|} nq|sd| }ttrttd||  t|| jj| S d}ttrttd||  t|)Nrw   rF   ;binaryz;rangezattribute '%s' not foundrg   attribute name must be a string)
isinstancer   rS   __getattr__rL   splitr`   rw   r6   rN   aliasesr+   r,   r*   r   r?   r   
attr_foundrm   rl   r@   r@   rA   r      sd   
zEntryBase.__getattr__c                 C   sr   |dkrt | || d S || jjv r&d| }ttr"ttd||  t|d| }ttr5ttd||  t|)Nrw   zattribute '%s' is read onlyrg   z#entry is read only, cannot add '%s')rS   __setattr__rw   r6   r+   r,   r*   r   )r?   r   valuerl   r@   r@   rA   r      s   zEntryBase.__setattr__c                 C   s$  t |trd|  }d }| jj D ]}|| kr"|} nq|s8| jj D ]}|| kr7|} nq+|sO| jj D ]}|d | krN|} nq@|sf| jj D ]}|d | kre|} nqW|s{d| }t	t
rwtt
d||  t|| jj| S d}t	t
rtt
d||  t|)NrF   r   zkey '%s' not foundrg   zkey must be a string)r   r   rL   r   r`   rw   r6   rN   r   r+   r,   r*   r   r   r@   r@   rA   r      sH   
zEntryBase.__getitem__c                 C   s   t |tr| j|jkS dS NFr   rv   rx   r?   otherr@   r@   rA   __eq__     
zEntryBase.__eq__c                 C   s   t |tr| j|jkS dS r   r   r   r@   r@   rA   __lt__  r   zEntryBase.__lt__c                 C      | j jS r1   )rw   r2   rX   r@   r@   rA   rx        zEntryBase.entry_dnc                 C   r   r1   )rw   r9   rX   r@   r@   rA   entry_cursor  r   zEntryBase.entry_cursorc                 C   r   r1   )rw   r5   rX   r@   r@   rA   ry     r   zEntryBase.entry_statusc                 C   r   r1   )rw   r=   rX   r@   r@   rA   entry_definition  r   zEntryBase.entry_definitionc                 C   r   r1   rw   r7   rX   r@   r@   rA   rp     r   zEntryBase.entry_raw_attributesc                 C   s   || j jv r| j j| S dS )z

        :param name: name of the attribute
        :return: raw (unencoded) value of the attribute, None if attribute is not found
        Nr   )r?   namer@   r@   rA   entry_raw_attribute"  s   zEntryBase.entry_raw_attributec                    s    fdd j jD S )Nc                    s   g | ]} j j| jr|qS r@   )r   rP   ri   )rb   r}   rX   r@   rA   rd   ,  s    z8EntryBase.entry_mandatory_attributes.<locals>.<listcomp>)r   rP   rX   r@   rX   rA   entry_mandatory_attributes*  s   z$EntryBase.entry_mandatory_attributesc                 C   s   t | jj S r1   )listrw   r6   rN   rX   r@   r@   rA   entry_attributes.  s   zEntryBase.entry_attributesc                 C   s   t dd | jj D S )Nc                 s   s"    | ]\}}|t |jfV  qd S r1   )r   values)rb   attribute_keyattribute_valuer@   r@   rA   	<genexpr>4  s     z5EntryBase.entry_attributes_as_dict.<locals>.<genexpr>)r[   rw   r6   itemsrX   r@   r@   rA   entry_attributes_as_dict2  s   z"EntryBase.entry_attributes_as_dictc                 C   r   r1   )rw   r;   rX   r@   r@   rA   rz   6  r   zEntryBase.entry_read_timec                 C   r   r1   )rw   r<   rX   r@   r@   rA   _changes:  r   zEntryBase._changesF   TNc           	   	      s   t  } j|d< |r |st  fdd jD |d< n j|d< |r:|s3t  fdd jD |d< nt  j|d< ttu rBt| tj|d||dt	dd	}|rU|
| |S )
Nr2   c                 3   (    | ]} j | r| j | fV  qd S r1   )r   rb   keyrX   r@   rA   r   D     & z*EntryBase.entry_to_json.<locals>.<genexpr>r6   c                 3   r   r1   )rp   r   rX   r@   rA   r   J  r   rawT),z: )ensure_ascii	sort_keysindentcheck_circulardefault
separators)r[   rx   r   rp   rh   bytesr   jsondumpsr   write)	r?   r   r   sortstreamchecked_attributesinclude_empty
json_entryjson_outputr@   rX   rA   entry_to_json>  s.   


zEntryBase.entry_to_jsonc                 C   s~   t d| jjg||d}t|}|pt}||}|r=| dkr2tdgd }|t|| |  |t|| |  |S )NsearchResponse)
sort_orderr   -)	r   rw   r8   r   r   rL   tellr   r   )r?   
all_base64line_separatorr   r   
ldif_linesldif_outputheaderr@   r@   rA   entry_to_ldif^  s   
zEntryBase.entry_to_ldif)Fr   TNTT)FNNN)rR   rr   rs   rt   rB   rT   rY   r~   r   r   r   r   r   r   ru   rx   r   ry   r   rp   r   r   r   r   rz   r   r   r   r@   r@   r@   rA   rv   y   sF    .#










 rv   c                   @   s   e Zd ZdZdddZdS )Entrya  The Entry object contains a single LDAP entry.
    Attributes can be accessed either by sequence, by assignment
    or as dictionary keys. Keys are not case sensitive.

    The Entry object is read only

    - The DN is retrieved by entry_dn
    - The Reader reference is in _cursor()
    - Raw attributes values are retrieved by the _ra_attributes and
      _raw_attribute() methods

    Nc                 C   s6  t d}| jjsd}ttrttd||  t||d u r<| jjjr4| j	j}| j	j
t|tr0|ng  }nd| v r<| jj}|sOd}ttrKttd||  t|t|ts]t|| jj||}|rt|trg|g}t|tr|D ]}||jvrd||f }ttrttd||  t|qnng }|sddlm}	 |	| jj|}
n|}
|r|
| jt|| j }n|
| jj}|
j| | j|j_| |j_ g }g }g }g }|jj j	jD ]}|jj j	j| }||j!kr_||jj"vs||j! |jj"vr_|j	j|j! }t#|j!||j$|j%|j&|j'|j(|j)|j*|j+|j,d	}|j	}|| || t-|||
}|j!|jj"v rR|jj"|j! j,|_,|jj"|j! j.|_.|jj"|j! j|_|jj"|j! j|_|||f ||j! q|D ]\}}||jj"|< qb|D ]}|jj"|= qp|D ]
}|j	}||8 }q{|D ]
}|j	}||7 }q|j/t0 |S )
N(ABSTRACTION_OPERATIONAL_ATTRIBUTE_PREFIXz2schema must be available to make an entry writablerg   objectclassz8object class must be specified to make an entry writablez%attribute '%s' not in schema for '%s'r   )Writer)
r   validate	pre_query
post_queryr   dereference_dndescriptionri   single_valuealias)1r   r   schemar+   r,   r*   r   r=   rO   r   _auxiliary_classr   r   r   r   r   r   rP   r9   r   
connection_refresh_objectrx   r   r   _create_entryrw   r8   entriesappendrz   r;   r:   r   r6   r   r   r   r   r   r   r   ri   r   other_namesr   
raw_valuesrn   r   )r?   
object_defwriter_cursorr6   custom_validatorauxiliary_class!conf_operational_attribute_prefixrl   r}   r   writable_cursorwritable_entryattr_to_addattr_to_removeobject_def_to_addobject_def_to_removerm   original_attrold_attr_defnew_attr_defodnew_attror@   r@   rA   entry_writablex  s   





,

zEntry.entry_writable)NNNNN)rR   rr   rs   rt   r   r@   r@   r@   rA   r   k  s    r   c                   @   sp   e Zd Zdd Zdd Zdd Zedd ZdddZdd Z	dd Z
dddZdd Zdd Zedd Zd
S )WritableEntryc                 C   s   |t ur| || d S d S r1   )Ellipsisr   )r?   r   r   r@   r@   rA   __setitem__  s   zWritableEntry.__setitem__c                 C   s   dd t dD }|dkrt|tr|| jd< d S |turc|| jjjv s*| |v rP|| j	j
vrEt| jjj| | | jd}|| j	j
t|< | j	j
| | d S d| }ttr_ttd||  t|d S )Nc                 S   r^   r@   r_   ra   r@   r@   rA   rd     re   z-WritableEntry.__setattr__.<locals>.<listcomp>#ATTRIBUTES_EXCLUDED_FROM_OBJECT_DEFrw   )r9   attribute '%s' not definedrg   )r   r   r0   rJ   r   r   r=   rP   r`   rw   r6   r   rh   setr+   r,   r*   r   )r?   r   r   (conf_attributes_excluded_from_object_defnew_attributerl   r@   r@   rA   r     s   
zWritableEntry.__setattr__c                 C   s  t |trz|dkr| jd S d|  }| jj D ]}|| kr-| jj|   S q| jj	 D ]}|| krD| jj|   S q4|| j
jv rgt| j
j| | | j| jj|< | jj| | jj| S d| }ttrvttd||  t|d}ttrttd||  t|)Nrw   rF   r   rg   r   )r   r   rJ   rL   r   r`   rw   r6   rN   r   r   rP   r   r   addr+   r,   r*   r   )r?   r   rm   rl   r@   r@   rA   r     s0   

zWritableEntry.__getattr__c                    s    fdd j D S )Nc                    s   g | ]	} | j r|qS r@   )rj   )rb   rm   rX   r@   rA   rd     s    z:WritableEntry.entry_virtual_attributes.<locals>.<listcomp>)r   rX   r@   rX   rA   entry_virtual_attributes  s   z&WritableEntry.entry_virtual_attributesTNc                 C   s  |r| j   | jtkr| j j| j|}| j jjjs(| j jj	|dd\}}}n| j jjj
r5|\}}}}n| j jj}| j jj}| j jj}| j ||| |d tkr| j}| jjr| j jj| jjj jjkr| jjj }	| jjj  t||	| jjjd< | jjjt | j }	| j  t||	| _| jt dS dS | jtkr3| j jj| jdt| j| jjd}| j jjjs| j jj	|dd\}}}n| j jjj
r|\}}}}n| j jj}| j jj}| j jj}| j ||| |d tkr1tdt| jd | jj | j_|r%|  r%| jjr%| j jj| jjj jjkr%| j| jjj_| jt  d | j_dS dS | jt!krdt| jj}
| j j| j|
}| j jjjs_| j jj	|dd\}}}n| j jjj
rm|\}}}}n| j jj}| j jj}| j jj}| j ||| |d tkr|
d dt"| jd	d   | j_|r|  r| jjr| j jj| jjj jjkr| j| jjj_| jt  d | j_dS dS | jt#t$fv rg }| j%D ]!}|| jj&vs| jj&| j'r|| j(vr|)d
| d
  qdd|| jf }t*t+rt,t+d||  t-|| jt.krn| j(rn| j/j0rr| j(D ]J}| j j1j2| j3| j j1j2| j4 }| j5D ]
}||v rE n+q<| j/j0D ]$}||v rn| jj6t#krg| j(d d d	 )| qK|  j5|7  _5qKq'| jj6t#krt7 }| j(D ]}| j(| d d	 ||< q| j j8| jd ||}n| j j9| j| j(|}| j jjjs| j jj	|dd\}}}n| j jjj
r|\}}}}n| j jj}| j jj}| j jj}| j ||| |d tkrn|rb|  ra| jjra| j jj| jjj jjkra| D ]$}|j:| j/j;v r$|j:| jjj/j;vr$| jjj j<=| j j<j;|j:  q| jjj >| jj}| jjj  |j| jjjd< | D ]}t?|dsX| jjjj&|j: | jjj|j:< qA| j@| jjj_An| B  | jt  dS dS )NT)get_requestresultrw   F+)new_superiorr   r   'z+mandatory attributes %s missing in entry %srE   rg   objectClassr   rj   )Cr   _reset_historyry   r#   r   deleterx   strategysyncget_responsethread_safer8   r   request_store_operation_in_historyr)   rw   r:   serverrJ   clearr0   rn   r!   r$   	modify_dnrL   r   r4   r   r2   entry_refreshr    r%   r   r   r&   r   r6   rj   r   r   r+   r,   r*   r   r   r   r   r   attribute_typesmandatory_inoptional_inr   r3   r[   r   modifyr   rP   r=   add_attributer   r{   rz   r;   entry_discard_changes)r?   refreshcontrolsclear_historyr   r8   r  _r2   r9   rdnmissing_attributesrm   rl   attr_classesobject_class	aux_classnew_attributes
temp_entryr@   r@   rA   entry_commit_changes  s   




 

&


$
$


$
$
*


 







$$z"WritableEntry.entry_commit_changesc                 C   s   | j   | j| jj d S r1   )r   r  rw   rn   r3   rX   r@   r@   rA   r    s   
z#WritableEntry.entry_discard_changesc                 C   sH   | j tttfvrd| j  }ttrttd||  t|| j	t d S )Nz%cannot delete entry, invalid status: rg   )
ry   r   r    r#   r+   r,   r*   r   rw   rn   )r?   rl   r@   r@   rA   entry_delete  s   
zWritableEntry.entry_deleter   r   c                 C   s    | j jr| j | ||rdS dS )z;

        Refreshes the entry from the LDAP Server
        TF)r   r   refresh_entry)r?   triessecondsr@   r@   rA   r    s   zWritableEntry.entry_refreshc                 C   sT   | j tttfvrd| j  }ttrttd||  t|t|| j	_
| j	t d S )Nz#cannot move entry, invalid status: rg   )ry   r   r    r$   r+   r,   r*   r   r   rw   r4   rn   )r?   destination_dnrl   r@   r@   rA   
entry_move  s   
zWritableEntry.entry_movec                 C   sP   | j tttfvrd| j  }ttrttd||  t||| j_	| j
t d S )Nz%cannot rename entry, invalid status: rg   )ry   r   r    r%   r+   r,   r*   r   rw   r4   rn   )r?   new_namerl   r@   r@   rA   entry_rename  s   
zWritableEntry.entry_renamec                 C   ro   r1   )r   rX   r@   r@   rA   entry_changes  rq   zWritableEntry.entry_changes)TNT)r   r   )rR   rr   rs   r   r   r   ru   r   r  r  r  r  r  r!  r"  r@   r@   r@   rA   r     s    

 
		r   )Drt   r   collectionsr   ImportErrorutils.ordDictosr   copyr   rF   r   r   r	   r
   r}   r   	objectDefr   attrDefr   core.exceptionsr   r   r   
utils.convr   r   r   protocol.rfc2849r   r   utils.dnr   r   r   
utils.reprr   utils.ciDictr   utils.configr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   core.resultsr)   	utils.logr*   r+   r,   r-   r.   r/   rS   r0   rv   r   r   r@   r@   r@   rA   <module>   s8    8 D sr