-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage_users.py
65 lines (53 loc) · 2.27 KB
/
manage_users.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from typing import List
from talen.dal.mongo_dal import MongoDAL
from talen.models.user import User
from talen.config import Config
import argparse
from pymongo.errors import DuplicateKeyError
def add_user(mongo_dal: MongoDAL, user: User):
try:
mongo_dal.add_user(user)
except DuplicateKeyError:
print("User with that username already exists in the db")
def update_user(mongo_dal: MongoDAL, user: User):
delete_user(mongo_dal, user.id)
add_user(mongo_dal, user)
def delete_user(mongo_dal: MongoDAL, username: str):
user = mongo_dal.load_user(username)
if not user:
print(f"User {username} not found!")
elif input(f"Please confirm deletion of user: {username}: [y/n]: ") == "y":
mongo_dal.delete_user(username)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("command", help="Whether to add, update, or delete", choices=["add", "update", "delete"], type=str)
parser.add_argument("--username", "-u", help="Username", required=True, type=str)
parser.add_argument("--password", "-p", help="Password", type=str)
parser.add_argument("--email", "-m", help="Email", type=str)
parser.add_argument("--admin", action="store_true", default=False)
parser.add_argument("--read-only", action="store_true", default=False)
parser.add_argument("--environment", "-e", help="Which environment to use", choices=["dev", "prod"], default="dev")
args = parser.parse_args()
config = Config(args.environment)
mongo_dal = MongoDAL(config.mongo_url)
# slightly awkward way of doing things... we set the password later
password_hash = None
admin = args.admin
readonly = args.read_only
user = User(args.username, args.email, password_hash, admin, readonly)
if args.password:
user.set_password(args.password)
if args.command == "add":
if(not args.password or not args.email):
print("Password and Email are required!")
else:
add_user(mongo_dal, user)
elif args.command == "update":
update_user(mongo_dal, user)
pass
elif args.command == "delete":
# All this needs is the username
delete_user(mongo_dal, args.username)
pass
else:
print("Shouldn't ever get here!")