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

Functionality to remove users from AWS User Pool #62

Merged
merged 3 commits into from
Feb 8, 2024

Conversation

nataliejschultz
Copy link
Contributor

This PR is a continuation of e-mission/e-mission-docs#1008

Copying the following from comment in #45:

I added functionality to remove users who are not in the config file, but are already in the user pool. Previously, I created the user_already_exists function:

def user_already_exists(pool_id, email, cognito_client):
    try:
        response = cognito_client.list_users(UserPoolId=pool_id)
        users = response["Users"]
        result = False
        if str(users).find(email) > 1:
            result = True
        return result
    except ClientError as err:
        logger.error(
            "Couldn't list users for %s. Here's why: %s: %s",
            pool_id,
            err.response["Error"]["Code"],
            err.response["Error"]["Message"],
        )
        raise

this function takes users, which is a list of dictionaries containing info on each user in the pool, and searches for a string matching the desired email.

For the new use case, I modified it a bit to create get_users:

def get_users(pool_id, cognito_client):
    try:
        response = cognito_client.list_users(UserPoolId=pool_id)
        return response["Users"]
    except ClientError as err:
        logger.error(
            "Couldn't list users for %s. Here's why: %s: %s",
            pool_id,
            err.response["Error"]["Code"],
            err.response["Error"]["Message"],
        )
        raise

This function is called early on in the program, and gives us the users variable:
users = get_users(pool_id, cognito_client)

Next, each dictionary in users is looped over, and the email is isolated as user_email.

    for user in users:
        for attr_dict in user["Attributes"]:
            if attr_dict["Name"] == "email":
                user_email = attr_dict["Value"]
                if user_email not in emails:
                    remove_user(pool_id, user_email)
                    print(f"{user_email} removed from pool.")

If the user_email is not in the emails list that was pulled from the config file, another new function named remove_user is called:

def remove_user(pool_id, user):
    response = cognito_client.admin_delete_user(
        UserPoolId= pool_id,
        Username= str(user)
)

I tested this by adding myself to the pool:

email1@nrel.gov not in user pool! Creating account...
Account created! Sending welcome email.

Then trying to re-add myself AND another new email,

email1@nrel.gov already in user pool!
email2@yahoo.com not in user pool! Creating account...
Account created! Sending welcome email.

And finally, removing my first email from the config to see what happens:

email1@nrel.gov removed from pool.
email2@yahoo.com already in user pool!

I checked the user pool, and my email1@nrel.gov was indeed removed from the pool! I decided to try and follow the welcome email to log in with my removed email, just to see what would happen. I got the following message:
Screenshot 2024-01-31 at 11 20 25 PM

Adding functionality to remove a user from the user pool when their email is not found in the config file.
@nataliejschultz nataliejschultz marked this pull request as ready for review February 6, 2024 21:23
@shankari shankari merged commit 2fff992 into e-mission:main Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants