2019-02-26 04:38:20 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-05-29 02:11:58 +00:00
|
|
|
from rest_framework import generics
|
2024-02-22 03:26:12 +00:00
|
|
|
from rest_framework.views import Response
|
2019-02-26 04:38:20 +00:00
|
|
|
|
2023-06-22 13:36:19 +00:00
|
|
|
from common.utils import get_logger
|
2023-05-25 08:26:35 +00:00
|
|
|
from users.models import User
|
|
|
|
from ..models import Setting
|
2024-02-22 03:26:12 +00:00
|
|
|
from ..serializers import LDAPUserSerializer
|
2023-05-25 08:26:35 +00:00
|
|
|
from ..utils import (
|
2024-02-22 03:26:12 +00:00
|
|
|
LDAPServerUtil, LDAPCacheUtil,
|
|
|
|
LDAP_USE_CACHE_FLAGS
|
2023-05-25 08:26:35 +00:00
|
|
|
)
|
2019-02-26 04:38:20 +00:00
|
|
|
|
2019-03-28 04:50:04 +00:00
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
|
2019-05-29 02:11:58 +00:00
|
|
|
class LDAPUserListApi(generics.ListAPIView):
|
2019-08-21 12:27:21 +00:00
|
|
|
serializer_class = LDAPUserSerializer
|
2022-03-10 09:59:05 +00:00
|
|
|
perm_model = Setting
|
2022-02-17 12:13:31 +00:00
|
|
|
rbac_perms = {
|
2022-03-10 09:59:05 +00:00
|
|
|
'list': 'settings.change_auth'
|
2022-02-17 12:13:31 +00:00
|
|
|
}
|
2019-03-22 07:55:46 +00:00
|
|
|
|
2019-11-11 08:41:32 +00:00
|
|
|
def get_queryset_from_cache(self):
|
|
|
|
search_value = self.request.query_params.get('search')
|
|
|
|
users = LDAPCacheUtil().search(search_value=search_value)
|
|
|
|
return users
|
|
|
|
|
|
|
|
def get_queryset_from_server(self):
|
|
|
|
search_value = self.request.query_params.get('search')
|
|
|
|
users = LDAPServerUtil().search(search_value=search_value)
|
|
|
|
return users
|
|
|
|
|
2019-05-29 02:11:58 +00:00
|
|
|
def get_queryset(self):
|
2019-09-18 14:06:46 +00:00
|
|
|
if hasattr(self, 'swagger_fake_view'):
|
2022-02-21 10:51:11 +00:00
|
|
|
return User.objects.none()
|
2019-11-11 08:41:32 +00:00
|
|
|
cache_police = self.request.query_params.get('cache_police', True)
|
|
|
|
if cache_police in LDAP_USE_CACHE_FLAGS:
|
|
|
|
users = self.get_queryset_from_cache()
|
|
|
|
else:
|
|
|
|
users = self.get_queryset_from_server()
|
2019-05-29 02:11:58 +00:00
|
|
|
return users
|
|
|
|
|
2019-11-11 09:45:39 +00:00
|
|
|
@staticmethod
|
|
|
|
def processing_queryset(queryset):
|
|
|
|
db_username_list = User.objects.all().values_list('username', flat=True)
|
|
|
|
for q in queryset:
|
|
|
|
q['id'] = q['username']
|
|
|
|
q['existing'] = q['username'] in db_username_list
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def sort_queryset(self, queryset):
|
|
|
|
order_by = self.request.query_params.get('order')
|
|
|
|
if not order_by:
|
|
|
|
order_by = 'existing'
|
|
|
|
if order_by.startswith('-'):
|
|
|
|
order_by = order_by.lstrip('-')
|
|
|
|
reverse = True
|
|
|
|
else:
|
|
|
|
reverse = False
|
|
|
|
queryset = sorted(queryset, key=lambda x: x[order_by], reverse=reverse)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
if queryset is None:
|
|
|
|
return queryset
|
|
|
|
queryset = self.processing_queryset(queryset)
|
|
|
|
queryset = self.sort_queryset(queryset)
|
|
|
|
return queryset
|
|
|
|
|
2019-11-11 08:41:32 +00:00
|
|
|
def list(self, request, *args, **kwargs):
|
|
|
|
cache_police = self.request.query_params.get('cache_police', True)
|
|
|
|
# 不是用缓存
|
|
|
|
if cache_police not in LDAP_USE_CACHE_FLAGS:
|
|
|
|
return super().list(request, *args, **kwargs)
|
|
|
|
|
2019-11-11 09:45:39 +00:00
|
|
|
try:
|
|
|
|
queryset = self.get_queryset()
|
|
|
|
except Exception as e:
|
|
|
|
data = {'error': str(e)}
|
|
|
|
return Response(data=data, status=400)
|
|
|
|
|
2019-11-11 08:41:32 +00:00
|
|
|
# 缓存有数据
|
|
|
|
if queryset is not None:
|
|
|
|
return super().list(request, *args, **kwargs)
|
2023-11-22 08:46:05 +00:00
|
|
|
else:
|
|
|
|
data = {'msg': _('Users are not synchronized, please click the user synchronization button')}
|
2019-11-11 08:41:32 +00:00
|
|
|
return Response(data=data, status=400)
|