Skip to content

Commit

Permalink
YDA-5409: add SRAM invitation flow
Browse files Browse the repository at this point in the history
  • Loading branch information
lwesterhof committed Oct 5, 2023
1 parent 84723c7 commit a14b105
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
2 changes: 1 addition & 1 deletion groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ def api_group_user_add(ctx, username, group_name):
if config.sram_flow == 'join_request':
sram.invitation_mail_group_add_user(ctx, group_name, username.split('#')[0], co_identifier)
elif config.sram_flow == 'invitation':
sram.invitation_mail_group_add_user(ctx, group_name, username.split('#')[0], co_identifier)
sram.sram_put_collaboration_invitation(ctx, group_name, username.split('#')[0], co_identifier)
return api.Result.ok()
else:
return api.Error('policy_error', message)
Expand Down
45 changes: 45 additions & 0 deletions sram.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,51 @@ def sram_delete_collaboration_membership(ctx, co_identifier, uuid):
return response.status_code == 204


def sram_put_collaboration_invitation(ctx, group_name, username, co_identifier):
"""Create SRAM Collaborative Organisation Identifier.
:param ctx: Combined type of a ctx and rei struct
:param group_name: Name of the group the user is to invited to join
:param username: Name of the user to be invited
:param co_identifier: SRAM identifier of the group the user is to invited to join
:returns: Boolean indicating if put of new collaboration invitation succeeded
"""
url = "{}/api/invitations/v1/collaboration_invites".format(config.sram_rest_api_url)
headers = {'Content-Type': 'application/json', 'charset': 'UTF-8', 'Authorization': 'bearer ' + config.sram_api_key}

# Now plus a year.
expiration_date = datetime.datetime.fromtimestamp(int(time.time() + 3600 * 24 * 365)).strftime('%Y-%m-%d')

# Get epoch expiry date.
date = datetime.datetime.strptime(expiration_date, "%Y-%m-%d")
epoch = datetime.datetime.utcfromtimestamp(0)
epoch_date = int((date - epoch).total_seconds())

# Build SRAM payload.
payload = {
"short_name": group_name,
"collaboration_identifier": co_identifier,
"message": "Invitation to join Yoda group {}".format(group_name),
"intended_role": "member",
"invitation_expiry_date": epoch_date,
"invites": [
username
],
"groups": []
}

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

response = requests.post(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 == 201


def invitation_mail_group_add_user(ctx, group_name, username, co_identifier):
"""Send invitation email to newly added user to the group.
Expand Down

0 comments on commit a14b105

Please sign in to comment.