Browse Source

perf: ldap import user error msg

pull/14062/head
wangruidong 3 months ago committed by Bryan
parent
commit
7a19007aba
  1. 1
      apps/settings/api/ldap.py
  2. 7
      apps/settings/const.py
  3. 1
      apps/settings/serializers/auth/ldap.py
  4. 2
      apps/settings/utils/ldap.py
  5. 15
      apps/settings/ws.py

1
apps/settings/api/ldap.py

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

7
apps/settings/const.py

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

1
apps/settings/serializers/auth/ldap.py

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

2
apps/settings/utils/ldap.py

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

15
apps/settings/ws.py

@ -23,6 +23,7 @@ from settings.utils import (
LDAPServerUtil, LDAPCacheUtil, LDAPImportUtil, LDAPSyncUtil,
LDAP_USE_CACHE_FLAGS, LDAPTestUtil
)
from .const import ImportStatus
from .tools import (
verbose_ping, verbose_telnet, verbose_nmap,
verbose_tcpdump, verbose_traceroute
@ -208,10 +209,24 @@ class LdapWebsocket(AsyncJsonWebsocketConsumer):
msg = _('Total {}, success {}, failure {}').format(
len(users), success_count, len(error_msg)
)
self.set_users_status(users, error_msg)
except Exception as e:
msg = str(e)
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
def get_orgs(org_ids):
if org_ids:

Loading…
Cancel
Save