diff options
author | prathamesh | 2014-03-26 14:48:45 +0530 |
---|---|---|
committer | prathamesh | 2014-03-26 14:48:45 +0530 |
commit | ba50e500a1da9266f9bee5bc38b69635637f6129 (patch) | |
tree | 4836211b8109824d9f8912e80f83be1e6f857ce0 /testapp/myauthentication/backend.py | |
parent | b2a95b4f99debc4d165bb0122b03b8e67f26b669 (diff) | |
download | online_test-ba50e500a1da9266f9bee5bc38b69635637f6129.tar.gz online_test-ba50e500a1da9266f9bee5bc38b69635637f6129.tar.bz2 online_test-ba50e500a1da9266f9bee5bc38b69635637f6129.zip |
User authentication using external database.
Django authentication backend overriden to authenticate user
using spoken tutorial database.
Added database router to handle the database operations.
Diffstat (limited to 'testapp/myauthentication/backend.py')
-rw-r--r-- | testapp/myauthentication/backend.py | 37 |
1 files changed, 37 insertions, 0 deletions
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 |