Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.conf import settings
- from django.contrib.auth.models import User
- class EmailOrUsernameModelBackend(object):
- """
- This is a ModelBacked that allows authentication with either a username or an email address.
- """
- def authenticate(self, username=None, password=None):
- if '@' in username:
- kwargs = {'email': username}
- else:
- kwargs = {'username': username}
- try:
- user = User.objects.get(**kwargs)
- if user.check_password(password):
- return user
- except User.DoesNotExist:
- return None
- def get_user(self, username):
- try:
- return User.objects.get(pk=username)
- except User.DoesNotExist:
- return None
- from django.conf import settings
- from django.contrib.auth.models import User
- from django.contrib.auth.backends import ModelBackend
- class EmailOrUsernameModelBackend(ModelBackend):
- """
- This is a ModelBacked that allows authentication with either a username or an email address.
- """
- def authenticate(self, username=None, password=None):
- if '@' in username:
- kwargs = {'email': username}
- else:
- kwargs = {'username': username}
- try:
- user = User.objects.get(**kwargs)
- if user.check_password(password):
- return user
- except User.DoesNotExist:
- return None
- def get_user(self, username):
- try:
- return User.objects.get(pk=username)
- except User.DoesNotExist:
- return None
- from django.conf import settings
- from django.contrib.auth import get_user_model
- class EmailOrUsernameModelBackend(object):
- """
- This is a ModelBacked that allows authentication with either a username or an email address.
- """
- def authenticate(self, username=None, password=None):
- if '@' in username:
- kwargs = {'email': username}
- else:
- kwargs = {'username': username}
- try:
- user = get_user_model().objects.get(**kwargs)
- if user.check_password(password):
- return user
- except User.DoesNotExist:
- return None
- def get_user(self, username):
- try:
- return get_user_model().objects.get(pk=username)
- except get_user_model().DoesNotExist:
- return None
- from django.contrib.auth.backends import ModelBackend
- from django.contrib.auth import get_user_model
- from django.db.models import Q
- class DualModelBackend(ModelBackend):
- def authenticate(self, username=None, password=None, **kwargs):
- UserModel = get_user_model()
- if username is None:
- username = kwargs.get(UserModel.USERNAME_FIELD)
- # `username` field does not restring using `@`, so technically email can be
- # as username and email, even with different users
- users = UserModel._default_manager.filter(
- Q(**{UserModel.USERNAME_FIELD: username}) | Q(email__iexact=username))
- # check for any password match
- for user in users:
- if user.check_password(password):
- return user
- if not users:
- # Run the default password hasher once to reduce the timing
- # difference between an existing and a non-existing user (#20760).
- UserModel().set_password(password)
- # -*- coding: utf-8 -*-
- from django.contrib.auth import backends, get_user_model
- from django.db.models import Q
- class ModelBackend(backends.ModelBackend):
- def authenticate(self, username=None, password=None, **kwargs):
- UserModel = get_user_model()
- try:
- user = UserModel.objects.get(Q(username__iexact=username) | Q(email__iexact=username))
- if user.check_password(password):
- return user
- except UserModel.DoesNotExist:
- # Run the default password hasher once to reduce the timing
- # difference between an existing and a non-existing user (#20760).
- UserModel().set_password(password)
- from django.contrib.auth.backends import ModelBackend
- from django.contrib.auth import get_user_model
- from django.conf import settings
- ###################################
- """ DEFAULT SETTINGS + ALIAS """
- ###################################
- try:
- am = settings.AUTHENTICATION_METHOD
- except:
- am = 'both'
- try:
- cs = settings.AUTHENTICATION_CASE_SENSITIVE
- except:
- cs = 'both'
- #####################
- """ EXCEPTIONS """
- #####################
- VALID_AM = ['username', 'email', 'both']
- VALID_CS = ['username', 'email', 'both', 'none']
- if (am not in VALID_AM):
- raise Exception("Invalid value for AUTHENTICATION_METHOD in project "
- "settings. Use 'username','email', or 'both'.")
- if (cs not in VALID_CS):
- raise Exception("Invalid value for AUTHENTICATION_CASE_SENSITIVE in project "
- "settings. Use 'username','email', 'both' or 'none'.")
- ############################
- """ OVERRIDDEN METHODS """
- ############################
- class DualAuthentication(ModelBackend):
- """
- This is a ModelBacked that allows authentication
- with either a username or an email address.
- """
- def authenticate(self, username=None, password=None):
- UserModel = get_user_model()
- try:
- if ((am == 'email') or (am == 'both')):
- if ((cs == 'email') or cs == 'both'):
- kwargs = {'email': username}
- else:
- kwargs = {'email__iexact': username}
- user = UserModel.objects.get(**kwargs)
- else:
- raise
- except:
- if ((am == 'username') or (am == 'both')):
- if ((cs == 'username') or cs == 'both'):
- kwargs = {'username': username}
- else:
- kwargs = {'username__iexact': username}
- user = UserModel.objects.get(**kwargs)
- finally:
- try:
- if user.check_password(password):
- return user
- except:
- # Run the default password hasher once to reduce the timing
- # difference between an existing and a non-existing user.
- UserModel().set_password(password)
- return None
- def get_user(self, username):
- UserModel = get_user_model()
- try:
- return UserModel.objects.get(pk=username)
- except UserModel.DoesNotExist:
- return None
- from django.contrib.auth import authenticate, login
- def my_view(request):
- username = request.POST['username']
- password = request.POST['password']
- user = authenticate(username=username, password=password)
- if user is not None:
- login(request, user)
- # Redirect to a success page.
- ...
- else:
- # Return an 'invalid login' error message.
- ...
- from django.contrib.auth import authenticate, login, get_user_model
- def my_view(request):
- username = request.POST['username']
- password = request.POST['password']
- user = authenticate(username=username, password=password)
- if user is None:
- User = get_user_model()
- user_queryset = User.objects.all().filter(email__iexact=username)
- if user_queryset:
- username = user_queryset[0].username
- user = authenticate(username=username, password=password)
- if user is not None:
- login(request, user)
- # Redirect to a success page.
- ...
- else:
- # Return an 'invalid login' error message.
- ...
- if request.method == 'POST':
- form = LoginForm(request.POST)
- if form.is_valid():
- cd=form.cleaned_data
- if '@' in cd['username']:
- username=User.objects.get(email=cd['username']).username
- else:
- username=cd['username']
- user = authenticate(username=username,
- password=cd['password'])
- if user is not None and user.is_active:
- login(request,user)
- return redirect('loggedin')
- else:
- return render(request, 'login.html')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement