mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-08 10:49:08 +00:00
perf: ldap接口请求换成websocket连接
This commit is contained in:
@@ -28,71 +28,6 @@ from ..utils import (
|
||||
logger = get_logger(__file__)
|
||||
|
||||
|
||||
class LDAPTestingConfigAPI(AsyncApiMixin, CreateAPIView):
|
||||
serializer_class = LDAPTestConfigSerializer
|
||||
perm_model = Setting
|
||||
rbac_perms = {
|
||||
'POST': 'settings.change_auth',
|
||||
'create': 'settings.change_auth',
|
||||
}
|
||||
|
||||
def is_need_async(self):
|
||||
return True
|
||||
|
||||
def create(self, request, *args, **kwargs):
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if not serializer.is_valid():
|
||||
return Response({"error": str(serializer.errors)}, status=400)
|
||||
config = self.get_ldap_config(serializer)
|
||||
ok, msg = LDAPTestUtil(config).test_config()
|
||||
status = 200 if ok else 400
|
||||
return Response(msg, status=status)
|
||||
|
||||
@staticmethod
|
||||
def get_ldap_config(serializer):
|
||||
server_uri = serializer.validated_data["AUTH_LDAP_SERVER_URI"]
|
||||
bind_dn = serializer.validated_data["AUTH_LDAP_BIND_DN"]
|
||||
password = serializer.validated_data["AUTH_LDAP_BIND_PASSWORD"]
|
||||
use_ssl = serializer.validated_data.get("AUTH_LDAP_START_TLS", False)
|
||||
search_ou = serializer.validated_data["AUTH_LDAP_SEARCH_OU"]
|
||||
search_filter = serializer.validated_data["AUTH_LDAP_SEARCH_FILTER"]
|
||||
attr_map = serializer.validated_data["AUTH_LDAP_USER_ATTR_MAP"]
|
||||
auth_ldap = serializer.validated_data.get('AUTH_LDAP', False)
|
||||
|
||||
if not password:
|
||||
password = settings.AUTH_LDAP_BIND_PASSWORD
|
||||
|
||||
config = {
|
||||
'server_uri': server_uri,
|
||||
'bind_dn': bind_dn,
|
||||
'password': password,
|
||||
'use_ssl': use_ssl,
|
||||
'search_ou': search_ou,
|
||||
'search_filter': search_filter,
|
||||
'attr_map': attr_map,
|
||||
'auth_ldap': auth_ldap
|
||||
}
|
||||
return config
|
||||
|
||||
|
||||
class LDAPTestingLoginAPI(APIView):
|
||||
serializer_class = LDAPTestLoginSerializer
|
||||
perm_model = Setting
|
||||
rbac_perms = {
|
||||
'POST': 'settings.change_auth'
|
||||
}
|
||||
|
||||
def post(self, request):
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if not serializer.is_valid():
|
||||
return Response({"error": str(serializer.errors)}, status=400)
|
||||
username = serializer.validated_data['username']
|
||||
password = serializer.validated_data['password']
|
||||
ok, msg = LDAPTestUtil().test_login(username, password)
|
||||
status = 200 if ok else 400
|
||||
return Response(msg, status=status)
|
||||
|
||||
|
||||
class LDAPUserListApi(generics.ListAPIView):
|
||||
serializer_class = LDAPUserSerializer
|
||||
perm_model = Setting
|
||||
@@ -162,31 +97,10 @@ class LDAPUserListApi(generics.ListAPIView):
|
||||
# 缓存有数据
|
||||
if queryset is not None:
|
||||
return super().list(request, *args, **kwargs)
|
||||
|
||||
sync_util = LDAPSyncUtil()
|
||||
# 还没有同步任务
|
||||
if sync_util.task_no_start:
|
||||
ok, msg = LDAPTestUtil().test_config()
|
||||
if not ok:
|
||||
return Response(data={'msg': msg}, status=400)
|
||||
# 任务外部设置 task running 状态
|
||||
sync_util.set_task_status(sync_util.TASK_STATUS_IS_RUNNING)
|
||||
t = threading.Thread(target=sync_ldap_user)
|
||||
t.start()
|
||||
data = {'msg': _('Synchronization start, please wait.')}
|
||||
return Response(data=data, status=409)
|
||||
# 同步任务正在执行
|
||||
if sync_util.task_is_running:
|
||||
data = {'msg': _('Synchronization is running, please wait.')}
|
||||
return Response(data=data, status=409)
|
||||
# 同步任务执行结束
|
||||
if sync_util.task_is_over:
|
||||
msg = sync_util.get_task_error_msg()
|
||||
data = {'error': _('Synchronization error: {}'.format(msg))}
|
||||
else:
|
||||
data = {'msg': _('Users are not synchronized, please click the user synchronization button')}
|
||||
return Response(data=data, status=400)
|
||||
|
||||
return super().list(request, *args, **kwargs)
|
||||
|
||||
|
||||
class LDAPUserImportAPI(APIView):
|
||||
perm_model = Setting
|
||||
@@ -232,18 +146,3 @@ class LDAPUserImportAPI(APIView):
|
||||
return Response({
|
||||
'msg': _('Imported {} users successfully (Organization: {})').format(count, orgs_name)
|
||||
})
|
||||
|
||||
|
||||
class LDAPCacheRefreshAPI(generics.RetrieveAPIView):
|
||||
perm_model = Setting
|
||||
rbac_perms = {
|
||||
'retrieve': 'settings.change_auth'
|
||||
}
|
||||
|
||||
def retrieve(self, request, *args, **kwargs):
|
||||
try:
|
||||
LDAPSyncUtil().clear_cache()
|
||||
except Exception as e:
|
||||
logger.error(str(e))
|
||||
return Response(data={'msg': str(e)}, status=400)
|
||||
return Response(data={'msg': 'success'})
|
||||
|
Reference in New Issue
Block a user