Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
Agrega manejador de sesiones por correo electrónico
Browse files Browse the repository at this point in the history
  • Loading branch information
aitorres committed Nov 25, 2018
1 parent 5fb3922 commit 881871a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
31 changes: 31 additions & 0 deletions sip/authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# encoding=utf-8

"""
Módulo con funciones para complementar el manejo de sesiones
de Django.
"""

from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend

class EmailBackend(ModelBackend):
"""
Implementa un manejador de autenticación que permite iniciar
sesión con correo electrónico en vez de nombre de usuario.
"""

def authenticate(self, username=None, password=None, **kwargs):
"""
Verifica si existe un usuario con el correo electrónico PASADO
COMO 'username', en cuyo caso intenta iniciar sesión.
"""

UserModel = get_user_model()
try:
user = UserModel.objects.get(email=username)
except UserModel.DoesNotExist:
return None
else:
if user.check_password(password):
return user
return None
1 change: 1 addition & 0 deletions sip/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
},
]

AUTHENTICATION_BACKENDS = ['sip.authentication.EmailBackend']

# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/
Expand Down

0 comments on commit 881871a

Please sign in to comment.