perf:LDAP报错信息不对

pull/12832/head
wangruidong 2024-03-14 16:52:52 +08:00 committed by Bryan
parent d8a891a7d7
commit 80a506e99f
1 changed files with 24 additions and 65 deletions

View File

@ -28,15 +28,7 @@ from .tools import (
logger = get_logger(__name__)
CACHE_KEY_LDAP_TEST_CONFIG_MSG = 'CACHE_KEY_LDAP_TEST_CONFIG_MSG'
CACHE_KEY_LDAP_TEST_LOGIN_MSG = 'CACHE_KEY_LDAP_TEST_LOGIN_MSG'
CACHE_KEY_LDAP_SYNC_USER_MSG = 'CACHE_KEY_LDAP_SYNC_USER_MSG'
CACHE_KEY_LDAP_IMPORT_USER_MSG = 'CACHE_KEY_LDAP_IMPORT_USER_MSG'
CACHE_KEY_LDAP_TEST_CONFIG_TASK_STATUS = 'CACHE_KEY_LDAP_TEST_CONFIG_TASK_STATUS'
CACHE_KEY_LDAP_TEST_LOGIN_TASK_STATUS = 'CACHE_KEY_LDAP_TEST_LOGIN_TASK_STATUS'
CACHE_KEY_LDAP_SYNC_USER_TASK_STATUS = 'CACHE_KEY_LDAP_SYNC_USER_TASK_STATUS'
CACHE_KEY_LDAP_IMPORT_USER_TASK_STATUS = 'CACHE_KEY_LDAP_IMPORT_USER_TASK_STATUS'
TASK_STATUS_IS_RUNNING = 'RUNNING'
TASK_STATUS_IS_OVER = 'OVER'
@ -118,15 +110,7 @@ class LdapWebsocket(AsyncJsonWebsocketConsumer):
msg_type = data.pop('msg_type', 'testing_config')
try:
tool_func = getattr(self, f'run_{msg_type.lower()}')
await asyncio.to_thread(tool_func, data)
if msg_type == 'testing_config':
ok, msg = cache.get(CACHE_KEY_LDAP_TEST_CONFIG_MSG)
elif msg_type == 'sync_user':
ok, msg = cache.get(CACHE_KEY_LDAP_SYNC_USER_MSG)
elif msg_type == 'import_user':
ok, msg = cache.get(CACHE_KEY_LDAP_IMPORT_USER_MSG)
else:
ok, msg = cache.get(CACHE_KEY_LDAP_TEST_LOGIN_MSG)
ok, msg = await asyncio.to_thread(tool_func, data)
await self.send_msg(ok, msg)
except Exception as error:
await self.send_msg(msg='Exception: %s' % error)
@ -172,60 +156,35 @@ class LdapWebsocket(AsyncJsonWebsocketConsumer):
def set_task_status_over(task_key, ttl=120):
cache.set(task_key, TASK_STATUS_IS_OVER, ttl)
@staticmethod
def set_task_msg(task_key, ok, msg, ttl=120):
cache.set(task_key, (ok, msg), ttl)
def run_testing_config(self, data):
while True:
if self.task_is_over(CACHE_KEY_LDAP_TEST_CONFIG_TASK_STATUS):
break
else:
serializer = LDAPTestConfigSerializer(data=data)
if not serializer.is_valid():
self.send_msg(msg=f'error: {str(serializer.errors)}')
config = self.get_ldap_config(serializer)
ok, msg = LDAPTestUtil(config).test_config()
if ok:
self.set_task_status_over(CACHE_KEY_LDAP_TEST_CONFIG_TASK_STATUS)
self.set_task_msg(CACHE_KEY_LDAP_TEST_CONFIG_MSG, ok, msg)
return ok, msg
def run_testing_login(self, data):
while True:
if self.task_is_over(CACHE_KEY_LDAP_TEST_LOGIN_TASK_STATUS):
break
else:
serializer = LDAPTestLoginSerializer(data=data)
if not serializer.is_valid():
self.send_msg(msg=f'error: {str(serializer.errors)}')
username = serializer.validated_data['username']
password = serializer.validated_data['password']
ok, msg = LDAPTestUtil().test_login(username, password)
self.set_task_status_over(CACHE_KEY_LDAP_TEST_LOGIN_TASK_STATUS, 3)
self.set_task_msg(CACHE_KEY_LDAP_TEST_LOGIN_MSG, ok, msg)
return ok, msg
def run_sync_user(self, data):
while True:
if self.task_is_over(CACHE_KEY_LDAP_SYNC_USER_TASK_STATUS):
break
else:
@staticmethod
def run_sync_user(data):
sync_util = LDAPSyncUtil()
sync_util.clear_cache()
sync_ldap_user()
msg = sync_util.get_task_error_msg()
ok = False if msg else True
self.set_task_status_over(CACHE_KEY_LDAP_SYNC_USER_TASK_STATUS)
self.set_task_msg(CACHE_KEY_LDAP_SYNC_USER_MSG, ok, msg)
return ok, msg
def run_import_user(self, data):
while True:
if self.task_is_over(CACHE_KEY_LDAP_IMPORT_USER_TASK_STATUS):
break
else:
ok, msg = self.import_user(data)
self.set_task_status_over(CACHE_KEY_LDAP_IMPORT_USER_TASK_STATUS, 3)
self.set_task_msg(CACHE_KEY_LDAP_IMPORT_USER_MSG, ok, msg, 3)
def import_user(self, data):
ok = False
org_ids = data.get('org_ids')
username_list = data.get('username_list', [])