App Django per impostazioni utente / account
-
15-11-2019 - |
Domanda
IM PREPPING PER SVILUPPO UN PROGETTO E AM IN PROCESSO DI MAPPING OUT DOCCI I POSSONO UTILIZZARE PER IT. Questo progetto avrà bisogno di diversi modelli di gruppo / utente e autorizzazioni e impostazioni di oggetti.
So che posso usare Django-ObjecterPermissions (https://github.com/washingtontimes/django-obieppermissioni) e le autorizzazioni del modello di Djangos. Ma ci sono alcuni casi, quando non hai davvero bisogno di impostare le autorizzazioni di oggetti / modello, ma piuttosto un'impostazione - può questo account / utente fare qualcosa, che non è correlato a determinati oggetti o modelli. Qualcuno ha sviluppato un'app per questo? O che tipo di approccio prende le persone quando hanno bisogno di tale bisogno? Forse creare il dummy django modello e lasciare che i permessi del modello Djangos gestiscano il resto?
Lo so anche, che questo fa qualcosa di simile - https://github.com/danielroseman/django- dbsettings . Ma quando ho scremato attraverso il codice ho avuto impressione, che quelle impostazioni sono tutte basate sugli utenti - ho bisogno anche di impostazioni basate su gruppo.
Modifica: Le persone continuano ad offrire a me permissioni di app. Quello che sto cercando non è l'app per le autorizzazioni, ma le impostazioni delle impostazioni. Quelle impostazioni / autorizzazioni non sono correlate a nessun oggetto.
fondamentalmente. Nel mio progetto ho bisogno di rispondere alle domande: questo utente può vedere / fare questa cosa? Quella "cosa" è molto probabilmente una vista. Quindi una delle risposte quasi funziona. Ma il controllo che faccio in vista, non è se l'utente ha il permesso a qualche modello / oggetto. È piuttosto, se l'utente ha acceso questa impostazione, se il gruppo di utenti ha acceso questa impostazione.
Alan
Soluzione
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.
Altri suggerimenti
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