jumpserver/apps/users/filters.py

33 lines
1.1 KiB
Python
Raw Normal View History

2020-07-06 03:14:20 +00:00
from rest_framework.compat import coreapi, coreschema
from rest_framework import filters
from users.models.user import User
2020-07-06 03:14:20 +00:00
from orgs.utils import current_org
class OrgRoleUserFilterBackend(filters.BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
org_role = request.query_params.get('org_role')
if not org_role:
return queryset
if org_role == 'admins':
2020-07-28 09:53:01 +00:00
return queryset & (current_org.admins | User.objects.filter(role=User.ROLE.ADMIN))
2020-07-06 03:14:20 +00:00
elif org_role == 'auditors':
2020-07-20 02:42:22 +00:00
return queryset & current_org.auditors
2020-07-06 03:14:20 +00:00
elif org_role == 'users':
2020-07-20 02:42:22 +00:00
return queryset & current_org.users
2020-07-06 03:14:20 +00:00
elif org_role == 'members':
2020-07-20 02:42:22 +00:00
return queryset & current_org.get_members()
2020-07-06 03:14:20 +00:00
def get_schema_fields(self, view):
return [
coreapi.Field(
name='org_role', location='query', required=False, type='string',
schema=coreschema.String(
title='Organization role users',
description='Organization role users can be {admins|auditors|users|members}'
)
)
]