-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.py
50 lines (42 loc) · 1.95 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
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
from django.contrib.auth.models import User
from openid.consumer.consumer import SUCCESS
from django.core.mail import mail_admins
from coursereview.models import UserProfile
class GoogleBackend:
def authenticate(self, openid_response):
allow = ['[email protected]', '[email protected]']
if openid_response is None:
return None
if openid_response.status != SUCCESS:
return None
google_email = openid_response.getSigned('http://openid.net/srv/ax/1.0', 'value.email')
google_firstname = openid_response.getSigned('http://openid.net/srv/ax/1.0', 'value.firstname')
google_lastname = openid_response.getSigned('http://openid.net/srv/ax/1.0', 'value.lastname')
if ("@iiitd.ac.in" not in google_email and google_email not in allow):
return None
try:
#user = User.objects.get(username=google_email)
# Make sure that the e-mail is unique.
user = User.objects.get(email=google_email)
except User.DoesNotExist:
user = User.objects.create_user(google_email, google_email, 'password')
user.save()
user = User.objects.get(username=google_email)
p = UserProfile.objects.get_or_create(user= user,
email= google_email,
defaults= {'name': google_firstname + " " + google_lastname})[0]
if p.email.split("@")[0][-1] in "0123456789": #Most likely a student
print p.email.split("@")[0][-1]
print p.email, p.email.split("@")[0]
p.type = "S"
print p.type
print p.__dict__
else:
p.type = "P"
p.save()
return user
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None