如何在多个项目中使用自定义用户模型和对象权限(EX / django-guardian)实现Django SSO

时间:2019-04-12 10:05:27

标签: python django django-guardian

我试图在Google上搜索和研究一些信息,但找不到合适的解决方案。

我有几个Django项目,并且想要创建实现“单点登录”(SSO)功能(ex /“ django-simple-sso”)的“成员”项目存储用户信息。然后我需要对用户权限进行分类,研究软件包“ django-guardian”,它可能可以帮助我进行对象权限。

我下面要做的事情是在“成员”项目中创建自定义模型。然后,我只发布我添加/更改的代码。

Project A

我在名为“ Member”的项目中添加了一个调用用户的应用。

Solution A
Project A
Solution C
# setting.py of "Member" project

INSTALLED_APPS = [    
    'users',
    'guardian'
]

AUTHENTICATION_BACKENDS = (
    'guardian.backends.ObjectPermissionBackend',
)
# admin.py in the users app of Member project.

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = ('email', 'is_staff', 'is_active', 'employee_account', 'location', 'chinese_name', 'english_name', 'extension', 'last_login', 'date_joined')
    list_filter = ('email', 'is_staff', 'is_active', 'employee_account', 'location', 'chinese_name', 'english_name', 'extension', 'last_login', 'date_joined')
    fieldsets = (
        (None, {'fields': ('employee_account', 'password')}),
        ('Permissions', {'fields': ('is_staff', 'is_active')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('employee_account', 'password1', 'password2', 'is_staff', 'is_active')}
        ),
    )
    search_fields = ('employee_account',)
    ordering = ('employee_account',)

admin.site.register(CustomUser, CustomUserAdmin)

然后我使用django shell添加用户并分配权限。

# apps.py in the users app of Member project.  

from django.apps import AppConfig

class UsersConfig(AppConfig):
    name = 'users'

之后,我在另一个名为book_web的项目中创建了一个图书应用。

# forms.py in the users app of Member project.

from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser

class CustomUserCreationForm(UserCreationForm):

    class Meta(UserCreationForm):
        model = CustomUser
        fields = ('employee_account',)


class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = ('employee_account',)

1。按下此命令# manager.py in the users app of Member project. from django.contrib.auth.base_user import BaseUserManager from django.utils.translation import ugettext_lazy as _ class CustomUserManager(BaseUserManager): """ Custom user model manager where email is the unique identifiers for authentication instead of usernames. """ def create_user(self, email, password, **extra_fields): """ Create and save a User with the given email and password. """ if not email: raise ValueError(_('The Email must be set')) email = self.normalize_email(email) user = self.model(email=email, **extra_fields) user.set_password(password) user.save() return user def create_superuser(self, email, password, **extra_fields): """ Create and save a SuperUser with the given email and password. """ extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) extra_fields.setdefault('is_active', True) if extra_fields.get('is_staff') is not True: raise ValueError(_('Superuser must have is_staff=True.')) if extra_fields.get('is_superuser') is not True: raise ValueError(_('Superuser must have is_superuser=True.')) return self.create_user(email, password, **extra_fields) 时,似乎工作异常。我发现响应未显示与django-guardian示例代码不同的用户名。

2。我不知道如何在Member项目(例如django-guardian示例代码)中分配Book对象的权限。因为在成员项目中没有与Book模型相同的模型。

有人对此有类似的经验吗?

如果您有另一个推荐套件来实现该功能,欢迎分享您的经验。感谢您的回复。

0 个答案:

没有答案