auth_backend.py 3.09 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
		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:
					# if user_or_none.userprofile.login_site != Site.objects.get_current().id:
					is_customer = False
					is_staff = False
					is_producer = False
					try:
						a = user_or_none.customer
						is_customer = True
					except:
						try:
							a = user_or_none.staff
							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(
							site__exact = settings.SITE_ID,
							customer__exact = user_or_none.customer)
						if not(site_customer.exists()):
							user_or_none = None
					elif is_staff:
						# A staff member may only access to one django admin site
						if user_or_none.staff.login_site.id != settings.SITE_ID:
							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
56 57 58
		return user_or_none

	def get_user(self, user_id):
Patrick Colmant's avatar
Patrick Colmant committed
59
		user_or_none = None
Patrick Colmant's avatar
Patrick Colmant committed
60 61 62 63 64 65 66
		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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
					is_customer = False
					is_staff = False
					is_producer = False
					try:
						a = user_or_none.customer
						is_customer = True
					except:
						try:
							a = user_or_none.staff
							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(
							site__exact = settings.SITE_ID,
							customer__exact = user_or_none.customer)
						if not(site_customer.exists()):
							user_or_none = None
					elif is_staff:
						# A staff member may only access to one django admin site
						if user_or_none.staff.login_site.id != settings.SITE_ID:
							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
98
			user_or_none = None
Patrick Colmant's avatar
Patrick Colmant committed
99 100 101 102
		# 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
103
		return user_or_none