使用两个用户表 [英] Work with two Users table

查看:152
本文介绍了使用两个用户表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以使用默认的 django.contrib.auth.models.User 存储管理员用户,使用 python创建的用户manage.py creationuperuser ,但使用另一个表来存储用户的注册,还将注册Social的用户( python-social-auth )。通过为用户使用另一个表,我将需要使用相同的密码哈希。



auth_users 不是一个选项。



谢谢。

解决方案

p>嗯,这是我这样做的:



python-social-auth docs 可以定义一个自定义模型:

  SOCIAL_AUTH_USER_MODEL ='foo.bar.User'

当我尝试'myapp.models.MyUserModel'时,我有一个错误,这必须是:'myapp。 MyUserModel'



这固定了 python-social-auth 注册。 p>

对于常见的表单寄存器,我只是在 MyUserModel 中创建了一个表单并创建了一个用户:

  class RegisterAction(FormView):

form_cl ass = RegisterForm

def form_valid(self,form):

MyUserModel.objects.create(
first_name = form.data.get('f_name'),
password = form.data.get('pwd'),
email = form.data.get('email'),
newsletter = form.data.get('newsletter')


返回超级(RegisterAction,self).form_valid(form)

def get_success_url(self):
return reverse('home')

您可以找到 FormView here



要修复自动化方法,我创建了一个自定义身份验证后端:

  from django.contrib.auth。导入check_password 

from myapp.models import MyUserModel

class MyAuthenticationBackend(object):

MODEL = MyUserModel

def authenticate(self,email,password):

如果电子邮件和密码匹配,返回一个用户(MODEL实例)

如果电子邮件和密码:
尝试:
user = self.MODEL.objects.get(email = email)
如果check_password(password = password,encoded = user.password):
返回用户
返回无
除了self.MODEL.DoesNotExist:
返回无
返回无

def get_user(self,user_id):
$
返回基于user_id的用户

try:
user = self.MODEL.objects.get(pk = user_id)
返回用户
除了self.MODEL.DoesNotExist:
return None
except self.MODEL.MultipleObjectsReturned:
return None

您可以找到身份验证结束文档此处以及如何编写自己的后端此处



然后您需要注册新的后端:

 #验证后端
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'myapp.backends.MyAuthenticationBackend',
'social.backends.facebook.FacebookOAuth2',
'social.backends.twitter.TwitterOAuth',
]

你会发现 AUTHENTICATION_BACKENDS 设置文档这里



现在我可以去一个shell:

  >>>来自django.contrib.auth import authenticate 
>>> authenticate(email='someuser@somemail.com',password ='123')
< MyUserModel:MyUserModel对象>
>>>

仍然可以使用创建用户python manage.py creationuperuser ,它们存储在默认的 auth_user 表中。


I wonder if is it possible to use the default django.contrib.auth.models.User to store admins users, users created with python manage.py createsuperuser, but use another table to store registered by form users, also users that will be registered with Social provides (python-social-auth). By using another table for users I will need to use the same password hashing also.

Use a 1-to-1 relationship with auth_users is not an option.

Thanks.

解决方案

Well, this is how I did this:

From python-social-auth docs is possible define a custom model:

SOCIAL_AUTH_USER_MODEL = 'foo.bar.User'

Here I got an error when I tried 'myapp.models.MyUserModel', this must be: 'myapp.MyUserModel'.

This fixed the python-social-auth register.

For a common form register I just did a form and create a user in MyUserModel:

class RegisterAction(FormView):

    form_class = RegisterForm

    def form_valid(self, form):

        MyUserModel.objects.create(
            first_name=form.data.get('f_name'),
            password=form.data.get('pwd'),
            email=form.data.get('email'),
            newsletter=form.data.get('newsletter')
        )

        return super(RegisterAction, self).form_valid(form)

    def get_success_url(self):
        return reverse('home')

You can find docs for FormView here.

To fix the autentication methods I created a custom authentication backend:

from django.contrib.auth.hashers import check_password

from myapp.models import MyUserModel

class MyAuthenticationBackend(object):

    MODEL = MyUserModel

    def authenticate(self, email, password):
        """
            Returns a User (MODEL instance) if email and password match
        """
        if email and password:
            try:
                user = self.MODEL.objects.get(email=email)
                if check_password(password=password, encoded=user.password):
                    return user
                return None
            except self.MODEL.DoesNotExist:
                return None
        return None

    def get_user(self, user_id):
        """
            Returns a User based on user_id
        """
        try:
            user = self.MODEL.objects.get(pk=user_id)
            return user
        except self.MODEL.DoesNotExist:
            return None
        except self.MODEL.MultipleObjectsReturned:
            return None

You can find authentication backends docs here and how write your own backend here.

Then you need to register your new backend:

# Authentication Backends
AUTHENTICATION_BACKENDS = [
    'django.contrib.auth.backends.ModelBackend',
    'myapp.backends.MyAuthenticationBackend',
    'social.backends.facebook.FacebookOAuth2',
    'social.backends.twitter.TwitterOAuth',
]

You will find AUTHENTICATION_BACKENDS settings docs here

Now I can go to a shell:

>>> from django.contrib.auth import authenticate
>>> authenticate(email='someuser@somemail.com', password='123')
<MyUserModel: MyUserModel object>
>>> 

And still can create user with python manage.py createsuperuser, which are stored in default auth_user table.

这篇关于使用两个用户表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