o
    uvXjÏ  ã                   @   s|   d dl Z d dlZd dlmZ d dlmZ d dlmZ eƒ ZG dd„ dƒZ	G dd„ de	ƒZ
G d	d
„ d
e	ƒZG dd„ deƒZdS )é    N)Úget_user_model)Ú
Permission)ÚRemovedInDjango31Warningc                   @   sr   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ Zddd„Z	ddd„Z
ddd„Zddd„Zdd„ Zdd„ ZdS )ÚModelBackendz9
    Authenticates against settings.AUTH_USER_MODEL.
    Nc                 K   sh   |d u r
|  tj¡}ztj |¡}W n tjy#   tƒ  |¡ Y d S w | |¡r0|  |¡r2|S d S d S ©N)	ÚgetÚ	UserModelÚUSERNAME_FIELDÚ_default_managerÚget_by_natural_keyÚDoesNotExistÚset_passwordÚcheck_passwordÚuser_can_authenticate)ÚselfÚrequestÚusernameÚpasswordÚkwargsÚuser© r   úY/var/www/html/myproject/venv/lib/python3.10/site-packages/django/contrib/auth/backends.pyÚauthenticate   s   ýÿzModelBackend.authenticatec                 C   s   t |ddƒ}|p|du S )z{
        Reject users with is_active=False. Custom user models that don't have
        that attribute are allowed.
        Ú	is_activeN)Úgetattr)r   r   r   r   r   r   r      s   z"ModelBackend.user_can_authenticatec                 C   s
   |j  ¡ S r   )Úuser_permissionsÚall)r   Úuser_objr   r   r   Ú_get_user_permissions%   s   
z"ModelBackend._get_user_permissionsc                 C   s0   t ƒ j d¡}d| ¡  }tjjdi ||i¤ŽS )NÚgroupsz	group__%sr   )r   Ú_metaÚ	get_fieldÚrelated_query_namer   ÚobjectsÚfilter)r   r   Úuser_groups_fieldÚuser_groups_queryr   r   r   Ú_get_group_permissions(   s   z#ModelBackend._get_group_permissionsc                 C   s€   |j r
|js
|durtƒ S d| }t||ƒs;|jrtj ¡ }n	t| d| ƒ|ƒ}| 	dd¡ 
¡ }t||dd„ |D ƒƒ t||ƒS )zá
        Return the permissions of `user_obj` from `from_name`. `from_name` can
        be either "group" or "user" to return permissions from
        `_get_group_permissions` or `_get_user_permissions` respectively.
        Nz_%s_perm_cachez_get_%s_permissionsÚcontent_type__app_labelÚcodenamec                 S   s   h | ]
\}}d ||f ’qS )z%s.%sr   )Ú.0ÚctÚnamer   r   r   Ú	<setcomp>=   s    z0ModelBackend._get_permissions.<locals>.<setcomp>)r   Úis_anonymousÚsetÚhasattrÚis_superuserr   r#   r   r   Úvalues_listÚorder_byÚsetattr)r   r   ÚobjÚ	from_nameÚperm_cache_nameÚpermsr   r   r   Ú_get_permissions-   s   

zModelBackend._get_permissionsc                 C   ó   |   ||d¡S )zs
        Return a set of permission strings the user `user_obj` has from their
        `user_permissions`.
        r   ©r9   ©r   r   r5   r   r   r   Úget_user_permissions@   ó   z!ModelBackend.get_user_permissionsc                 C   r:   )zq
        Return a set of permission strings the user `user_obj` has from the
        groups they belong.
        Úgroupr;   r<   r   r   r   Úget_group_permissionsG   r>   z"ModelBackend.get_group_permissionsc                 C   sD   |j r
