diff options
author | Prabhu Ramachandran | 2014-06-06 17:28:57 +0530 |
---|---|---|
committer | Prabhu Ramachandran | 2014-06-06 17:28:57 +0530 |
commit | e50eb8426d5f22fd35a2575cd3f4617226bc1a01 (patch) | |
tree | af75e47255f39402c79ded6f75399a47084584ff /testapp/myauthentication/backend.py | |
parent | 8b5d13df2a8f4ac3acc075c522128722b987e57b (diff) | |
parent | 77caf5d44ab14d56efd4acbc65a6dd2d78c1c1a6 (diff) | |
download | online_test-e50eb8426d5f22fd35a2575cd3f4617226bc1a01.tar.gz online_test-e50eb8426d5f22fd35a2575cd3f4617226bc1a01.tar.bz2 online_test-e50eb8426d5f22fd35a2575cd3f4617226bc1a01.zip |
Merge pull request #24 from prathamesh920/spoken_tutorial
Spoken tutorial authentication
Diffstat (limited to 'testapp/myauthentication/backend.py')
-rw-r--r-- | testapp/myauthentication/backend.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/testapp/myauthentication/backend.py b/testapp/myauthentication/backend.py new file mode 100644 index 0000000..721fe54 --- /dev/null +++ b/testapp/myauthentication/backend.py @@ -0,0 +1,43 @@ +import hashlib +from django.contrib.auth.models import User, check_password +from models_spoken_tutorial import MoodleUser + + +class MyBackend: + supports_object_permissions = False + supports_anonymous_user = False + 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 details in django User table + and authenticates the user. + ''' + try: + 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()) + 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 + except Exception, e: + return None + + def get_user(self, user_id): + try: + return User.objects.get(pk=user_id) + except Exception, e: + return None |