auth_backend.py 3 KB
Newer Older
Patrick Colmant's avatar
Patrick Colmant committed
1 2 3
# -*- coding: utf-8 -*-
from const import *
from django.conf import settings
Patrick Colmant's avatar
Patrick Colmant committed
4 5 6
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
Patrick Colmant's avatar
Patrick Colmant committed
7
from repanier.models import SiteCustomer
Patrick Colmant's avatar
Patrick Colmant committed
8 9 10

class RepanierCustomBackend(ModelBackend):
	def authenticate(self, **credentials):
Patrick Colmant's avatar
Patrick Colmant committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
		user_or_none = None
		try:
			user_or_none = super(RepanierCustomBackend, self).authenticate(**credentials)
			if user_or_none:
				if user_or_none.is_superuser:
					if not (user_or_none.is_active):
						user_or_none = None
				else:
					is_customer = False
					is_staff = False
					is_producer = False
					try:
						a = user_or_none.customer
						is_customer = True
					except:
						try:
27
							a = user_or_none.sitestaff
Patrick Colmant's avatar
Patrick Colmant committed
28 29 30 31 32 33 34 35 36
							is_staff = True
						except:
							try:
								a = user_or_none.producer
								is_producer = True
							except:
								user_or_none = None
					if is_customer:
						site_customer = SiteCustomer.objects.filter(
37 38
							site_id = settings.SITE_ID,
							customer_id = user_or_none.customer)
Patrick Colmant's avatar
Patrick Colmant committed
39 40 41 42
						if not(site_customer.exists()):
							user_or_none = None
					elif is_staff:
						# A staff member may only access to one django admin site
43
						if user_or_none.sitestaff.site_id != settings.SITE_ID:
Patrick Colmant's avatar
Patrick Colmant committed
44 45 46 47 48 49 50 51 52 53 54
							user_or_none = None
					elif is_producer:
						# Only allowed to log into the site dedicated to producers
						if SITE_ID_PRODUCER != settings.SITE_ID:
							user_or_none = None
		except Exception as e:
			user_or_none = None
		# if user_or_none :
		# 	print ('Authenyticate user : %s' % getattr(user_or_none, get_user_model().USERNAME_FIELD))
		# else:
		# 	print ('Authenticate user : not defined')	
Patrick Colmant's avatar
Patrick Colmant committed
55 56 57
		return user_or_none

	def get_user(self, user_id):
Patrick Colmant's avatar
Patrick Colmant committed
58
		user_or_none = None
Patrick Colmant's avatar
Patrick Colmant committed
59 60 61 62 63 64 65
		try:
			user_or_none = User.objects.get(pk=user_id)
			if user_or_none:
				if user_or_none.is_superuser:
					if not (user_or_none.is_active):
						user_or_none = None
				else:
Patrick Colmant's avatar
Patrick Colmant committed
66 67 68 69 70 71 72 73
					is_customer = False
					is_staff = False
					is_producer = False
					try:
						a = user_or_none.customer
						is_customer = True
					except:
						try:
74
							a = user_or_none.sitestaff
Patrick Colmant's avatar
Patrick Colmant committed
75 76 77 78 79 80 81 82 83
							is_staff = True
						except:
							try:
								a = user_or_none.producer
								is_producer = True
							except:
								user_or_none = None
					if is_customer:
						site_customer = SiteCustomer.objects.filter(
84 85
							site_id = settings.SITE_ID,
							customer_id = user_or_none.customer)
Patrick Colmant's avatar
Patrick Colmant committed
86 87 88 89
						if not(site_customer.exists()):
							user_or_none = None
					elif is_staff:
						# A staff member may only access to one django admin site
90
						if user_or_none.sitestaff.site_id != settings.SITE_ID:
Patrick Colmant's avatar
Patrick Colmant committed
91 92 93 94 95 96
							user_or_none = None
					elif is_producer:
						# Only allowed to log into the site dedicated to producers
						if SITE_ID_PRODUCER != settings.SITE_ID:
							user_or_none = None
		except:
Patrick Colmant's avatar
Patrick Colmant committed
97
			user_or_none = None
Patrick Colmant's avatar
Patrick Colmant committed
98 99 100 101
		# if user_or_none :
		# 	print ('Get user : %s' % getattr(user_or_none, get_user_model().USERNAME_FIELD))
		# else:
		# 	print ('Get user : not defined')
Patrick Colmant's avatar
Patrick Colmant committed
102
		return user_or_none