B
    £0H[  ã               @   s`   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 )é    )Úget_user_model)Ú
Permissionc               @   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   sd   |d kr|  tj¡}ytj |¡}W n" tjk
rF   tƒ  |¡ Y nX | |¡r`|  |¡r`|S d S )N)	ÚgetÚ	UserModelÚUSERNAME_FIELDÚ_default_managerÚget_by_natural_keyÚDoesNotExistZset_passwordZcheck_passwordÚuser_can_authenticate)ÚselfÚrequestÚusernameÚpasswordÚkwargsÚuser© r   ú`C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\contrib\auth\backends.pyÚauthenticate   s    zModelBackend.authenticatec             C   s   t |ddƒ}|p|dk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 )N)Zuser_permissionsÚall)r   Úuser_objr   r   r   Ú_get_user_permissions!   s    z"ModelBackend._get_user_permissionsc             C   s,   t ƒ j d¡}d| ¡  }tjjf ||iŽS )NÚgroupsz	group__%s)r   Z_metaÚ	get_fieldZrelated_query_namer   ÚobjectsÚfilter)r   r   Zuser_groups_fieldZuser_groups_queryr   r   r   Ú_get_group_permissions$   s    z#ModelBackend._get_group_permissionsc             C   s€   |j r|js|dk	rtƒ S d| }t||ƒsv|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_permissionsZcontent_type__app_labelZcodenamec             S   s   h | ]\}}d ||f ’qS )z%s.%sr   )Ú.0ÚctÚnamer   r   r   ú	<setcomp>9   s    z0ModelBackend._get_permissions.<locals>.<setcomp>)r   Úis_anonymousÚsetÚhasattrZis_superuserr   r   r   r   Zvalues_listZorder_byÚsetattr)r   r   ÚobjÚ	from_nameZperm_cache_nameZpermsr   r   r   Ú_get_permissions)   s    
zModelBackend._get_permissionsc             C   s   |   ||d¡S )zs
        Return a set of permission strings the user `user_obj` has from their
        `user_permissions`.
        r   )r)   )r   r   r'   r   r   r   Úget_user_permissions<   s    z!ModelBackend.get_user_permissionsc             C   s   |   ||d¡S )zq
        Return a set of permission strings the user `user_obj` has from the
        groups they belong.
        Úgroup)r)   )r   r   r'   r   r   r   Úget_group_permissionsC   s    z"ModelBackend.get_group_permissionsc             C   sV   |j r|js|d k	rtƒ S t|dƒsPtƒ |_|j |  |¡¡ |j |  |¡¡ |jS )NÚ_perm_cache)r   r#   r$   r%   r-   Úupdater*   r,   )r   r   r'   r   r   r   Úget_all_permissionsJ   s    
z ModelBackend.get_all_permissionsc             C   s   |j s
dS ||  ||¡kS )NF)r   r/   )r   r   Úpermr'   r   r   r   Úhas_permS   s    zModelBackend.has_permc             C   s<   |j s
dS x,|  |¡D ]}|d| d¡… |krdS qW dS )zU
        Return True if user_obj has any permissions in the given app_label.
        FNÚ.T)r   r/   Úindex)r   r   Z	app_labelr0   r   r   r   Úhas_module_permsX   s    zModelBackend.has_module_permsc             C   s<   yt jj|d�}W n t jk
r(   d S X |  |¡r8|S d S )N)Úpk)r   r   r   r
   r   )r   Zuser_idr   r   r   r   Úget_userc   s
    zModelBackend.get_user)NN)N)N)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r)   r*   r,   r/   r1   r4   r6   r   r   r   r   r      s   



	
r   c               @   s   e Zd Zdd„ ZdS )ÚAllowAllUsersModelBackendc             C   s   dS )NTr   )r   r   r   r   r   r   l   s    z/AllowAllUsersModelBackend.user_can_authenticateN)r7   r8   r9   r   r   r   r   r   r;   k   s   r;   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rDtjjf tj|iŽ\}}|rl|  |¡}n(ytj |¡}W n tjk
rj   Y nX |  	|¡rz|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.
        N)
Úclean_usernameÚcreate_unknown_userr   r   Zget_or_creater   Úconfigure_userr	   r
   r   )r   r   Zremote_userr   r   Úcreatedr   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?   ¦   s    z RemoteUserBackend.configure_userN)r7   r8   r9   r:   r>   r   r=   r?   r   r   r   r   r<   p   s
   
	r<   c               @   s   e Zd Zdd„ ZdS )ÚAllowAllUsersRemoteUserBackendc             C   s   dS )NTr   )r   r   r   r   r   r   °   s    z4AllowAllUsersRemoteUserBackend.user_can_authenticateN)r7   r8   r9   r   r   r   r   r   rA   ¯   s   rA   N)	Zdjango.contrib.authr   Zdjango.contrib.auth.modelsr   r   r   r;   r<   rA   r   r   r   r   Ú<module>   s   d?