From ba50e500a1da9266f9bee5bc38b69635637f6129 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Wed, 26 Mar 2014 14:48:45 +0530 Subject: User authentication using external database. Django authentication backend overriden to authenticate user using spoken tutorial database. Added database router to handle the database operations. --- testapp/myauthentication/backend.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 testapp/myauthentication/backend.py (limited to 'testapp/myauthentication/backend.py') diff --git a/testapp/myauthentication/backend.py b/testapp/myauthentication/backend.py new file mode 100644 index 0000000..35dcfeb --- /dev/null +++ b/testapp/myauthentication/backend.py @@ -0,0 +1,37 @@ +import hashlib +from django.contrib.auth.models import User, check_password +from models_spoken import MdlUser + +class MyBackend: + supports_object_permissions = False + supports_anonymous_user = False + supports_inactive_user = False + + def authenticate(self, username=None, password=None): + try: + user = MdlUser.objects.get(username=username) + pwd = user.password + uid = user.id + firstname = user.firstname + lastname = user.lastname + email = user.email + p = hashlib.md5(password) + pwd_valid = (pwd == p.hexdigest()) + if user and pwd_valid: + try: + print "here" + user = User.objects.get(username=username) + print "hh" + return user + except Exception, e: + user=User(id=uid, username=username, password=pwd, first_name=firstname, last_name=lastname, email=email) + user.save() + return user + except Exception, e: + return None + + def get_user(self, user_id): + try: + return User.objects.get(pk=user_id) + except Exception, e: + return None -- cgit From 6ed7eb5a06d16a3a761f4c6c99b9fc78ed4027d5 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Fri, 6 Jun 2014 12:21:31 +0530 Subject: README file added to the authentication app. --- testapp/myauthentication/backend.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'testapp/myauthentication/backend.py') diff --git a/testapp/myauthentication/backend.py b/testapp/myauthentication/backend.py index 35dcfeb..698ec54 100644 --- a/testapp/myauthentication/backend.py +++ b/testapp/myauthentication/backend.py @@ -8,23 +8,28 @@ class MyBackend: supports_inactive_user = False def authenticate(self, username=None, password=None): + ''' + Checks username and password with external User table. + If valid then adds the user detail in django User table + and authenticates user. + ''' try: user = MdlUser.objects.get(username=username) pwd = user.password uid = user.id firstname = user.firstname lastname = user.lastname - email = user.email + email_id = user.email p = hashlib.md5(password) pwd_valid = (pwd == p.hexdigest()) if user and pwd_valid: try: - print "here" user = User.objects.get(username=username) - print "hh" return user except Exception, e: - user=User(id=uid, username=username, password=pwd, first_name=firstname, last_name=lastname, email=email) + user=User(id=uid, username=username, password=pwd,\ + first_name=firstname, last_name=lastname,\ + email=email_id) user.save() return user except Exception, e: -- cgit From c4c00dfd3548e2dda9c0fda62cb86fb2237aca10 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Fri, 6 Jun 2014 12:40:15 +0530 Subject: minor changes --- testapp/myauthentication/backend.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'testapp/myauthentication/backend.py') diff --git a/testapp/myauthentication/backend.py b/testapp/myauthentication/backend.py index 698ec54..d82f11a 100644 --- a/testapp/myauthentication/backend.py +++ b/testapp/myauthentication/backend.py @@ -10,8 +10,8 @@ class MyBackend: def authenticate(self, username=None, password=None): ''' Checks username and password with external User table. - If valid then adds the user detail in django User table - and authenticates user. + If valid then adds the user details in django User table + and authenticates the user. ''' try: user = MdlUser.objects.get(username=username) -- cgit From 77caf5d44ab14d56efd4acbc65a6dd2d78c1c1a6 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Fri, 6 Jun 2014 15:20:45 +0530 Subject: changes made as per comments. --- testapp/myauthentication/backend.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'testapp/myauthentication/backend.py') diff --git a/testapp/myauthentication/backend.py b/testapp/myauthentication/backend.py index d82f11a..721fe54 100644 --- a/testapp/myauthentication/backend.py +++ b/testapp/myauthentication/backend.py @@ -1,6 +1,7 @@ import hashlib from django.contrib.auth.models import User, check_password -from models_spoken import MdlUser +from models_spoken_tutorial import MoodleUser + class MyBackend: supports_object_permissions = False @@ -14,24 +15,24 @@ class MyBackend: and authenticates the user. ''' try: - user = MdlUser.objects.get(username=username) + user = MoodleUser.objects.get(username=username) pwd = user.password uid = user.id firstname = user.firstname lastname = user.lastname email_id = user.email p = hashlib.md5(password) - pwd_valid = (pwd == p.hexdigest()) + pwd_valid = (pwd == p.hexdigest()) if user and pwd_valid: - try: - user = User.objects.get(username=username) - return user - except Exception, e: - user=User(id=uid, username=username, password=pwd,\ - first_name=firstname, last_name=lastname,\ - email=email_id) - user.save() - return user + try: + user = User.objects.get(username=username) + return user + except Exception, e: + user = User(id=uid, username=username, password=pwd, + first_name=firstname, last_name=lastname, + email=email_id) + user.save() + return user except Exception, e: return None -- cgit