groups 'admin and help'

My code:

from django.contrib.auth.mixins import UserPassesTestMixin
from django.http import JsonResponse

class aiutanteRequiredMixin(UserPassesTestMixin):
    def test_func(self):
        user = self.request.user
        return user.groups.filter(name="aiutante").exists()

class adminRequiredMixin(UserPassesTestMixin):
    def test_func(self):
        user = self.request.user
        return user.groups.filter(name="admin").exists()
    def handle_no_permission(self):
        return JsonResponse(
            {'message': 'Only company administrators have access to this view'}
class GestioneUtenti(aiutanteRequiredMixin, adminRequiredMixin,ListView):
    model = User
    group_required = [u"aiutante", u"admin"]
    template_name = 'users_list.html'

Not working … how to use group level ??

My model:

from django.db import models
from django.utils.translation import gettext_lazy as _
from core.utils import *
from django.contrib.auth.models import AbstractUser
from .managers import UserProfileManager
from django.utils import timezone
from stdimage import StdImageField
from django.contrib.auth.models import Group

    (2, "Donna"),

class User(AbstractUser):

    objects = UserProfileManager()
    id = models.AutoField(primary_key=True)
    # cognome
    last_name = models.CharField(_('Last name'), null=True, blank=True, max_length=30)
    # nome
    first_name = models.CharField(_('First name'), null=True, blank=True, max_length=30)
    state = models.CharField(_('State'), null=True, blank=True, max_length=255)
    country = models.CharField(_('Country'), null=True, blank=True, max_length=255)
    home_address = models.CharField(_('Home address'), null=True, blank=True, max_length=255)
    postal_code = models.CharField(_('Postal code'), null=True, blank=True, max_length=255)
    city = models.CharField(_('City'), null=True, blank=True, max_length=255)
    email = models.EmailField(_('Email'), unique=True, error_messages={'unique':"Email già in uso."})
    username = models.CharField(_('Username'), max_length=100, unique=True, null=True, default=None)
    display_name = models.PositiveIntegerField(_('Display name'), null=True, default=None)
    groups = models.ManyToManyField(Group)
    sex = models.IntegerField(_('Sex'), choices=Sex_CHOICES, default=0)
    birth_date=models.DateField(_('Birth date'), null=True, blank=True,)
    information = models.TextField(_('Information'), blank=True, null=True)
    image = StdImageField(_('Photo'), upload_to=PathRename('profile'), render_variations= resize_and_autorotate,  blank=True ,  variations={
         'avatar': (120, 120, True),
    }, delete_orphans=True)
    is_superuser = models.BooleanField( blank=True, null=True)
    is_staff = models.BooleanField( blank=True, null=True)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(_('Date of registration'),

    USERNAME_FIELD = "email"

    REQUIRED_FIELDS = ['username','first_name', 'last_name']

    def __str__(self):
        if self.display_name == 1:
            return self.first_name + ' ' + self.last_name
        elif self.display_name == 2:
            return self.last_name + ' ' +  self.first_name
        elif self.display_name == 3:
            return self.username
            return self.first_name + ' ' +  self.last_name

    def save(self, *args, **kwargs):
        super(User, self).save(*args, **kwargs)

groups = models.ManyToManyField(Group)

idea?. Thanks.

What’s the issue?

More specifically, it’s not clear to me what you’re saying isn’t working, what you’re expecting to see, and how that differs from what you’ve posted here.


admin = All Access
aiutante (Editors) = Edit/View/add
user = normal user

How to use the groups permessions django authentication ?


It’s still not clear what you’re asking for here.

However, based upon your example, I suggest you read the docs for the UserPassesTestMixin, particularly the green box in that section titled Stacking UserPassesTestMixin. (Briefly, you can’t use two instances of the mixin class on a single view.)

I will also point out here that while it is valid to test group membership for your authorization tests, that’s not really the best way to do this.

You want a permission to be assigned to that view, and then you test for the user being granted that permission. The purpose of the Group is to allow you to assign multiple permissions to the Group, and then assign Users to those groups.