Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user creation examples #391

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions examples/registering-users/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Registering Users

This example illustrates how to write a script to register users in Saturn Cloud.

This example takes a list of email addresses (passed in via an `EMAILS_FOR_ACCOUNTS` environment variable). It will check if a user account exists or not, and if not, create an account for the user. The reason we are passing this in via an ENV var is so that we can pass this via the Saturn secrets manager.
81 changes: 81 additions & 0 deletions examples/registering-users/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import os
from urllib.parse import urlencode

import requests


# this should be populated by the secrets manager
EMAILS_FOR_ACCOUNTS = os.getenv("EMAILS_FOR_ACCOUNTS")

# this should be populated by Saturn. This script will only work if
# run from an account that has admin access
BASE_URL = os.getenv("BASE_URL")
SATURN_TOKEN = os.getenv("SATURN_TOKEN")
saturn_headers = {"Authorization": f"token {SATURN_TOKEN}"}

def check_for_account_by_email(email: str) -> bool:
url = f"{BASE_URL}/api/users"
query_string = urlencode(dict(q=f"email:{email}", page=1, size=1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
query_string = urlencode(dict(q=f"email:{email}", page=1, size=1))
query_string = urlencode(dict(q=f"email:{email}", page=1, page_size=1))

url = url + "?" + query_string
response = requests.get(url, headers=saturn_headers)
results = response.json()['users']
if results:
return True
return False


def check_for_account_by_username(username: str) -> bool:
url = f"{BASE_URL}/api/users"
query_string = urlencode(dict(q=f"username:{username}", page=1, size=1))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
query_string = urlencode(dict(q=f"username:{username}", page=1, size=1))
query_string = urlencode(dict(q=f"username:{username}", page=1, page_size=1))

url = url + "?" + query_string
response = requests.get(url, headers=saturn_headers)
results = response.json()['users']

if results:
return True
return False


def make_unique_username(email: str) -> str:
candidate_username = email.split('@')[0]
candidate_username = "".join(c for c in candidate_username if c.isalnum())

# we'll try 100 integers until we get a unique name
for c in range(100):
to_try = candidate_username
if c:
to_try = candidate_username + str(c)
if not check_for_account_by_username(to_try):
return to_try
raise ValueError(f'unable to find username for {candidate_username}')


def make_account(username: str, email: str):
url = f"{BASE_URL}/api/users"
body = dict(
username=username,
email=email,
admin=False,
locked=False,
send_reset_email=False,
prevent_duplicate_emails=True,
)
response = requests.post(url, json=body, headers=saturn_headers)
print(response.json())


def ensure_account_exists(email: str) -> None:
if check_for_account_by_email(email):
return
username = make_unique_username(email)
make_account(username, email)


def run():
for email in EMAILS_FOR_ACCOUNTS.split('\n'):
if email:
ensure_account_exists(email)


if __name__ == "__main__":
run()