import sys, os, random from tracker import db import boto3 client = boto3.client("cognito-idp") user_pool_id = "eu-west-1_Q9RIAHBuV" tables = [ ("users_tw_influencers", "eid"), ("users_in_influencers", "inid"), ("users_sc_influencers", "scid"), ("users_spy_playlists", "spyid"), ("users_yt_channels", "ytid"), ("users_tt_influencers", "unique_id"), ] statements = [ f"""insert into {table} (username, {column}) select :username, {column} from {table} where username = :copyfrom on conflict do nothing""" for (table, column) in tables ] def run_statements(username, copyfrom): for statement in statements: db.Session.execute(statement, params=dict(username=username, copyfrom=copyfrom)) for table in tables: print( db.Session.execute( "select '{table}', count(*) from {table} where username = :username".format( table=table[0] ), params=dict(username=username), ).fetchall() ) def create_user_in_db(username, team, copy_from_username): db.Session.add(db.User(username=username, team=team)) db.Session.flush() run_statements(username, copy_from_username) db.Session.commit() def randpass(): return "".join([random.choice("89abcdefg12345jklmnzsx") for _ in range(8)]) def _old_create_user_in_cognito(username, password, team): temppass = randpass() client.admin_create_user( UserPoolId=user_pool_id, Username=username, UserAttributes=[ {"Name": "custom:team", "Value": team}, ] if team else [], TemporaryPassword=temppass, ) response2 = client.admin_initiate_auth( UserPoolId=user_pool_id, ClientId="3fqbkgoe766qdmm5nbes3fmo9h", AuthFlow="ADMIN_NO_SRP_AUTH", AuthParameters={"USERNAME": username, "PASSWORD": temppass}, ) session = response2["Session"] client.admin_respond_to_auth_challenge( UserPoolId=user_pool_id, ClientId="3fqbkgoe766qdmm5nbes3fmo9h", ChallengeName="NEW_PASSWORD_REQUIRED", ChallengeResponses={ "NEW_PASSWORD": password, "USERNAME": username, }, Session=session, ) def create_user_in_cognito_with_email(username, email, team): """UserPoolId="eu-west-1_Q9RIAHBuV", Username='joeltest1', UserAttributes=[, {'Name':'email_verified', 'Value':'true'}])""" user_attributes = [ {"Name": "email", "Value": email}, {"Name": "email_verified", "Value": "true"}, ] if team: user_attributes.append({"Name": "custom:team", "Value": team}) return client.admin_create_user( UserPoolId=user_pool_id, Username=username, UserAttributes=user_attributes, ) def create_new_user_no_email_force_password( username: str, password: str, team: str, copy_from_username: str ): """Creates a new user account. The copy_from_username will be used to seed the scouts.""" _old_create_user_in_cognito(username, password, team) create_user_in_db(username, team, copy_from_username) def create_new_user(username: str, email: str, team: str, copy_from_username: str): create_user_in_db(username, team, copy_from_username) create_user_in_cognito_with_email(username, email, team) def list_cognito_users(username_starts_with): return client.list_users( **dict( UserPoolId=user_pool_id, # AttributesToGet=['string',], Limit=60, # PaginationToken='string', Filter=f'username ^= \"{username_starts_with}\"', ) ) def delete_cognito_and_resend_email_invite(username: str, email: str, team: str): try: client.admin_get_user( UserPoolId=user_pool_id, Username=username, ) except client.exceptions.UserNotFoundException: print(f"No user with username {username} found. Not creating.") return else: client.admin_delete_user( UserPoolId=user_pool_id, Username=username, ) create_user_in_cognito_with_email(username, email, team) def print_users(): all_users = db.Session.execute( "select team, array_agg(username) from users group by team order by team" ).fetchall() header = ["Team", "Count", "Some"] formatted_users = [[r[0], len(r[1]), ", ".join(r[1][0:10])] for r in all_users] formatted_users.insert(0, header) from scripts.scheduling import ColoredTable print(ColoredTable(formatted_users, "Teams").table)