diff --git a/apps/settings/api.py b/apps/settings/api.py index d7c789603..29986637e 100644 --- a/apps/settings/api.py +++ b/apps/settings/api.py @@ -5,7 +5,9 @@ import os import json import jms_storage +from rest_framework import generics from rest_framework.views import Response, APIView +from rest_framework.pagination import LimitOffsetPagination from django.conf import settings from django.core.mail import send_mail from django.utils.translation import ugettext_lazy as _ @@ -89,19 +91,55 @@ class LDAPTestingAPI(APIView): return Response({"error": "Have user but attr mapping error"}, status=401) -class LDAPUserListApi(APIView): +class LDAPUserListApi(generics.ListAPIView): + pagination_class = LimitOffsetPagination permission_classes = (IsOrgAdmin,) - def get(self, request): + def get_queryset(self): util = LDAPUtil() try: users = util.search_user_items() except Exception as e: users = [] logger.error(e, exc_info=True) + # 前端data_table会根据row.id对table.selected值进行操作 + for user in users: + user['id'] = user['username'] + return users + + def filter_queryset(self, queryset): + search = self.request.query_params.get('search') + if not search: + return queryset + search = search.lower() + queryset = [ + q for q in queryset + if + search in q['username'].lower() + or search in q['name'].lower() + or search in q['email'].lower() + ] + 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: - users = sorted(users, key=lambda u: (u['existing'], u['username'])) - return Response(users) + reverse = False + queryset = sorted(queryset, key=lambda x: x[order_by], reverse=reverse) + return queryset + + def list(self, request, *args, **kwargs): + queryset = self.filter_queryset(self.get_queryset()) + queryset = self.sort_queryset(queryset) + page = self.paginate_queryset(queryset) + if page is not None: + return self.get_paginated_response(page) + return Response(queryset) class LDAPUserSyncAPI(APIView): diff --git a/apps/settings/templates/settings/_ldap_list_users_modal.html b/apps/settings/templates/settings/_ldap_list_users_modal.html index 9609d80ce..c0d062714 100644 --- a/apps/settings/templates/settings/_ldap_list_users_modal.html +++ b/apps/settings/templates/settings/_ldap_list_users_modal.html @@ -52,7 +52,7 @@ var ldap_users_table = 0; function initLdapUsersTable() { if(ldap_users_table){ - return + return ldap_users_table } var options = { ele: $('#ldap_list_users_table'), @@ -73,10 +73,10 @@ function initLdapUsersTable() { {data: "username" },{data: "username" }, {data: "name" }, {data:"email"}, {data:'existing'} ], - pageLength: 10 + pageLength: 15 }; - ldap_users_table = jumpserver.initDataTable(options); + ldap_users_table = jumpserver.initServerSideDataTable(options); return ldap_users_table } diff --git a/apps/settings/templates/settings/ldap_setting.html b/apps/settings/templates/settings/ldap_setting.html index d19a84292..58e4ae71b 100644 --- a/apps/settings/templates/settings/ldap_setting.html +++ b/apps/settings/templates/settings/ldap_setting.html @@ -110,10 +110,7 @@ $(document).ready(function () { }); }) .on("click","#btn_ldap_modal_confirm",function () { - var username_list=[]; - $("tbody input[type='checkbox']:checked").each(function () { - username_list.push($(this).attr('id')); - }); + var username_list = ldap_users_table.selected; if (username_list.length === 0){ var msg = "{% trans 'User is not currently selected, please check the user you want to import'%}"; diff --git a/apps/settings/utils.py b/apps/settings/utils.py index 563f5fc3a..adcd2c839 100644 --- a/apps/settings/utils.py +++ b/apps/settings/utils.py @@ -61,7 +61,6 @@ class LDAPUtil: try: user = User.objects.get(username=username) except Exception as e: - logger.info(e) return None else: return user