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

Bug fix: admin users with duplicate email #445

Merged
merged 1 commit into from
Jan 9, 2024
Merged
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
31 changes: 17 additions & 14 deletions api/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,44 @@
import asyncio
import argparse
import sys

import getpass
import pymongo

from .auth import Authentication
from .db import Database
from .user_models import User


async def setup_admin_user(db, username, email):

Check warning on line 23 in api/admin.py

View workflow job for this annotation

GitHub Actions / Lint

Argument name "db" doesn't conform to snake_case naming style
"""Create an admin user"""
user_obj = await db.find_one_by_attributes(User,
{'username': username})
if user_obj:
print(f"User {username} already exists, aborting.")
print(user_obj.json())
return None
password = getpass.getpass(f"Password for user '{username}': ")
retyped = getpass.getpass(f"Retype password for user '{username}': ")
if password != retyped:
print("Sorry, passwords do not match, aborting.")
return None
hashed_password = Authentication.get_password_hash(password)
print(f"Creating {username} user...")
return await db.create(User(
username=username,
hashed_password=hashed_password,
email=email,
is_superuser=1,
is_verified=1,
))
try:
return await db.create(User(
username=username,
hashed_password=hashed_password,
email=email,
is_superuser=1,
is_verified=1,
))
except pymongo.errors.DuplicateKeyError as exc:
err = str(exc)
if "username" in err:
print(f"User {username} already exists, aborting.")
elif "email" in err:
print(f"User with {email} already exists, aborting.")
return None


async def main(args):

Check warning on line 49 in api/admin.py

View workflow job for this annotation

GitHub Actions / Lint

Missing function or method docstring

Check warning on line 49 in api/admin.py

View workflow job for this annotation

GitHub Actions / Lint

Redefining name 'args' from outer scope (line 67)
db = Database(args.mongo, args.database)

Check warning on line 50 in api/admin.py

View workflow job for this annotation

GitHub Actions / Lint

Variable name "db" doesn't conform to snake_case naming style
await db.initialize_beanie()
await db.create_indexes()
await setup_admin_user(db, args.username, args.email)
return True

Expand Down
Loading