o
    h                     @   s   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	 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 d d
lmZ d dlZd dlmZ e ZeeZG dd dZG dd dZdS )    )timezone)login_required)JsonResponse)redirect)reverse)AnonymousUser)MiddlewareMixin)logout)messages)	timedeltaN)get_user_modelc                   @   s    e Zd ZdZdd Zdd ZdS )CustomerOnlyMiddlewarez7Middleware to handle customer-only access and redirectsc                 C   s
   || _ d S Nget_responseselfr    r   4/var/www/html/optinet_system/customers/middleware.py__init__   s   
zCustomerOnlyMiddleware.__init__c                 C   s   |  |}|S r   r   r   requestresponser   r   r   __call__   s   
zCustomerOnlyMiddleware.__call__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    r   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )DashboardSecurityMiddlewarez1Enhanced security middleware for dashboard accessc                 C   s   || _ g d| _d S )N)zadmin/auth/
phpmyadminzwp-adminz.envz
config.php)r   suspicious_patternsr   r   r   r   r   !   s   z$DashboardSecurityMiddleware.__init__c                    sL   t  fdd| jD r|   d jv r jjr|   |  }|S )Nc                 3   s    | ]
}| j  v V  qd S r   )pathlower).0patternr   r   r   	<genexpr>-   s    z7DashboardSecurityMiddleware.__call__.<locals>.<genexpr>z/dashboard/)anyr    log_suspicious_activityr!   useris_authenticatedlog_dashboard_accessr   r   r   r%   r   r   +   s   


z$DashboardSecurityMiddleware.__call__c                 C   sV   |j jr)ddlm} |jj|j d| ||jdddd |j	|j
dd	 dS dS )
zLog suspicious access attemptsr   UserActivityLogsuspicious_accessHTTP_USER_AGENT N  )suspicious_pathmethodr)   action
ip_address
user_agentadditional_data)r)   r*   accounts.modelsr-   objectscreateget_client_ipMETAgetr!   r3   r   r   r-   r   r   r   r(   7   s   
z3DashboardSecurityMiddleware.log_suspicious_activityc              	   C   s\   ddl m} |jj|jd| ||jdddd |jt	|jdr&|jj
nd	d
d dS )z$Log dashboard access for audit trailr   r,   dashboard_accessr/   r0   Nr1   roleunknown)dashboard_path	user_roler4   )r9   r-   r:   r;   r)   r<   r=   r>   r!   hasattrrA   r?   r   r   r   r+   G   s   
z0DashboardSecurityMiddleware.log_dashboard_accessc                 C   s2   |j d}|r|dd }|S |j d}|S )NHTTP_X_FORWARDED_FOR,r   REMOTE_ADDR)r=   r>   split)r   r   x_forwarded_foripr   r   r   r<   V   s   z)DashboardSecurityMiddleware.get_client_ipN)	r   r   r   r   r   r   r(   r+   r<   r   r   r   r   r      s    
r   )django.utilsr   django.contrib.auth.decoratorsr   django.httpr   django.shortcutsr   django.urlsr   django.contrib.auth.modelsr   django.utils.deprecationr   django.contrib.authr	   django.contribr
   datetimer   loggingr   User	getLoggerr   loggerr   r   r   r   r   r   <module>   s"   
