Commit 00e986a6 authored by BaiJiangJie's avatar BaiJiangJie Committed by 老广

[Update] 优化LDAP用户导入功能,可导入跨页选取的所有用户 (#2745)

parent dc4bf669
......@@ -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):
......
......@@ -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
}
......
......@@ -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'%}";
......
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment