diff --git a/apps/settings/api/ldap.py b/apps/settings/api/ldap.py index f8b052a90..3c596cf19 100644 --- a/apps/settings/api/ldap.py +++ b/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 ( diff --git a/apps/settings/const.py b/apps/settings/const.py new file mode 100644 index 000000000..4e4028d4f --- /dev/null +++ b/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' diff --git a/apps/settings/serializers/auth/ldap.py b/apps/settings/serializers/auth/ldap.py index e0bd51389..c835cf011 100644 --- a/apps/settings/serializers/auth/ldap.py +++ b/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): diff --git a/apps/settings/utils/ldap.py b/apps/settings/utils/ldap.py index 5699e4b20..6f0fc97de 100644 --- a/apps/settings/utils/ldap.py +++ b/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): diff --git a/apps/settings/ws.py b/apps/settings/ws.py index 22fcb137a..e5762e1f5 100644 --- a/apps/settings/ws.py +++ b/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: