Guest User

Untitled

a guest
Dec 6th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. from django.conf import settings
  2. from django.contrib.auth.models import User
  3. from django import forms
  4.  
  5. def my_portal_authenticate(username, password):
  6. if username == 'fooDjango'
  7. and password == 'barDjango':
  8. return True
  9. return False
  10.  
  11. class MyPortalBackend(object):
  12. def authenticate(self, **kwargs):
  13. '''
  14. kwargs will receive the python dict that may contain
  15. username & password to authenticate which will be
  16. received from the Custom admin site.
  17. '''
  18. try:
  19. username = kwargs['username']
  20. password = kwgars['password']
  21.  
  22. if not my_portal_authenticate(username, password):
  23. raise forms.ValidationError(
  24. _("Username / Password Mismatch")
  25. )
  26.  
  27. '''
  28. Check if the user exist in the django_auth_user
  29. table, if not then UserNotExist exception will
  30. be raised automatically and user will be added
  31. (with or without password) in the exception
  32. handling block
  33. '''
  34.  
  35. #Check if the user exist in the database, if it exist in the
  36. #database, auth_user will not be updated and exception will not be raised
  37. user = User.objects.get(username = username)
  38.  
  39. except KeyError, e:
  40. raise forms.ValidationError(
  41. _("Programming Error")
  42. )
  43.  
  44. except User.DoesNotExist:
  45. '''
  46. Add the username to the django_auth_users so
  47. that login session can keep track of it.
  48. Django Admin is heavily coupled with the
  49. Django User model for the user instane in the
  50. django_auth_users table. The request object then
  51. map the request.user feild to this object of the
  52. data model.
  53. '''
  54. user = User(username=username)
  55. #defining the user of access group of that particular user
  56. user.is_staff= True
  57. user.is_superuser = True
  58. suer.save()
  59.  
  60. return user
  61.  
  62. def get_user(self, user_id):
  63. try:
  64. return User.objects.get(pk=user_id)
  65. except User.DoesNotExist:
  66. #Djano Admin treats None user as anonymous your who have no right at all.
  67. return None
Add Comment
Please, Sign In to add comment