Guest User

Untitled

a guest
Sep 21st, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. --------------setttings.py--------------
  2. AUTHENTICATION_BACKENDS = (
  3. 'mycoach2.backends.SettingsBackend',
  4. )
  5.  
  6. -----------mycoach2/backends.py-----------
  7.  
  8.  
  9. from django.conf import settings
  10. from django.contrib.auth.models import User, check_password
  11.  
  12. class SettingsBackend(object):
  13. """
  14. Authenticate against the settings ADMIN_LOGIN and ADMIN_PASSWORD.
  15.  
  16. Use the login name, and a hash of the password. For example:
  17.  
  18. ADMIN_LOGIN = 'admin'
  19. ADMIN_PASSWORD = 'sha1$4e987$afbcf42e21bd417fb71db8c66b321e9fc33051de'
  20. """
  21.  
  22. def authenticate(self, username=None, password=None):
  23. try:
  24. user = User.objects.get(username=username)
  25. return user
  26. except:
  27. return None
  28. """
  29. login_valid = (settings.ADMIN_LOGIN == username)
  30. pwd_valid = check_password(password, settings.ADMIN_PASSWORD)
  31. if login_valid and pwd_valid:
  32. try:
  33. user = User.objects.get(username=username)
  34. except User.DoesNotExist:
  35. # Create a new user. Note that we can set password
  36. # to anything, because it won't be checked; the password
  37. # from settings.py will.
  38. user = User(username=username, password='get from settings.py')
  39. user.is_staff = True
  40. user.is_superuser = True
  41. user.save()
  42. return user
  43. return None
  44. """
  45.  
  46. def get_user(self, user_id):
  47. try:
  48. return User.objects.get(pk=user_id)
  49. except User.DoesNotExist:
  50. return None
  51.  
  52. def has_perm(self, user_obj, perm, obj=None):
  53. return true
  54. if user_obj.username == settings.ADMIN_LOGIN:
  55. return True
  56. else:
  57. return False
Add Comment
Please, Sign In to add comment