o
    .yh0                     @   sz   d dl mZ d dlmZ d dlmZ d dlmZ e ZG dd deZ	G dd deZ
G d	d
 d
eZG dd deZdS )    )ModelBackend)get_user_model)Customer)Employeec                   @      e Zd ZdZdddZdS )SuperuserAuthBackendzF
    Authentication backend specifically for superusers and staff
    Nc                 K   s   |d u s|d u r
d S z4t jj|d}|jr ||r |jr |W S |jr3t|ds6||r9|jr<|W S W d S W d S W d S W d S  t jyI   Y d S w )Nusernameemployee_profile)	Userobjectsgetis_superusercheck_password	is_activeis_staffhasattrDoesNotExist)selfrequestr	   passwordkwargsuser r   1/var/www/html/optinet_system/accounts/backends.pyauthenticate   s.   z!SuperuserAuthBackend.authenticateNN__name__
__module____qualname____doc__r   r   r   r   r   r   
       r   c                   @   r   )EmployeeAuthBackendz;
    Authentication backend specifically for employees
    Nc                 K   n   |d u s|d u r
d S d }zt jj|dd}|jr|jjr|j}W n t jy+   Y d S w |r5||r5|S d S )Nactiveemployee_idemployment_status)r   r   r   r   r   r   r   )r   r   r	   r   r   r   employeer   r   r   r   *      z EmployeeAuthBackend.authenticater   r   r   r   r   r   r#   %   r"   r#   c                   @   r   )CustomerAuthBackendz;
    Authentication backend specifically for customers
    Nc                 K   r$   )NT)customer_idr   )r   r   r   r   r   r   r   )r   r   r	   r   r   r   customerr   r   r   r   D   r*   z CustomerAuthBackend.authenticater   r   r   r   r   r   r+   ?   r"   r+   c                   @   s"   e Zd ZdZdddZdd ZdS )CustomerEmployeeAuthBackendz
    Custom authentication backend that allows login with:
    - Employee ID (for employees)
    - Customer ID (for customers)
    - Regular username (for admin users)
    Nc                 K   s   |d u s|d u r
d S d }zt jj|dd}|jr|j}W n
 t jy&   Y nw |sCztjj|d}|jr7|j}W n
 tjyB   Y nw |sYz	tjj|d}W n
 tjyX   Y nw |rb||rb|S d S )Nr%   r&   )r,   r   )r   r   r   r   r   r   r   r   )r   r   r	   r   r   r   r)   r-   r   r   r   r   a   s:   z(CustomerEmployeeAuthBackend.authenticatec                 C   s(   zt jj|dW S  t jy   Y d S w )N)pk)r   r   r   r   )r   user_idr   r   r   get_user   s
   z$CustomerEmployeeAuthBackend.get_userr   )r   r   r    r!   r   r1   r   r   r   r   r.   Y   s    
$r.   N)django.contrib.auth.backendsr   django.contrib.authr   customers.modelsr   	hr.modelsr   r   r   r#   r+   r.   r   r   r   r   <module>   s   