Skip to content

Commit

Permalink
YDA-5468 - Added API call to SRAM to change user role
Browse files Browse the repository at this point in the history
  • Loading branch information
kaur16 authored Oct 5, 2023
1 parent a14b105 commit 1bb5f23
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
20 changes: 14 additions & 6 deletions groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -1146,9 +1146,8 @@ def api_group_delete(ctx, group_name):
if config.enable_sram:
sram_group, co_identifier = sram_enabled(ctx, group_name)
if sram_group:
if not sram.sram_delete_collaboration(ctx, co_identifier):
message = response_sram['message']
return api.Error('sram_error', message)
if sram.sram_delete_collaboration(ctx, co_identifier):
return api.Error('sram_error', 'Something went wrong deleting group "{}"'.format(group_name))

response = ctx.uuGroupRemove(group_name, '', '')['arguments']
status = response[1]
Expand Down Expand Up @@ -1242,6 +1241,16 @@ def api_group_user_update_role(ctx, username, group_name, new_role):
:returns: Dict with API status result
"""
try:
if config.enable_sram:
sram_group, co_identifier = sram_enabled(ctx, group_name)
if sram_group:
uid = sram.sram_get_uid(ctx, co_identifier, username)
if uid == '':
return api.Error('sram_error', 'Something went wrong getting the unique user id for user {} from SRAM. Please contact a system administrator.'.format(username))
else:
if sram.sram_update_collaboration_membership(ctx, co_identifier, uid, new_role):
return api.Error('sram_error', 'Something went wrong updating role for {} user.'.format(username))

response = ctx.uuGroupUserChangeRole(group_name, username, new_role, '', '')['arguments']
status = response[3]
message = response[4]
Expand Down Expand Up @@ -1285,9 +1294,8 @@ def api_group_remove_user_from_group(ctx, username, group_name):
if uid == '':
return api.Error('sram_error', 'Something went wrong getting the unique user id for user {} from SRAM. Please contact a system administrator.'.format(username))
else:
if not sram.sram_delete_collaboration_membership(ctx, co_identifier, uid):
message = response_sram['message']
return api.Error('sram_error', message)
if sram.sram_delete_collaboration_membership(ctx, co_identifier, uid):
return api.Error('sram_error', 'Something went wrong removing {} from group "{}"'.format(username, group_name))

response = ctx.uuGroupUserRemove(group_name, username, '', '')['arguments']
status = response[2]
Expand Down
34 changes: 34 additions & 0 deletions sram.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,37 @@ def invitation_mail_group_add_user(ctx, group_name, username, co_identifier):
With kind regards,
Yoda
""".format(username.split('@')[0], session_vars.get_map(ctx.rei)["client_user"]["user_name"], config.sram_rest_api_url, co_identifier))


def sram_update_collaboration_membership(ctx, co_identifier, uuid, new_role):
"""Update SRAM Collaborative Organisation membership.
:param ctx: Combined type of a callback and rei struct
:param co_identifier: SRAM CO identifier
:param uuid: Unique id of the user
:param new_role: New role of the user
:returns: Boolean indicating that updation of collaboration membership succeeded
"""
url = "{}/api/collaborations/v1/{}/members".format(config.sram_rest_api_url, co_identifier)
headers = {'Content-Type': 'application/json', 'charset': 'UTF-8', 'Authorization': 'bearer ' + config.sram_api_key}

if new_role == 'manager':
role = 'admin'
else:
role = 'member'

payload = {
"role": role,
"uid": uuid
}

if config.sram_verbose_logging:
log.write(ctx, "put {}".format(url))

response = requests.put(url, json=payload, headers=headers, timeout=30, verify=config.sram_tls_verify)

if config.sram_verbose_logging:
log.write(ctx, "response: {}".format(response.status_code))

return response.status_code == 204

0 comments on commit 1bb5f23

Please sign in to comment.