perf: ldap import user error msg

pull/14062/head
wangruidong 3 months ago committed by Bryan
parent f866b93f96
commit 7a19007aba

@ -6,6 +6,7 @@ from rest_framework.views import Response
from common.utils import get_logger from common.utils import get_logger
from users.models import User from users.models import User
from ..const import ImportStatus
from ..models import Setting from ..models import Setting
from ..serializers import LDAPUserSerializer from ..serializers import LDAPUserSerializer
from ..utils import ( from ..utils import (

@ -0,0 +1,7 @@
from django.db.models import TextChoices
class ImportStatus(TextChoices):
ok = 'ok', 'Ok'
pending = 'pending', 'Pending'
error = 'error', 'Error'

@ -33,6 +33,7 @@ class LDAPUserSerializer(serializers.Serializer):
email = serializers.CharField() email = serializers.CharField()
groups = serializers.ListField(child=serializers.CharField(), default=[]) groups = serializers.ListField(child=serializers.CharField(), default=[])
existing = serializers.BooleanField(read_only=True) existing = serializers.BooleanField(read_only=True)
status = serializers.JSONField(read_only=True)
class LDAPSettingSerializer(serializers.Serializer): class LDAPSettingSerializer(serializers.Serializer):

@ -30,6 +30,7 @@ from common.db.utils import close_old_connections
from common.utils import timeit, get_logger from common.utils import timeit, get_logger
from common.utils.http import is_true from common.utils.http import is_true
from orgs.utils import tmp_to_org from orgs.utils import tmp_to_org
from settings.const import ImportStatus
from users.models import User, UserGroup from users.models import User, UserGroup
from users.utils import construct_user_email from users.utils import construct_user_email
@ -199,6 +200,7 @@ class LDAPServerUtil(object):
if not isinstance(value, list): if not isinstance(value, list):
value = [] value = []
user[attr] = value.strip() if isinstance(value, str) else value user[attr] = value.strip() if isinstance(value, str) else value
user['status'] = ImportStatus.pending
return user return user
def user_entries_to_dict(self, user_entries): def user_entries_to_dict(self, user_entries):

@ -23,6 +23,7 @@ from settings.utils import (
LDAPServerUtil, LDAPCacheUtil, LDAPImportUtil, LDAPSyncUtil, LDAPServerUtil, LDAPCacheUtil, LDAPImportUtil, LDAPSyncUtil,
LDAP_USE_CACHE_FLAGS, LDAPTestUtil LDAP_USE_CACHE_FLAGS, LDAPTestUtil
) )
from .const import ImportStatus
from .tools import ( from .tools import (
verbose_ping, verbose_telnet, verbose_nmap, verbose_ping, verbose_telnet, verbose_nmap,
verbose_tcpdump, verbose_traceroute verbose_tcpdump, verbose_traceroute
@ -208,10 +209,24 @@ class LdapWebsocket(AsyncJsonWebsocketConsumer):
msg = _('Total {}, success {}, failure {}').format( msg = _('Total {}, success {}, failure {}').format(
len(users), success_count, len(error_msg) len(users), success_count, len(error_msg)
) )
self.set_users_status(users, error_msg)
except Exception as e: except Exception as e:
msg = str(e) msg = str(e)
return ok, msg return ok, msg
def set_users_status(self, import_users, errors):
util = LDAPCacheUtil()
all_users = util.get_users()
import_usernames = [u['username'] for u in import_users]
errors_mapper = {k: v for err in errors for k, v in err.items()}
for user in all_users:
username = user['username']
if username in errors_mapper:
user['status'] = {'error': errors_mapper[username]}
elif username in import_usernames:
user['status'] = ImportStatus.ok
LDAPCacheUtil().set_users(all_users)
@staticmethod @staticmethod
def get_orgs(org_ids): def get_orgs(org_ids):
if org_ids: if org_ids:

Loading…
Cancel
Save