1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-08-12 12:22:13 +00:00
seahub/thirdpart/termsandconditions/middleware.py

61 lines
2.0 KiB
Python
Raw Normal View History

2016-07-15 02:47:32 +00:00
"""Terms and Conditions Middleware"""
2020-07-27 06:59:18 +00:00
from django.utils.deprecation import MiddlewareMixin
2016-07-15 02:47:32 +00:00
from .models import TermsAndConditions
from django.conf import settings
import logging
from .pipeline import redirect_to_terms_accept
2018-06-05 09:32:48 +00:00
from constance import config
2016-07-15 02:47:32 +00:00
LOGGER = logging.getLogger(name='termsandconditions')
ACCEPT_TERMS_PATH = getattr(settings, 'ACCEPT_TERMS_PATH', '/terms/accept/')
2018-06-09 07:58:55 +00:00
TERMS_EXCLUDE_URL_PREFIX_LIST = getattr(settings, 'TERMS_EXCLUDE_URL_PREFIX_LIST', {'/admin', '/terms', '/media', '/static', '/api2'})
TERMS_EXCLUDE_URL_LIST = getattr(settings, 'TERMS_EXCLUDE_URL_LIST', {'/termsrequired/', '/accounts/logout/', '/securetoo/'})
2016-07-15 02:47:32 +00:00
2020-07-27 06:59:18 +00:00
class TermsAndConditionsRedirectMiddleware(MiddlewareMixin):
2016-07-15 02:47:32 +00:00
"""
This middleware checks to see if the user is logged in, and if so,
if they have accepted the site terms.
"""
def process_request(self, request):
"""Process each request to app to ensure terms have been accepted"""
2018-06-05 09:32:48 +00:00
if not config.ENABLE_TERMS_AND_CONDITIONS:
2016-08-02 07:03:19 +00:00
return None
2016-07-15 02:47:32 +00:00
LOGGER.debug('termsandconditions.middleware')
current_path = request.META['PATH_INFO']
protected_path = is_path_protected(current_path)
2021-04-21 15:38:53 +00:00
if request.user.is_authenticated and protected_path:
2016-07-15 02:47:32 +00:00
for term in TermsAndConditions.get_active_list():
if not TermsAndConditions.agreed_to_latest(request.user, term):
return redirect_to_terms_accept(current_path, term)
return None
def is_path_protected(path):
"""
returns True if given path is to be protected, otherwise False
The path is not to be protected when it appears on:
TERMS_EXCLUDE_URL_PREFIX_LIST, TERMS_EXCLUDE_URL_LIST or as
ACCEPT_TERMS_PATH
"""
protected = True
for exclude_path in TERMS_EXCLUDE_URL_PREFIX_LIST:
if path.startswith(exclude_path):
protected = False
if path in TERMS_EXCLUDE_URL_LIST:
protected = False
if path.startswith(ACCEPT_TERMS_PATH):
protected = False
return protected