Add basic superuser permission checks for admin

This commit is contained in:
Tobias Kunze 2025-03-16 09:29:34 +01:00
parent 4dd7d8628d
commit 1664e9409c

View file

@ -61,3 +61,31 @@ class User(AbstractBaseUser):
def normalize_username(self, username):
return super().normalize_username(username).strip().lower()
def has_perm(self, perm, obj=None):
"""
Return True if the user has the specified permission.
Superusers automatically have all permissions.
"""
return self.is_superuser
def has_module_perms(self, app_label):
"""
Return True if the user has any permissions in the given app label.
Superusers automatically have all permissions.
"""
return self.is_superuser
def get_all_permissions(self, obj=None):
"""
Return a set of permission strings that the user has.
Superusers have all permissions.
"""
if self.is_superuser:
from django.contrib.auth.models import Permission
return {
f"{perm.content_type.app_label}.{perm.codename}"
for perm in Permission.objects.all()
}
return set()