1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-06-19 20:03:03 +00:00
seahub/subdomain/middleware.py
2012-06-20 19:39:21 +08:00

38 lines
1.5 KiB
Python

from django.http import HttpResponseRedirect, Http404
from seahub.settings import SITE_BASE_NAME, SITE_SUBDOMAIN
class SubdomainMiddleware(object):
def process_request(self, request):
if not request.user.is_authenticated():
return None
host = request.META.get('HTTP_HOST', '')
http_or_https = request.is_secure() and 'https://' or 'http://'
has_subdomain = True if host.replace(SITE_BASE_NAME, '', 1).find('.') >= 0 else False
full_path = request.get_full_path()
if request.user.org:
# business account
url_prefix = request.user.org.url_prefix
if not has_subdomain:
host = request.user.org.url_prefix + '.' + host
return HttpResponseRedirect(http_or_https + host + full_path)
elif host.split('.')[0] != url_prefix:
host = url_prefix + '.' + '.'.join(host.split('.')[1:])
return HttpResponseRedirect(http_or_https + host + full_path)
else:
# personal account
if not has_subdomain:
host = SITE_SUBDOMAIN + '.' + host
return HttpResponseRedirect(http_or_https + host + full_path)
elif host.split('.')[0] != SITE_SUBDOMAIN:
host = SITE_SUBDOMAIN + '.' + '.'.join(host.split('.')[1:])
return HttpResponseRedirect(http_or_https + host + full_path)
return None
def process_response(self, request, response):
return response