auth_backend.py 1.6 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 7 8
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User

class RepanierCustomBackend(ModelBackend):
	def authenticate(self, **credentials):
Patrick Colmant's avatar
Patrick Colmant committed
9 10 11
		user_or_none = None
		try:
			user_or_none = super(RepanierCustomBackend, self).authenticate(**credentials)
pi's avatar
pi committed
12 13 14 15 16 17 18 19 20 21
			if user_or_none and not user_or_none.is_superuser:
				# if not (user_or_none.is_active):
				# 	user_or_none = None
				# else:
				is_customer = False
				is_staff = False
				try:
					a = user_or_none.customer
					is_customer = True
				except:
Patrick Colmant's avatar
Patrick Colmant committed
22
					try:
pi's avatar
pi committed
23 24
						a = user_or_none.staff
						is_staff = True
Patrick Colmant's avatar
Patrick Colmant committed
25
					except:
pi's avatar
pi committed
26
						user_or_none = None
Patrick Colmant's avatar
Patrick Colmant committed
27 28 29 30 31 32
		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
33 34 35
		return user_or_none

	def get_user(self, user_id):
Patrick Colmant's avatar
Patrick Colmant committed
36
		user_or_none = None
Patrick Colmant's avatar
Patrick Colmant committed
37 38
		try:
			user_or_none = User.objects.get(pk=user_id)
pi's avatar
pi committed
39 40 41 42 43 44 45 46 47 48
			if user_or_none and not user_or_none.is_superuser:
				# if not (user_or_none.is_active):
				# 	user_or_none = None
				# else:
				is_customer = False
				is_staff = False
				try:
					a = user_or_none.customer
					is_customer = True
				except:
Patrick Colmant's avatar
Patrick Colmant committed
49
					try:
pi's avatar
pi committed
50 51
						a = user_or_none.staff
						is_staff = True
Patrick Colmant's avatar
Patrick Colmant committed
52
					except:
pi's avatar
pi committed
53
						user_or_none = None
Patrick Colmant's avatar
Patrick Colmant committed
54
		except:
Patrick Colmant's avatar
Patrick Colmant committed
55
			user_or_none = None
Patrick Colmant's avatar
Patrick Colmant committed
56 57 58 59
		# 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
60
		return user_or_none