From b886fddc829e708c2e436b78d3516016dc844ac6 Mon Sep 17 00:00:00 2001 From: kaur16 <126662478+kaur16@users.noreply.github.com> Date: Wed, 3 Jan 2024 17:05:13 +0100 Subject: [PATCH] YDA-5566: add a check if user is already in SRAM collaboration before sending an invitation Co-authored-by: Lazlo Westerhof --- groups.py | 37 +++++++++++++++++++++---------------- sram.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/groups.py b/groups.py index 162285179..a658dc1ad 100644 --- a/groups.py +++ b/groups.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Functions for group management and group queries.""" -__copyright__ = 'Copyright (c) 2018-2023, Utrecht University' +__copyright__ = 'Copyright (c) 2018-2024, Utrecht University' __license__ = 'GPLv3, see LICENSE' import time @@ -1345,27 +1345,32 @@ def rule_group_sram_sync(ctx): log.write(ctx, "Something went wrong connecting service to group {} in SRAM".format(group_name)) break + log.write(ctx, "Get members of group {} from SRAM".format(group_name)) + co_members = sram.sram_get_co_members(ctx, co_identifier) + log.write(ctx, "Sync members of group {} with SRAM".format(group_name)) for member in members: # Validate email if not yoda_names.is_email_username(member): log.write(ctx, "User {} cannot be added to group {} because user email is invalid".format(member, group_name)) continue - elif config.sram_flow == 'join_request': - sram.invitation_mail_group_add_user(ctx, group_name, member.split('#')[0], co_identifier) - log.write(ctx, "User {} added to group {}".format(member, group_name)) - elif config.sram_flow == 'invitation': - sram.sram_put_collaboration_invitation(ctx, group_name, member.split('#')[0], co_identifier) - log.write(ctx, "User {} added to group {}".format(member, group_name)) - - if member in managers: - uid = sram.sram_get_uid(ctx, co_identifier, member) - if uid == '': - log.write(ctx, "Something went wrong getting the SRAM user id for user {} of group {}".format(member, group_name)) - else: - if sram.sram_update_collaboration_membership(ctx, co_identifier, uid, "manager"): - log.write(ctx, "Updated {} user to manager of group {}".format(member, group_name)) + + if member.split('#')[0] not in co_members: + if config.sram_flow == 'join_request': + sram.invitation_mail_group_add_user(ctx, group_name, member.split('#')[0], co_identifier) + log.write(ctx, "User {} added to group {}".format(member, group_name)) + elif config.sram_flow == 'invitation': + sram.sram_put_collaboration_invitation(ctx, group_name, member.split('#')[0], co_identifier) + log.write(ctx, "User {} added to group {}".format(member, group_name)) + else: + if member in managers: + uid = sram.sram_get_uid(ctx, co_identifier, member) + if uid == '': + log.write(ctx, "Something went wrong getting the SRAM user id for user {} of group {}".format(member, group_name)) else: - log.write(ctx, "Something went wrong updating {} user to manager of group {} in SRAM".format(member, group_name)) + if sram.sram_update_collaboration_membership(ctx, co_identifier, uid, "manager"): + log.write(ctx, "Updated {} user to manager of group {}".format(member, group_name)) + else: + log.write(ctx, "Something went wrong updating {} user to manager of group {} in SRAM".format(member, group_name)) log.write(ctx, "Finished syncing groups with SRAM") diff --git a/sram.py b/sram.py index bbf4813de..7525e160c 100644 --- a/sram.py +++ b/sram.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- """Functions for communicating with SRAM and some utilities.""" -__copyright__ = 'Copyright (c) 2023, Utrecht University' +__copyright__ = 'Copyright (c) 2023-2024, Utrecht University' __license__ = 'GPLv3, see LICENSE' import datetime @@ -273,3 +273,33 @@ def sram_update_collaboration_membership(ctx, co_identifier, uuid, new_role): log.write(ctx, "response: {}".format(response.status_code)) return response.status_code == 201 + + +def sram_get_co_members(ctx, co_identifier): + """Get SRAM Collaboration members. + + :param ctx: Combined type of a callback and rei struct + :param co_identifier: SRAM CO identifier + + :returns: Email of the user + """ + url = "{}/api/collaborations/v1/{}".format(config.sram_rest_api_url, co_identifier) + headers = {'Content-Type': 'application/json', 'charset': 'UTF-8', 'Authorization': 'bearer ' + config.sram_api_key} + + if config.sram_verbose_logging: + log.write(ctx, "get {}".format(url)) + + response = requests.get(url, headers=headers, timeout=30, verify=config.sram_tls_verify) + data = response.json() + + if config.sram_verbose_logging: + log.write(ctx, "response: {}".format(data)) + + co_members = [] + for key in data['collaboration_memberships']: + co_members.append(key['user']['email']) + + if config.sram_verbose_logging: + log.write(ctx, "collaboration_members: {}".format(co_members)) + + return co_members