From 2ea3ad4ca5d1b4d55d0a7cc1a276d60057980113 Mon Sep 17 00:00:00 2001 From: BaiJiangJie Date: Mon, 11 Nov 2019 17:45:39 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E9=87=8D=E6=9E=84=20LDAP/AD=20?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=9C=BA=E5=88=B6=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/settings/api.py | 80 +++++++++++-------- .../settings/_ldap_list_users_modal.html | 8 +- apps/settings/utils/ldap.py | 5 +- 3 files changed, 59 insertions(+), 34 deletions(-) diff --git a/apps/settings/api.py b/apps/settings/api.py index c226c9205..f5f6ddfb2 100644 --- a/apps/settings/api.py +++ b/apps/settings/api.py @@ -137,35 +137,6 @@ class LDAPUserListApi(generics.ListAPIView): users = self.get_queryset_from_server() return users - 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) - - queryset = self.get_queryset() - # 缓存有数据 - if queryset is not None: - return super().list(request, *args, **kwargs) - - sync_util = LDAPSyncUtil() - # 还没有同步任务 - if sync_util.task_no_start: - task = sync_ldap_user_task.delay() - data = {'msg': 'Cache no data, sync task {} started.'.format(task.id)} - return Response(data=data, status=409) - # 同步任务正在执行 - if sync_util.task_is_running: - data = {'msg': 'synchronization is running.'} - return Response(data=data, status=409) - # 同步任务执行结束 - if sync_util.task_is_over: - msg = sync_util.get_task_error_msg() - data = {'msg': 'Synchronization task report error: {}'.format(msg)} - return Response(data=data, status=400) - - return super().list(request, *args, **kwargs) - @staticmethod def processing_queryset(queryset): db_username_list = User.objects.all().values_list('username', flat=True) @@ -187,10 +158,46 @@ class LDAPUserListApi(generics.ListAPIView): 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 + 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) + + try: + queryset = self.get_queryset() + except Exception as e: + data = {'error': str(e)} + return Response(data=data, status=400) + + # 缓存有数据 + if queryset is not None: + return super().list(request, *args, **kwargs) + + sync_util = LDAPSyncUtil() + # 还没有同步任务 + if sync_util.task_no_start: + task = sync_ldap_user_task.delay() + data = {'msg': 'Cache no data, sync task {} started.'.format(task.id)} + return Response(data=data, status=409) + # 同步任务正在执行 + if sync_util.task_is_running: + data = {'msg': 'synchronization is running.'} + return Response(data=data, status=409) + # 同步任务执行结束 + if sync_util.task_is_over: + msg = sync_util.get_task_error_msg() + data = {'error': 'Synchronization task report error: {}'.format(msg)} + return Response(data=data, status=400) + + return super().list(request, *args, **kwargs) + class LDAPUserImportAPI(APIView): permission_classes = (IsOrgAdmin,) @@ -205,11 +212,20 @@ class LDAPUserImportAPI(APIView): return users def post(self, request): - users = self.get_ldap_users() + try: + users = self.get_ldap_users() + except Exception as e: + return Response({'error': str(e)}, status=401) + + if users is None: + return Response({'msg': 'Get ldap users is None'}, status=401) + errors = LDAPImportUtil().perform_import(users) if errors: - return Response({'Error': errors}, status=401) - return Response({'msg': 'Imported {} users successfully'.format(len(users))}) + return Response({'errors': errors}, status=401) + + count = users if users is None else len(users) + return Response({'msg': 'Imported {} users successfully'.format(count)}) class LDAPCacheRefreshAPI(generics.RetrieveAPIView): diff --git a/apps/settings/templates/settings/_ldap_list_users_modal.html b/apps/settings/templates/settings/_ldap_list_users_modal.html index cc63f72c5..1b2597924 100644 --- a/apps/settings/templates/settings/_ldap_list_users_modal.html +++ b/apps/settings/templates/settings/_ldap_list_users_modal.html @@ -37,6 +37,9 @@ +
+
{% trans 'Loading' %}...
+
@@ -48,7 +51,7 @@ var interval; function initLdapUsersTable() { if(ldap_users_table){ - ldap_users_table.ajax.reload(); + ldap_users_table.ajax.reload(null, false); return ldap_users_table } var options = { @@ -77,6 +80,7 @@ function initLdapUsersTable() { } function testRequestLdapUser(){ + $("#fake_datatable_wrapper_loading").css('display', 'block'); var the_url = "{% url 'api-settings:ldap-user-list' %}"; var error = function (data, status) { if (status === 409){ @@ -92,6 +96,7 @@ function testRequestLdapUser(){ console.log(data, status) }; var success = function() { + $("#fake_datatable_wrapper_loading").css('display', 'none'); initLdapUsersTable(); clearInterval(interval); interval = undefined @@ -145,6 +150,7 @@ $(document).ready(function(){ } function success(message) { toastr.success(message.msg); + ldap_users_table.selected = []; timingTestRequestLdapUser(); } requestApi({ diff --git a/apps/settings/utils/ldap.py b/apps/settings/utils/ldap.py index ba3e5a838..3df45bfae 100644 --- a/apps/settings/utils/ldap.py +++ b/apps/settings/utils/ldap.py @@ -187,7 +187,8 @@ class LDAPCacheUtil(object): def get_users(self): users = cache.get(self.CACHE_KEY_USERS) - logger.info('Get ldap users from cache, count: {}'.format(len(users))) + count = users if users is None else len(users) + logger.info('Get ldap users from cache, count: {}'.format(count)) return users def delete_users(self): @@ -195,6 +196,8 @@ class LDAPCacheUtil(object): cache.delete(self.CACHE_KEY_USERS) def filter_users(self, users): + if users is None: + return users if self.search_users: filter_users = [ user for user in users