Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  1. from django.conf import settings
  2. from django.contrib.auth.models import User
  3.  
  4. class EmailOrUsernameModelBackend(object):
  5. """
  6. This is a ModelBacked that allows authentication with either a username or an email address.
  7.  
  8. """
  9. def authenticate(self, username=None, password=None):
  10. if '@' in username:
  11. kwargs = {'email': username}
  12. else:
  13. kwargs = {'username': username}
  14. try:
  15. user = User.objects.get(**kwargs)
  16. if user.check_password(password):
  17. return user
  18. except User.DoesNotExist:
  19. return None
  20.  
  21. def get_user(self, username):
  22. try:
  23. return User.objects.get(pk=username)
  24. except User.DoesNotExist:
  25. return None
  26.  
  27. from django.conf import settings
  28. from django.contrib.auth.models import User
  29. from django.contrib.auth.backends import ModelBackend
  30. class EmailOrUsernameModelBackend(ModelBackend):
  31. """
  32. This is a ModelBacked that allows authentication with either a username or an email address.
  33.  
  34. """
  35. def authenticate(self, username=None, password=None):
  36. if '@' in username:
  37. kwargs = {'email': username}
  38. else:
  39. kwargs = {'username': username}
  40. try:
  41. user = User.objects.get(**kwargs)
  42. if user.check_password(password):
  43. return user
  44. except User.DoesNotExist:
  45. return None
  46.  
  47. def get_user(self, username):
  48. try:
  49. return User.objects.get(pk=username)
  50. except User.DoesNotExist:
  51. return None
  52.  
  53. from django.conf import settings
  54. from django.contrib.auth import get_user_model
  55.  
  56. class EmailOrUsernameModelBackend(object):
  57. """
  58. This is a ModelBacked that allows authentication with either a username or an email address.
  59.  
  60. """
  61. def authenticate(self, username=None, password=None):
  62. if '@' in username:
  63. kwargs = {'email': username}
  64. else:
  65. kwargs = {'username': username}
  66. try:
  67. user = get_user_model().objects.get(**kwargs)
  68. if user.check_password(password):
  69. return user
  70. except User.DoesNotExist:
  71. return None
  72.  
  73. def get_user(self, username):
  74. try:
  75. return get_user_model().objects.get(pk=username)
  76. except get_user_model().DoesNotExist:
  77. return None
  78.  
  79. from django.contrib.auth.backends import ModelBackend
  80. from django.contrib.auth import get_user_model
  81. from django.db.models import Q
  82.  
  83.  
  84. class DualModelBackend(ModelBackend):
  85.  
  86. def authenticate(self, username=None, password=None, **kwargs):
  87. UserModel = get_user_model()
  88. if username is None:
  89. username = kwargs.get(UserModel.USERNAME_FIELD)
  90. # `username` field does not restring using `@`, so technically email can be
  91. # as username and email, even with different users
  92. users = UserModel._default_manager.filter(
  93. Q(**{UserModel.USERNAME_FIELD: username}) | Q(email__iexact=username))
  94. # check for any password match
  95. for user in users:
  96. if user.check_password(password):
  97. return user
  98. if not users:
  99. # Run the default password hasher once to reduce the timing
  100. # difference between an existing and a non-existing user (#20760).
  101. UserModel().set_password(password)
  102.  
  103. # -*- coding: utf-8 -*-
  104. from django.contrib.auth import backends, get_user_model
  105. from django.db.models import Q
  106.  
  107.  
  108. class ModelBackend(backends.ModelBackend):
  109. def authenticate(self, username=None, password=None, **kwargs):
  110. UserModel = get_user_model()
  111.  
  112. try:
  113. user = UserModel.objects.get(Q(username__iexact=username) | Q(email__iexact=username))
  114.  
  115. if user.check_password(password):
  116. return user
  117. except UserModel.DoesNotExist:
  118. # Run the default password hasher once to reduce the timing
  119. # difference between an existing and a non-existing user (#20760).
  120. UserModel().set_password(password)
  121.  
  122. from django.contrib.auth.backends import ModelBackend
  123. from django.contrib.auth import get_user_model
  124. from django.conf import settings
  125.  
  126. ###################################
  127. """ DEFAULT SETTINGS + ALIAS """
  128. ###################################
  129.  
  130.  
  131. try:
  132. am = settings.AUTHENTICATION_METHOD
  133. except:
  134. am = 'both'
  135. try:
  136. cs = settings.AUTHENTICATION_CASE_SENSITIVE
  137. except:
  138. cs = 'both'
  139.  
  140. #####################
  141. """ EXCEPTIONS """
  142. #####################
  143.  
  144.  
  145. VALID_AM = ['username', 'email', 'both']
  146. VALID_CS = ['username', 'email', 'both', 'none']
  147.  
  148. if (am not in VALID_AM):
  149. raise Exception("Invalid value for AUTHENTICATION_METHOD in project "
  150. "settings. Use 'username','email', or 'both'.")
  151.  
  152. if (cs not in VALID_CS):
  153. raise Exception("Invalid value for AUTHENTICATION_CASE_SENSITIVE in project "
  154. "settings. Use 'username','email', 'both' or 'none'.")
  155.  
  156. ############################
  157. """ OVERRIDDEN METHODS """
  158. ############################
  159.  
  160.  
  161. class DualAuthentication(ModelBackend):
  162. """
  163. This is a ModelBacked that allows authentication
  164. with either a username or an email address.
  165. """
  166.  
  167. def authenticate(self, username=None, password=None):
  168. UserModel = get_user_model()
  169. try:
  170. if ((am == 'email') or (am == 'both')):
  171. if ((cs == 'email') or cs == 'both'):
  172. kwargs = {'email': username}
  173. else:
  174. kwargs = {'email__iexact': username}
  175.  
  176. user = UserModel.objects.get(**kwargs)
  177. else:
  178. raise
  179. except:
  180. if ((am == 'username') or (am == 'both')):
  181. if ((cs == 'username') or cs == 'both'):
  182. kwargs = {'username': username}
  183. else:
  184. kwargs = {'username__iexact': username}
  185.  
  186. user = UserModel.objects.get(**kwargs)
  187. finally:
  188. try:
  189. if user.check_password(password):
  190. return user
  191. except:
  192. # Run the default password hasher once to reduce the timing
  193. # difference between an existing and a non-existing user.
  194. UserModel().set_password(password)
  195. return None
  196.  
  197. def get_user(self, username):
  198. UserModel = get_user_model()
  199. try:
  200. return UserModel.objects.get(pk=username)
  201. except UserModel.DoesNotExist:
  202. return None
  203.  
  204. from django.contrib.auth import authenticate, login
  205.  
  206. def my_view(request):
  207. username = request.POST['username']
  208. password = request.POST['password']
  209. user = authenticate(username=username, password=password)
  210. if user is not None:
  211. login(request, user)
  212. # Redirect to a success page.
  213. ...
  214. else:
  215. # Return an 'invalid login' error message.
  216. ...
  217.  
  218. from django.contrib.auth import authenticate, login, get_user_model
  219.  
  220. def my_view(request):
  221. username = request.POST['username']
  222. password = request.POST['password']
  223. user = authenticate(username=username, password=password)
  224. if user is None:
  225. User = get_user_model()
  226. user_queryset = User.objects.all().filter(email__iexact=username)
  227. if user_queryset:
  228. username = user_queryset[0].username
  229. user = authenticate(username=username, password=password)
  230. if user is not None:
  231. login(request, user)
  232. # Redirect to a success page.
  233. ...
  234. else:
  235. # Return an 'invalid login' error message.
  236. ...
  237.  
  238. if request.method == 'POST':
  239. form = LoginForm(request.POST)
  240. if form.is_valid():
  241. cd=form.cleaned_data
  242. if '@' in cd['username']:
  243. username=User.objects.get(email=cd['username']).username
  244. else:
  245. username=cd['username']
  246.  
  247. user = authenticate(username=username,
  248. password=cd['password'])
  249.  
  250. if user is not None and user.is_active:
  251. login(request,user)
  252. return redirect('loggedin')
  253. else:
  254. return render(request, 'login.html')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement