-
Notifications
You must be signed in to change notification settings - Fork 34
/
mlapi_dbuser.py
65 lines (54 loc) · 1.98 KB
/
mlapi_dbuser.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 tinydb import TinyDB, Query, where
from passlib.hash import sha256_crypt
import modules.common_params as g
import getpass
import modules.db as Database
import argparse
ap = argparse.ArgumentParser()
ap.add_argument('-u', '--user', help='username to create')
ap.add_argument('-p', '--password', help='password of user')
ap.add_argument('-d', '--dbpath', default='./db', help='path to DB')
ap.add_argument('-f', '--force', help='force overwrite user', action='store_true')
ap.add_argument('-l', '--list', help='list all users', action='store_true')
ap.add_argument('-r', '--remove', help='remove user' )
args, u = ap.parse_known_args()
args = vars(args)
g.config['db_path']= args.get('dbpath')
db = Database.Database(prompt_to_create=False)
if args.get('list'):
print ('----- Configured users ---------------')
for i in db.get_all_users():
print ('User: {}'.format(i.get('name')))
exit(0)
if args.get('remove'):
u = args.get('remove')
if not db.get_user(u):
print ('User: {} not found'.format(u))
else:
db.delete_user(args.get('remove'))
print ('OK')
exit(0)
if not args.get('user') or not args.get('password'):
print ('--------------- User Creation ------------')
while True:
name = input ('\nuser name (Ctrl+C to exit):')
if not name:
print ('Error: username needed')
continue
p1 = getpass.getpass('Please enter password:')
if not p1:
print ('Error: password cannot be empty')
continue
p2 = getpass.getpass('Please re-enter password:')
if p1 != p2:
print ('Passwords do not match, please re-try')
continue
break
else:
name = args.get('user')
p1 = args.get('password')
if not db.get_user(name) or args.get('force'):
db.add_user(name,p1)
print ('User: {} created'.format(name))
else:
print ('User: {} already exists. Use --force to override or --remove to remove old user first'.format(name))