From 8547f66ca484d7cd7cba822c78b46bfb30189240 Mon Sep 17 00:00:00 2001 From: wang <40563566+loveclever@users.noreply.github.com> Date: Tue, 16 Jan 2024 18:43:41 +0800 Subject: [PATCH] fix code --- frontend/src/components/common/notice-item.js | 8 ++++ seahub/adfs_auth/utils.py | 2 +- seahub/adfs_auth/views.py | 40 +++++++++---------- .../management/commands/send_notices.py | 8 ++++ seahub/notifications/models.py | 3 ++ .../templates/notifications/notice_email.html | 3 ++ seahub/notifications/utils.py | 7 ++++ 7 files changed, 50 insertions(+), 21 deletions(-) diff --git a/frontend/src/components/common/notice-item.js b/frontend/src/components/common/notice-item.js index ab349db1237..f4f2fb44efc 100644 --- a/frontend/src/components/common/notice-item.js +++ b/frontend/src/components/common/notice-item.js @@ -19,6 +19,7 @@ const MSG_TYPE_DRAFT_REVIEWER = 'draft_reviewer'; // const MSG_TYPE_GUEST_INVITATION_ACCEPTED = 'guest_invitation_accepted'; const MSG_TYPE_REPO_MONITOR = 'repo_monitor'; const MSG_TYPE_DELETED_FILES = 'deleted_files'; +const MSG_TYPE_SAML_SSO_FAILED = 'saml_sso_failed'; class NoticeItem extends React.Component { @@ -282,6 +283,13 @@ class NoticeItem extends React.Component { return { avatar_url : null, notice }; } + if (noticeType === MSG_TYPE_SAML_SSO_FAILED) { + const { error_msg } = detail; + let notice = gettext(error_msg); + + return { avatar_url : null, notice }; + } + // if (noticeType === MSG_TYPE_GUEST_INVITATION_ACCEPTED) { // } diff --git a/seahub/adfs_auth/utils.py b/seahub/adfs_auth/utils.py index eed417a4429..1b66e4a281a 100644 --- a/seahub/adfs_auth/utils.py +++ b/seahub/adfs_auth/utils.py @@ -130,7 +130,7 @@ def config_settings_loader(request): try: conf = SPConfig() conf.load(copy.deepcopy(saml_config)) - except RuntimeError as e: + except Exception as e: logger.exception('Failed to load adfs/saml config, error: %s' % e) raise RuntimeError('Failed to load adfs/saml config, error: %s' % e) return conf diff --git a/seahub/adfs_auth/views.py b/seahub/adfs_auth/views.py index b1aa0b4919c..1edbc25ee2b 100644 --- a/seahub/adfs_auth/views.py +++ b/seahub/adfs_auth/views.py @@ -147,11 +147,11 @@ def login(request, org_id=None): if org: org_admins = get_org_admins(org) for org_admin in org_admins: - saml_sso_failed.send(sender=None, to_user=org_admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=org_admin.email, error_msg=error_msg) else: admins = User.objects.get_superusers() for admin in admins: - saml_sso_failed.send(sender=None, to_user=admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=admin.email, error_msg=error_msg) return HttpResponseBadRequest(_('Login failed: ADFS/SAML service error. ' 'Please report to your organization (company) administrator.')) except Exception as e: @@ -200,11 +200,11 @@ def assertion_consumer_service(request, org_id=None, attribute_mapping=None, cre if org: org_admins = get_org_admins(org) for org_admin in org_admins: - saml_sso_failed.send(sender=None, to_user=org_admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=org_admin.email, error_msg=error_msg) else: admins = User.objects.get_superusers() for admin in admins: - saml_sso_failed.send(sender=None, to_user=admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=admin.email, error_msg=error_msg) return HttpResponseBadRequest(_('Login failed: Bad response from ADFS/SAML service. ' 'Please report to your organization (company) administrator.')) @@ -217,11 +217,11 @@ def assertion_consumer_service(request, org_id=None, attribute_mapping=None, cre if org: org_admins = get_org_admins(org) for org_admin in org_admins: - saml_sso_failed.send(sender=None, to_user=org_admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=org_admin.email, error_msg=error_msg) else: admins = User.objects.get_superusers() for admin in admins: - saml_sso_failed.send(sender=None, to_user=admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=admin.email, error_msg=error_msg) return HttpResponseBadRequest(_('Login failed: ADFS/SAML service error. ' 'Please report to your organization (company) administrator.')) except Exception as e: @@ -244,11 +244,11 @@ def assertion_consumer_service(request, org_id=None, attribute_mapping=None, cre if org: org_admins = get_org_admins(org) for org_admin in org_admins: - saml_sso_failed.send(sender=None, to_user=org_admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=org_admin.email, error_msg=error_msg) else: admins = User.objects.get_superusers() for admin in admins: - saml_sso_failed.send(sender=None, to_user=admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=admin.email, error_msg=error_msg) return HttpResponseBadRequest(_('Login failed: Bad response from ADFS/SAML service. ' 'Please report to your organization (company) administrator.')) if response is None: @@ -258,11 +258,11 @@ def assertion_consumer_service(request, org_id=None, attribute_mapping=None, cre if org: org_admins = get_org_admins(org) for org_admin in org_admins: - saml_sso_failed.send(sender=None, to_user=org_admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=org_admin.email, error_msg=error_msg) else: admins = User.objects.get_superusers() for admin in admins: - saml_sso_failed.send(sender=None, to_user=admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=admin.email, error_msg=error_msg) return HttpResponseBadRequest(_('Login failed: Bad response from ADFS/SAML service. ' 'Please report to your organization (company) administrator.')) @@ -314,7 +314,7 @@ def assertion_consumer_service(request, org_id=None, attribute_mapping=None, cre error_msg = 'The number of users exceeds the license limit.' admins = User.objects.get_superusers() for admin in admins: - saml_sso_failed.send(sender=None, to_user=admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=admin.email, error_msg=error_msg) return HttpResponseForbidden(_('Internal server error. Please contact system administrator.')) # check user number limit by org member quota @@ -329,7 +329,7 @@ def assertion_consumer_service(request, org_id=None, attribute_mapping=None, cre error_msg = 'Failed to create new user: the number of users exceeds the organization quota.' org_admins = get_org_admins(org) for org_admin in org_admins: - saml_sso_failed.send(sender=None, to_user=org_admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=org_admin.email, error_msg=error_msg) return HttpResponseForbidden(_('Failed to create new user: ' 'the number of users exceeds the organization quota. ' 'Please report to your organization (company) administrator.')) @@ -347,11 +347,11 @@ def assertion_consumer_service(request, org_id=None, attribute_mapping=None, cre if org: org_admins = get_org_admins(org) for org_admin in org_admins: - saml_sso_failed.send(sender=None, to_user=org_admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=org_admin.email, error_msg=error_msg) else: admins = User.objects.get_superusers() for admin in admins: - saml_sso_failed.send(sender=None, to_user=admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=admin.email, error_msg=error_msg) return HttpResponseForbidden(_('Login failed: failed to create user. ' 'Please report to your organization (company) administrator.')) @@ -362,11 +362,11 @@ def assertion_consumer_service(request, org_id=None, attribute_mapping=None, cre if org: org_admins = get_org_admins(org) for org_admin in org_admins: - saml_sso_failed.send(sender=None, to_user=org_admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=org_admin.email, error_msg=error_msg) else: admins = User.objects.get_superusers() for admin in admins: - saml_sso_failed.send(sender=None, to_user=admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=admin.email, error_msg=error_msg) return HttpResponseForbidden(_('Login failed: user is deactivated. ' 'Please report to your organization (company) administrator.')) @@ -401,11 +401,11 @@ def metadata(request, org_id=None): if org: org_admins = get_org_admins(org) for org_admin in org_admins: - saml_sso_failed.send(sender=None, to_user=org_admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=org_admin.email, error_msg=error_msg) else: admins = User.objects.get_superusers() for admin in admins: - saml_sso_failed.send(sender=None, to_user=admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=admin.email, error_msg=error_msg) return HttpResponseBadRequest(_('Login failed: ADFS/SAML service error. ' 'Please report to your organization (company) administrator.')) except Exception as e: @@ -452,11 +452,11 @@ def saml2_connect(request, org_id=None): if org: org_admins = get_org_admins(org) for org_admin in org_admins: - saml_sso_failed.send(sender=None, to_user=org_admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=org_admin.email, error_msg=error_msg) else: admins = User.objects.get_superusers() for admin in admins: - saml_sso_failed.send(sender=None, to_user=admin, error_msg=error_msg) + saml_sso_failed.send(sender=None, to_user=admin.email, error_msg=error_msg) return HttpResponseBadRequest(_('Login failed: ADFS/SAML service error. ' 'Please report to your organization (company) administrator.')) except Exception as e: diff --git a/seahub/notifications/management/commands/send_notices.py b/seahub/notifications/management/commands/send_notices.py index eda003bbe51..6e9ea60f190 100644 --- a/seahub/notifications/management/commands/send_notices.py +++ b/seahub/notifications/management/commands/send_notices.py @@ -258,6 +258,11 @@ def format_repo_monitor_msg(self, notice): return notice + def format_saml_sso_error_msg(self, notice): + d = json.loads(notice.detail) + notice.error_msg = d['error_msg'] + return notice + def format_sdoc_msg(self, sdoc_queryset, sdoc_notice): sdoc_obj = sdoc_queryset.filter(uuid=sdoc_notice.doc_uuid).first() if not sdoc_obj: @@ -421,6 +426,9 @@ def do_action(self): elif notice.is_repo_monitor_msg(): notice = self.format_repo_monitor_msg(notice) + elif notice.is_saml_sso_error_msg(): + notice = self.format_saml_sso_error_msg(notice) + if notice is None: continue diff --git a/seahub/notifications/models.py b/seahub/notifications/models.py index 885a7a7c0db..ca5a3548c94 100644 --- a/seahub/notifications/models.py +++ b/seahub/notifications/models.py @@ -422,6 +422,9 @@ def is_repo_monitor_msg(self): def is_deleted_files_msg(self): return self.msg_type == MSG_TYPE_DELETED_FILES + def is_saml_sso_error_msg(self): + return self.msg_type == MSG_TYPE_SAML_SSO_FAILED + def user_message_detail_to_dict(self): """Parse user message detail, returns dict contains ``message`` and ``msg_from``. diff --git a/seahub/notifications/templates/notifications/notice_email.html b/seahub/notifications/templates/notifications/notice_email.html index cddb3a32d1c..16468b49533 100644 --- a/seahub/notifications/templates/notifications/notice_email.html +++ b/seahub/notifications/templates/notifications/notice_email.html @@ -65,6 +65,9 @@ {% elif notice.is_deleted_files_msg %}
{% blocktrans with repo_url=notice.repo_url repo_name=notice.repo_name %}A large number of files in your library {{ repo_name }} has been deleted recently.{% endblocktrans %}
+ {% elif notice.is_saml_sso_error_msg %} +{% blocktrans %}{{notice.error_msg}}{% endblocktrans %}
+ {% elif notice.is_repo_monitor_msg %}{% if notice.obj_type == 'file' %} diff --git a/seahub/notifications/utils.py b/seahub/notifications/utils.py index 2174dc14f76..ba415d6c304 100644 --- a/seahub/notifications/utils.py +++ b/seahub/notifications/utils.py @@ -268,6 +268,13 @@ def update_notice_detail(request, notices): except Exception as e: logger.error(e) + elif notice.is_saml_sso_error_msg(): + try: + d = json.loads(notice.detail) + notice.detail = d + except Exception as e: + logger.error(e) + return notices