Skip to content

Commit

Permalink
YDA-5566: add a check if user is already in SRAM collaboration before…
Browse files Browse the repository at this point in the history
… sending an invitation

Co-authored-by: Lazlo Westerhof <[email protected]>
  • Loading branch information
kaur16 and lwesterhof authored Jan 3, 2024
1 parent 5257e91 commit b886fdd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
37 changes: 21 additions & 16 deletions groups.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")
32 changes: 31 additions & 1 deletion sram.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit b886fdd

Please sign in to comment.