-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_users.py
executable file
·53 lines (43 loc) · 1.29 KB
/
create_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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import getpass
import requests
print("Login with your IDM admin username and password. Press CTRL + C to abort.")
host = input("IDM Host [localhost:9096]: ")
if not host:
host = "http://localhost:9096"
token = None
# log in
while not token:
try:
username = input("Admin username: ")
password = getpass.getpass("Admin password: ")
# get token
res = requests.post(host+'/auth', json={'username': username, 'password': password})
res.raise_for_status()
js = res.json()
token = js.get('access_token')
except Exception as e:
print("Failed: {}".format(e))
# create user
abort = False
while not abort:
print()
print("Create a new user")
newname = input("Username (email address): ")
newpass = getpass.getpass("Password: ")
isadmin = input("Is this an admin user? [y/N]: ")
# create user
try:
data = {'username': newname, 'password': newpass}
if isadmin and 'y' == isadmin[:1].lower():
data['admin'] = True
res = requests.post(host+'/user', json=data, headers={'Authorization': 'JWT {}'.format(token)})
res.raise_for_status()
print("Added")
nextone = input("Add another user? [y/N]: ")
if not nextone or 'n' == nextone[:1].lower():
abort = True
except Exception as e:
print("Failed: {}".format(e))
print("Done")