forked from DGNum/gestioCOF
Disambiguation in kfet's permission handling
In some places we used to refer to permissions based on their codename only (the part after the dot "." in the following examples) which can be ambiguous. Typically, we might define permissions like "bds.is_team" or "cof.is_team" in the near future ;)
This commit is contained in:
parent
67e28c704f
commit
64c792b11f
5 changed files with 37 additions and 25 deletions
|
@ -3,6 +3,7 @@ import heapq
|
|||
import statistics
|
||||
from collections import defaultdict
|
||||
from decimal import Decimal
|
||||
from typing import List
|
||||
from urllib.parse import urlencode
|
||||
|
||||
from django.contrib import messages
|
||||
|
@ -993,15 +994,19 @@ def kpsul_update_addcost(request):
|
|||
return JsonResponse(data)
|
||||
|
||||
|
||||
def get_missing_perms(required_perms, user):
|
||||
missing_perms_codenames = [
|
||||
(perm.split("."))[1] for perm in required_perms if not user.has_perm(perm)
|
||||
]
|
||||
missing_perms = list(
|
||||
Permission.objects.filter(codename__in=missing_perms_codenames).values_list(
|
||||
"name", flat=True
|
||||
def get_missing_perms(required_perms: List[str], user: User) -> List[str]:
|
||||
def get_perm_description(app_label: str, codename: str) -> str:
|
||||
name = Permission.objects.values_list("name", flat=True).get(
|
||||
codename=codename, content_type__app_label=app_label
|
||||
)
|
||||
)
|
||||
return "[{}] {}".format(app_label, name)
|
||||
|
||||
missing_perms = [
|
||||
get_perm_description(*perm.split("."))
|
||||
for perm in required_perms
|
||||
if not user.has_perm(perm)
|
||||
]
|
||||
|
||||
return missing_perms
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue