瀏覽代碼

Allow authentication with both Username and Email

tags/v1.0.0
Krzysztof Klimonda 11 年之前
父節點
當前提交
92b54799da
共有 3 個檔案被更改,包括 27 行新增0 行删除
  1. +0
    -0
      pinry/core/auth/__init__.py
  2. +25
    -0
      pinry/core/auth/backends.py
  3. +2
    -0
      pinry/settings/__init__.py

+ 0
- 0
pinry/core/auth/__init__.py 查看文件


+ 25
- 0
pinry/core/auth/backends.py 查看文件

@@ -0,0 +1,25 @@
from django.core.validators import email_re

from pinry.core.models import User

class CombinedAuthBackend(object):
def authenticate(self, username=None, password=None):
is_email = email_re.match(username)
if is_email:
qs = User.objects.filter(email=username)
else:
qs = User.objects.filter(username=username)

try:
user = qs.get()
except User.DoesNotExist:
return None
if user.check_password(password):
return user
return None

def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None

+ 2
- 0
pinry/settings/__init__.py 查看文件

@@ -79,3 +79,5 @@ INSTALLED_APPS = (
'pinry.pins',
'pinry.api',
)

AUTHENTICATION_BACKENDS = ('pinry.core.auth.backends.CombinedAuthBackend', 'django.contrib.auth.backends.ModelBackend',)

Loading…
取消
儲存