|js
|d urtƒ S t|dƒsh |  |¡£|  |¡£|_|jS )NÚ_perm_cache)r   r.   r/   r0   r=   r@   rA   r<   r   r   r   Úget_all_permissionsN   s   
ÿþz ModelBackend.get_all_permissionsc                 C   s   |j o
||  ||¡v S r   )r   rB   )r   r   Úpermr5   r   r   r   Úhas_permX   s   zModelBackend.has_permc                    s"   |j ot‡ fdd„|  |¡D ƒƒS )zU
        Return True if user_obj has any permissions in the given app_label.
        c                 3   s&   � | ]}|d |  d¡… ˆ kV  qd S )NÚ.)Úindex)r*   rC   ©Ú	app_labelr   r   Ú	<genexpr>_   s
   € ÿ
ÿz0ModelBackend.has_module_perms.<locals>.<genexpr>)r   ÚanyrB   )r   r   rH   r   rG   r   Úhas_module_perms[   s   þzModelBackend.has_module_permsc                 C   s<   z	t jj|d�}W n t jy   Y d S w |  |¡r|S d S )N)Úpk)r   r
   r   r   r   )r   Úuser_idr   r   r   r   Úget_userd   s   ÿzModelBackend.get_user)NNr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r'   r9   r=   r@   rB   rD   rK   rN   r   r   r   r   r      s    





	r   c                   @   ó   e Zd Zdd„ ZdS )ÚAllowAllUsersModelBackendc                 C   ó   dS ©NTr   ©r   r   r   r   r   r   m   ó   z/AllowAllUsersModelBackend.user_can_authenticateN©rO   rP   rQ   r   r   r   r   r   rT   l   ó    rT   c                   @   s,   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	S )
ÚRemoteUserBackendaÆ  
    This backend is to be used in conjunction with the ``RemoteUserMiddleware``
    found in the middleware module of this package, and is used when the server
    is handling authentication outside of Django.

    By default, the ``authenticate`` method creates ``User`` objects for
    usernames that don't already exist in the database.  Subclasses can disable
    this behavior by setting the ``create_unknown_user`` attribute to
    ``False``.
    Tc                 C   sÊ   |sdS d}|   |¡}| jrItjjdi tj|i¤Ž\}}|rH||f}z
t | j||¡ W n t	yB   |f}t
 d| jj t¡ Y nw | j|Ž }nztj |¡}W n
 tjy[   Y nw |  |¡rc|S dS )ai  
        The username passed as ``remote_user`` is considered trusted. Return
        the ``User`` object with the given username. Create a new ``User``
        object if ``create_unknown_user`` is ``True``.

        Return None if ``create_unknown_user`` is ``False`` and a ``User``
        object with the given username is not found in the database.
        NzEUpdate %s.configure_user() to accept `request` as the first argument.r   )Úclean_usernameÚcreate_unknown_userr   r
   Úget_or_creater	   ÚinspectÚgetcallargsÚconfigure_userÚ	TypeErrorÚwarningsÚwarnÚ	__class__rO   r   r   r   r   )r   r   Úremote_userr   r   ÚcreatedÚargsr   r   r   r   €   s:   	

ÿþýþ
€ÿzRemoteUserBackend.authenticatec                 C   s   |S )zÆ
        Perform any cleaning on the "username" prior to using it to get or
        create the user object.  Return the cleaned username.

        By default, return the username unchanged.
        r   )r   r   r   r   r   r\   ¨   s   z RemoteUserBackend.clean_usernamec                 C   s   |S )z
        Configure a user after creation and return the updated user.

        By default, return the user unmodified.
        r   )r   r   r   r   r   r   ra   ±   s   z RemoteUserBackend.configure_userN)rO   rP   rQ   rR   r]   r   r\   ra   r   r   r   r   r[   q   s    (	r[   c                   @   rS   )ÚAllowAllUsersRemoteUserBackendc                 C   rU   rV   r   rW   r   r   r   r   »   rX   z4AllowAllUsersRemoteUserBackend.user_can_authenticateNrY   r   r   r   r   ri   º   rZ   ri   )r_   rc   Údjango.contrib.authr   Údjango.contrib.auth.modelsr   Údjango.utils.deprecationr   r   r   rT   r[   ri   r   r   r   r   Ú<module>   s    aI