문제

IM은 하나의 프로젝트를 개발하고 내가 사용할 수있는 것들을 매핑하는 과정에서 AM을 준비합니다. 이 프로젝트는 다른 그룹 / 사용자 기반 모델 및 객체 사용 권한 및 설정이 필요합니다.

django-objectpermissions (https://github.com/washingtontimes/django-objectpermissions) 및 Djangos 자신의 모델 권한을 사용할 수 있습니다. 그러나 오브젝트 / 모델 권한을 정말로 설정할 필요가 없지만,이 계정 / 사용자가 특정 객체 또는 모델과 관련이없는 무언가를 수행 할 수있는 경우가 있습니다. 누군가가 앱을 개발 했습니까? 또는 그런 필요가있을 때 사람들이 어떤 종류의 접근 방식을 취합니까? 아마도 더미 Django 모델을 만들고 Djangos 모델 권한이 나머지를 처리하도록하도록하십시오.

나는 또한 이것이 비슷한 것을 알고 있습니다 - https://github.com/danielroseman/django- Dbsettings . 그러나 내가 코드를 훑어 보았을 때 나는 그 설정이 모든 사용자 기반이며, 그룹 기반 설정도 필요합니다.

편집 : 사람들은 저에게 사용 권한 앱을 계속 제공합니다. 내가 찾고있는 것은 권한 앱이 아니라 설정 앱이 아닙니다. 이러한 설정 / 사용 권한은 모든 개체와 관련이 없습니다.

기본적으로. 내 프로젝트에서는 질문에 답변해야합니다.이 사용자는이 작업을보고 할 수 있습니까? 그 "물건"은 가장 많이 가능성이 높습니다. 그래서 답변 중 하나가 거의 작동합니다. 그러나 내가 볼 수있는 수표는 사용자가 일부 모델 / 객체에 대한 권한이있는 경우가 아닙니다. 사용자 가이 설정이 켜져있는 경우 사용자 가이 설정을 켜면이 설정이 켜져있는 경우.

alan

도움이 되었습니까?

해결책

You're probably going to need to create your own system for this. It shouldn't be very difficult.

class Setting(models.Model):
    name = models.CharField(..)
    value = models.CharField(..)
    users = models.ManyToManyField(User)
    groups = models.ManyToManyField(Group)

    @classmethod
    def has_setting(cls, name, user):
        user_settings = cls.objects.filter(name=name, users__in=[user]).count()
        group_settings = cls.objects.filter(name=name, groups__in=user.groups.all()).count()
        return user_settings or group_settings

    @classmethod
    def get_settings(cls, name, user, priority=User):
        user_settings = list(cls.objects.filter(name=name, users__in=[user]))
        group_settings = list(cls.objects.filter(name=name, groups__in=user.groups.all()))
        if user_settings and not group_settings: 
            return user_settings
        elif group_settings and not user_settings:
            return group_settings
        else:
            return (group_settings, user_settings)[priority==User]

Then you can do something like this in your views:

def display_frob(request):
    if Setting.has_setting('display_frob', request.user):
         settings = Setting.get_setting('display_from', request.user, priority=Group)
         values = [setting.value for setting in settings]
         # if the User was in many groups, we now have a list of values, for the setting
         # `display_frob`, that aligns with each of those groups

You can easy build a decorator which will do the check, and provide a list (or a single item) of values to the view.

다른 팁

For permissions for "actions" (where an action is typically implemented by a view) I commonly use decorators.

The user_passes_test decorator is great for this kind of purpose.

You could create user permissions not linked to models.

Maybe this is what you looking for : django-guardian

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top