
    )JfJ                        d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
mZ d dl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 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 Z%d0dZ&d1dZ'da(d Z)d Z*d Z+dddZ,dddZ-dddZ.d2dZ/	 	 	 	 d3dZ0d Z1d  Z2d! Z3d4d"Z4d# Z5d2d$Z6d% Z7d& Z8d5d'Z9d( Z:d) Z;d6d*Z<d+ Z=d, Z>d- Z?d.ee@         fd/ZAdS )7    N)OrderedDict)Optional)settings)messages)get_user_modelupdate_session_auth_hash)FieldDoesNotExist)models)Q)	force_str)base36_to_intint_to_base36	urlencode)app_settingssignals)get_adapter)Login)ImmediateHttpResponse)get_request_paramimport_callablevalid_email_or_nonec                     t          j        d|                                           }t          j        d|                                          }||k    S )z
    Perform case-insensitive comparison of two identifiers, using the
    recommended algorithm from Unicode Technical Report 36, section
    2.11.2(B)(2).
    NFKC)unicodedata	normalizecasefold)s1s2norm_s1norm_s2s       X/home/alex/cs2snipeproduction/venv/lib/python3.11/site-packages/allauth/account/utils.py_unicode_ci_comparer"      sK     #FB//88::G#FB//88::Gg    nextc                 l    t          | |          }t                                          |          sd}|S )z_
    Returns the next URL to redirect to, if it was explicitly passed
    via the request.
    N)r   r   is_safe_url)requestredirect_field_nameredirect_tos      r!   get_next_redirect_urlr*   $   s9    
 $G-@AAK==$$[11 r#   Fc                     |}|rt          |          r
 |            }|st          | |          }|sE|r"t                                          |           }n!t                                          |           }|S )N)r(   )callabler*   r   get_signup_redirect_urlget_login_redirect_url)r'   urlr(   signuprets        r!   r.   r.   /   s    
C
 x}}  cee V#GATUUU @ 	@--77@@CC--66w??CJr#   c                 B    t           j        st          | |           d S d S N)r   LOGOUT_ON_PASSWORD_CHANGEr   )r'   users     r!   logout_on_password_changer6   B   s/     1 0 $/////0 0r#   c                 l    t           j        rt          | t           j                  S t          |           S r3   )r   USER_MODEL_USERNAME_FIELDgetattrr   r5   s    r!   default_user_displayr;   J   s,    - t\CDDDr#   c                     t           s*t          t          dt                    }t	          |          a t          |           S )NACCOUNT_USER_DISPLAY)_user_display_callabler9   r   r;   r   )r5   fs     r!   user_displayr@   Q   s9    ! 4H46JKK!0!3!3!$'''r#   )commitc                ^   |sdS t                      }	 |j                            |          }|j        }n%# t          $ r t          | |          sY dS d}Y nw xY w|rB|d         }|r
|d|         }t          | ||           |r|                     |g           dS dS t          | |          S )zR
    Gets or sets (optional) user model fields. No-op if fields do not exist.
    Nr   )update_fields)	r   _meta	get_field
max_lengthr	   hasattrsetattrsaver9   )r5   fieldrA   argsUser
field_metarF   vs           r!   
user_fieldrO   Y   s      DZ))%00
*

   tU## 	FF


  
$G 	 !J,AeQ 	-IIUGI,,,,,	- 	- tU###s   !6 AAAc                    |r/t           j        s#|d         r|d                                         g}t          | t           j        g|R  S )Nr   )r   PRESERVE_USERNAME_CASINGlowerrO   r8   r5   rA   rK   s      r!   user_usernamerT   t   sN     !L9 !d1g !Q dLBJTJJJJr#   c                8    t          | t          j        g|R d|iS )NrA   )rO   r   USER_MODEL_EMAIL_FIELDrS   s      r!   
user_emailrW   z   s%    dL?V$VVVvVVVr#   c                     ddl m} d }|r6d}	 |j                            | |          }|j        }n># |j        $ r Y n2w xY w|j                            | d                                          }|S )N   EmailAddressFT)r5   verified)r
   r[   objectsget_for_userr\   DoesNotExistfilterexists)r5   emailr[   emailaddressr1   s        r!   has_verified_emailrd   ~   s    $$$$$$L M	'/<<T5IIL'CC( 	 	 	D	 "))td)CCJJLLJs   "1 
>>c                 L    t          ||||||          }t          | |          S )z
    Keyword arguments:

    signup -- Indicates whether or not sending the
    email is essential (during signup), or if it can be skipped (e.g. in
    case email verification is optional and we are only logging in).
    )r5   email_verificationredirect_urlsignal_kwargsr0   rb   )r   _perform_login)r'   r5   rf   rg   rh   r0   rb   logins           r!   perform_loginrk      s<      -!#  E '5)))r#   c                     t                      }t          |          } |j        | |j        fi |}|r|S t	          | |          S r3   )r   _get_login_hook_kwargs	pre_loginr5   resume_login)r'   rj   adapterhook_kwargsresponses        r!   ri   ri      sU    
 mmG(//K w %*DDDDH '''r#   c                 \    t          | j        | j        | j        | j        | j                  S )zc
    TODO: Just break backwards compatibility and pass only `login` to
    `pre/post_login()`.
    )rf   rg   rh   r0   rb   )dictrf   rg   rh   r0   rb   )rj   s    r!   rm   rm      s7    
  3')|k   r#   c                 :   ddl m} t                      } || |          }	 |                                }|r|S |                    | |j                   t          |          } |j        | |j        fi |}|r|S n# t          $ r}|j	        }Y d }~nd }~ww xY w|S )Nr   )LoginStageController)
allauth.account.stagesrv   r   handlerj   r5   rm   
post_loginr   rr   )r'   rj   rv   rp   ctrlrr   rq   es           r!   ro   ro      s    ;;;;;;mmG//D
;;== 	Oguz***,U33%7%guzII[II 	O	    :Os   A= AA= =
BBBc                     d }|r| j                             d          }n| j                             dd           }|-	 t          j        |          }d| _        n# t          $ r Y nw xY w|S Naccount_loginT)sessiongetpopr   deserialize_account_login_accessed
ValueError)r'   peekrj   datas       r!   unstash_loginr      s    E :""?33""?D99	%d++E.2G++ 	 	 	D	Ls   A 
A'&A'c                 L    |                                 | j        d<   d| _        d S r}   )	serializer   r   )r'   rj   s     r!   stash_loginr      s&    ','8'8GOO$&*G###r#   c                 r    |i }t          j        j        d|j        | |d| t	          | ||d||          S )N)senderr'   r5   T)rf   r0   rg   rh    )r   user_signed_upsend	__class__rk   )r'   r5   rf   success_urlrh   s        r!   complete_signupr      sk     ~wT =J   - #   r#   c                    ddl m} t                      }t                      }g }g }g }|D ]t}t	          |j                  }	|	st          j        r,t          j        dk    r|j	        
                    |	g          rRt          j        r2t          j        dk    r"|j        r|j	                            |	          r|                    |	                                          }
|
r'|
j        p|j        |
_        |
j        p|j        |
_        n@|}
|
j        p|                    | |
j                  |
_        |
||	                                <   |
j        r1|                    |
           |
j        r|                    |
           |
j        r|                    |
           v|r	|d         }nB|r	|d         }n7|r	|d         }n,|r(t%          |                                          d         }nd}|                                D ];}
|j                                        |
j                                        k    |
_        <t%          |                                          |fS )a
  
    Takes a list of EmailAddress instances and cleans it up, making
    sure only valid ones remain, without multiple primaries etc.

    Order is important: e.g. if multiple primary email addresses
    exist, the first one encountered will be kept as primary.
    rY   rZ   strictr   N)r
   r[   r   r   r   rb   r   UNIQUE_EMAILPREVENT_ENUMERATIONr]   lookupr\   is_verifiedr   rR   primaryis_email_verifiedappendlistvalues)r'   	addressesr[   rp   e2aprimary_addressesverified_addressesprimary_verified_addressesaddressrb   aprimary_addresss               r!   cleanup_email_addressesr      sx    %$$$$$mmG
--C!# ") ")#GM22 	 %	0H<<$++UG44 = %	0H<<  =$0077 =
 GGEKKMM"" 	#	4W_AI7w'7AJJARw'@'@!''R'RAJ!"C9 	5$$Q'''z 5*11!444: 	)%%a(((! 4Q7	 ,Q/	 +A.	 szz||,,Q/ ZZ\\ E E#)//11QW]]__D		

..r#   c                    ddl m} |j                            |                                          rJ g }t                      }|                    |           }|r"|                     |||dd                     t          |          }|r"|                     |||dd                     t          | ||z             \  }}|D ]}	||	_
        |	                                 |j                            ||           |rZ|rX|                                |j                                        k    r)t          ||j                   |                                 |S )z
    Creates proper EmailAddress for the user that was just signed
    up. Only sets up, doesn't do any other handling such as sending
    out email confirmation mails etc.
    rY   rZ   r:   T)r5   rb   r   r\   F)r
   r[   r]   r`   ra   r   unstash_verified_emailr   rW   r   r5   rI   fill_cache_for_userrR   rb   )
r'   r5   r   r[   priority_addressesrp   stashed_emailrb   r   r   s
             r!   setup_user_emailr   C  s    %$$$$$#***55<<>>>>>mmG227;;M 
!!Ld-PTUUU	
 	
 	
 tE 
!!Ld%NNN	
 	
 	
 1#i/ Iw   	,,T9=== 5 U[[]]gm.A.A.C.CCC4'''		Nr#   c           	         ddl m} t                      }d}|st          |          }|sJ|j                            |                              dd                                          }|r|j        }|r|-	 |j        	                    ||          }n# |j
        $ r Y nw xY w|;|j        s1|                    | ||          }|r|                    | |           n(d}n%d	}|j                            | |||d	
          }|sJ |r'|                    | t           j        d|| |d           |r%|                    | t'          |                     dS dS )a[  
    Email verification mails are sent:
    a) Explicitly: when a user signs up
    b) Implicitly: when a user attempts to log in using an unverified
    email while EMAIL_VERIFICATION is mandatory.

    Especially in case of b), we want to limit the number of mails
    sent (consider a user retrying a few times), which is why there is
    a cooldown period before sending a new mail. This cooldown period
    can be configured in ACCOUNT_EMAIL_CONFIRMATION_COOLDOWN setting.

    TODO: This code is doing way too much. Looking up EmailAddress, creating
    if not present, etc. To be refactored.
    rY   rZ   Nr:   r\   pk)r0   FT)r0   confirmz,account/messages/email_confirmation_sent.txt)rb   rj   r0   )r
   r[   r   rW   r]   r`   order_byfirstrb   r^   r_   r\   should_send_confirmation_mailsend_confirmation	add_emailadd_messager   INFO
stash_useruser_pk_to_url_str)r'   r5   r0   rb   r[   rp   email_address
send_emails           r!   send_email_confirmationr   f  s    %$$$$$mmGM !4   ( ''T'22;;JMMSSUU 	  	(!'E   , 4 A A$ N N,   $ ) #$BB]F 
  L!33GF3KKK"

J(0::uVT ;  M !    	>f*GG	    >7$6t$<$<=====> >s   9B 
B"!B"c                     ddl m} t          |           }|rP|j                            | |                                          s$|j                            | |ddd           dS dS dS )z
    Keep user.email in sync with user.emailaddress_set.

    Under some circumstances the user.email may not have ended up as
    an EmailAddress record, e.g. in the case of manually created admin
    users.
    rY   rZ   )r5   email__iexactF)r   r\   )r5   rb   defaultsN)r
   r[   rW   r]   r`   ra   get_or_create)r5   r[   rb   s      r!   sync_user_email_addressesr     s     %$$$$$tE
$++U+KKRRTT

 	**UE-R-R 	+ 	
 	
 	
 	
 	

 
 
 
r#   c                  &   t           j        rMd | D             }|d         }|dd          D ]}||z  }t                      j                            |          }n7 t                      j        j        di t           j        dz   d | D             i}|S )Nc                 D    g | ]}t          di t          j        d z   |iS )__iexactr   )r   r   r8   .0us     r!   
<listcomp>z,filter_users_by_username.<locals>.<listcomp>  sC     
 
 
 II7*DaHII
 
 
r#   r   rY   __inc                 6    g | ]}|                                 S r   )rR   r   s     r!   r   z,filter_users_by_username.<locals>.<listcomp>  s     77717799777r#   r   )r   rQ   r   _default_managerr`   r8   )usernameqlistqq2r1   s        r!   filter_users_by_usernamer     s    , 

 

 
 
 !H) 	 	BBAA/66q996n/6 
 
