forked from DefectDojo/django-DefectDojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.py
57 lines (37 loc) · 1.9 KB
/
queries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from crum import get_current_user
from django.db.models import Exists, OuterRef
from dojo.models import Dojo_Group, Dojo_Group_Member, Product_Group, Product_Type_Group, Role
from dojo.authorization.authorization import get_roles_for_permission
from dojo.authorization.roles_permissions import Permissions
def get_authorized_groups(permission):
user = get_current_user()
if user is None:
return Dojo_Group.objects.none()
if user.is_superuser:
return Dojo_Group.objects.all().order_by('name')
roles = get_roles_for_permission(permission)
authorized_roles = Dojo_Group_Member.objects.filter(group=OuterRef('pk'),
user=user,
role__in=roles)
groups = Dojo_Group.objects.annotate(user=Exists(authorized_roles)).order_by('name')
return groups.filter(user=True)
def get_authorized_group_members(permission):
user = get_current_user()
if user is None:
return Dojo_Group_Member.objects.none()
if user.is_superuser:
return Dojo_Group_Member.objects.all().select_related('role')
groups = get_authorized_groups(permission)
return Dojo_Group_Member.objects.filter(group__in=groups).select_related('role')
def get_authorized_group_members_for_user(user):
groups = get_authorized_groups(Permissions.Group_View)
group_members = Dojo_Group_Member.objects.filter(user=user, group__in=groups).order_by('group__name').select_related('role', 'group')
return group_members
def get_group_members_for_group(group):
return Dojo_Group_Member.objects.filter(group=group).select_related('role')
def get_product_groups_for_group(group):
return Product_Group.objects.filter(group=group).select_related('role')
def get_product_type_groups_for_group(group):
return Product_Type_Group.objects.filter(group=group).select_related('role')
def get_group_member_roles():
return Role.objects.exclude(name='API_Importer').exclude(name='Writer')