677h777
 
 Jr#   c                    ddl m} t                      }|j                            |                               d          }t          |          }d}|r$t          t	          d |                    }|r|}d}g }|D ]1}	t          |	j        |           r|	                    |	j
                   2t          j        r{|syt          j        dz   | i}
 |j        j        di |
}|                                D ]A}t          |t          j                  }t          ||           r|	                    |           Bfd
t          |          D             }t          t          |                    S )aE  Return list of users by email address

    Typically one, at most just a few in length.  First we look through
    EmailAddress table, than customisable User model table. Add results
    together avoiding SQL joins and deduplicate.

    `prefer_verified`: When looking up users by email, there can be cases where
    users with verified email addresses are preferable above users who did not
    verify their email address. The password reset is such a use case -- if
    there is a user with a verified email than that user should be returned, not
    one of the other users.
    rY   rZ   )r   r5   Fc                     | j         S r3   )r\   )r{   s    r!   <lambda>z'filter_users_by_email.<locals>.<lambda>  s    qz r#   Tr   Nc                 *    g | ]}|j         k    |S r   )	is_active)r   r   r   s     r!   r   z)filter_users_by_email.<locals>.<listcomp>  s%    CCCq!+*B*B*B*B*Br#   r   )r
   r[   r   r]   r`   prefetch_relatedr   r"   rb   r   r5   r   rV   iteratorr9   set)rb   r   prefer_verifiedr[   rL   mailsr   verified_mailsusersr{   q_dictuser_qsr5   rW   s    `            r!   filter_users_by_emailr     s    %$$$$$D ''e'<<MMfUUEKKEK f%9%95AABB 	"EKE ! !qw.. 	!LL   * #; #5
BEJ%$,%////$$&& 	# 	#D |'JKKJ":u55 #T"""CCCCCJJCCCE

r#   c                     |                     d          dk     sJ t          | |          }|r|dz   t          ||i          z   }|S )N?r   )findr*   r   )r'   r/   r(   next_urls       r!   passthrough_next_redirect_urlr     sW    88C==1$W.ABBH ECi)%8($CDDDJr#   c                 |   t                      }t          |j        j                  }t	          |t
          j                  r-t          | j        t                    r| j        S | j        j	        S t	          |t
          j
                  r!t          t          | j                            S t          | j                  S )z&
    This should return a string.
    )r   typerD   r   
issubclassr
   	UUIDField
isinstancestrhexIntegerFieldr   int)r5   rL   pk_field_classs      r!   r   r     s     D$*-((N.&"233 +dgs## 	7Nw{	NF$7	8	8 +S\\***tw<<r#   c                    t                      }t          |j        j        dd           }|r2t          |dd           r!|j        j        j        j        j        j        }n|j        j        }t          |          }t          |t          j	                  r%t          |           }|                    |          }n|                    |           }|S )Nremote_fieldto)r   r9   rD   r   r   r   r   r   r
   r   r   	to_python)pk_strrL   r   pk_fieldr   r   s         r!   url_str_to_user_pkr     s    D4:=.$??L !dD99 !:=-069:=(^^N.&"566 (6"" ##''Ir#   returnc                     t          |           sdS t          j        sdS t          j        t          j        j        k    rt          j        sJ dS t          j        dk    rdS t          j        du sJ dS )z
    True -- email is unique
    False -- email is already in use
    None -- email is in use, but we should hide that using email verification.
    TFNr   )r   r   r   EMAIL_VERIFICATIONEmailVerificationMethod	MANDATORY)rb   s    r!   assess_unique_emailr     s     !'' t- u'/9	: 	: ////t		)X	5	5
 t/47777
 ur#   )r$   )Nr$   Fr3   )NNFN)F)FN)NF)Br   collectionsr   typingr   django.confr   django.contribr   django.contrib.authr   r   django.core.exceptionsr	   	django.dbr
   django.db.modelsr   django.utils.encodingr   django.utils.httpr   r   r   allauth.accountr   r   allauth.account.adapterr   allauth.account.modelsr   allauth.core.exceptionsr   allauth.utilsr   r   r   r"   r*   r.   r>   r6   r;   r@   rO   rT   rW   rd   rk   ri   rm   ro   r   r   r   r   r   r   r   r   r   r   r   r   boolr   r   r#   r!   <module>r     s       # # # # # #                   # # # # # # H H H H H H H H 4 4 4 4 4 4             + + + + + + E E E E E E E E E E 1 1 1 1 1 1 1 1 / / / / / / ( ( ( ( ( ( 9 9 9 9 9 9                    0 0 0  ( ( ( +0 $ $ $ $ $6 ', K K K K K $) W W W W W   ( 
* * * *6
( 
( 
(    &   + + +
    E/ E/ E/P     F;> ;> ;> ;>|
 
 
*  (% % % %P      $!(4. ! ! ! ! ! !r#   