This repository has been archived by the owner on Jan 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam-menu.py
executable file
·275 lines (248 loc) · 10 KB
/
iam-menu.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/python
import sys
import subprocess
# Copied this from the internet, as Python 2.6 does not yet support check_output in module subprocess
def check_output(*popenargs, **kwargs):
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
error = subprocess.CalledProcessError(retcode, cmd)
error.output = output
#raise error
return output
# The initial filler function. Returns the main menu and the tool header.
def begin_it():
output = """
*************************************
* JD IAM-tool *
*************************************
"""
menu = """
(1) Give overview
(2) Search for members
(3) Search for users
(4) List all users with their roles
(5) Make member a user
(6) Make member a user (search by name)
(7) Remove user (demote to member)
(8) Reset password for user
(9) List all roles
(0) Assign role to user
(-) Remove role from user
(=) Quit
"""
state = "main"
return output, menu, state
# The menu runner function. Displays provided information, asks for user input.
def run_menu(output, menu):
print output
print menu
print "----------------------------------------------------------------"
user_input = raw_input("> ")
return user_input
# The processing function for state searchmembers
def process_input_searchmembers(user_input):
output = "Input not recognized, returning to main menu."
dummy, menu, state = begin_it()
if not ' ' in user_input:
return output, menu, state
input_parts = user_input.split(' ', 1)
if input_parts[0] == "1":
search_option = "-i"
elif input_parts[0] == "2":
search_option = "-n"
elif input_parts[0] == "3":
search_option = "-e"
elif input_parts[0] == "4":
search_option = "-u"
else:
return output, menu, state
output = check_output(["python", "search_users.py", "-a", search_option, input_parts[1]])
return output, menu, state
# The processing function for state searchusers
def process_input_searchusers(user_input):
output = "Input not recognized, returning to main menu."
dummy, menu, state = begin_it()
if not ' ' in user_input:
return output, menu, state
input_parts = user_input.split(' ', 1)
if input_parts[0] == "1":
search_option = "-i"
elif input_parts[0] == "2":
search_option = "-n"
elif input_parts[0] == "3":
search_option = "-e"
elif input_parts[0] == "4":
search_option = "-u"
else:
return output, menu, state
output = check_output(["python", "search_users.py", search_option, input_parts[1]])
return output, menu, state
# The processing function for state makeuser
def process_input_makeuser(user_input):
output = "Input not recognized, returning to main menu."
dummy, menu, state = begin_it()
input_parts = user_input.split(' ', 2)
if len(input_parts) < 3:
return output, menu, state
output = check_output(["python", "make_user.py", input_parts[0], input_parts[1], input_parts[2]])
return output, menu, state
# The processing function for state makeuserbyname
def process_input_makeuserbyname(user_input):
output = "Input not recognized, returning to main menu."
dummy, menu, state = begin_it()
input_parts = user_input.rsplit(' ', 2)
if len(input_parts) < 3:
return output, menu, state
output = check_output(["python", "make_user_by_name.py", input_parts[0], input_parts[1], input_parts[2]])
return output, menu, state
# The processing function for state resetpassword
def process_input_resetpassword(user_input):
output = "Input not recognized, returning to main menu."
dummy, menu, state = begin_it()
input_parts = user_input.split(' ', 1)
if len(input_parts) < 2:
return output, menu, state
output = check_output(["python", "reset_password.py", input_parts[0], input_parts[1]])
return output, menu, state
# The processing function for state removeuser
def process_input_removeuser(user_input):
output = "Input not recognized, returning to main menu."
dummy, menu, state = begin_it()
if not ' ' in user_input:
return output, menu, state
input_parts = user_input.split(' ', 1)
output = check_output(["python", "remove_user.py", input_parts[0], input_parts[1]])
return output, menu, state
# The processing function for state giverole
def process_input_giverole(user_input):
output = "Input not recognized, returning to main menu."
dummy, menu, state = begin_it()
if not ' ' in user_input:
return output, menu, state
input_parts = user_input.split(' ', 1)
output = check_output(["python", "give_role.py", input_parts[0], input_parts[1]])
return output, menu, state
# The processing function for state removerole
def process_input_removerole(user_input):
output = "Input not recognized, returning to main menu."
dummy, menu, state = begin_it()
if not ' ' in user_input:
return output, menu, state
input_parts = user_input.split(' ', 1)
output = check_output(["python", "remove_role.py", input_parts[0], input_parts[1]])
return output, menu, state
# The processing function for state main
# (the main menu)
def process_input_main(user_input):
output = ""
menu = ""
state = ""
if user_input == "1":
dummy, menu, state = begin_it()
output = check_output(["python", "generate_overview.py"])
elif user_input == "2":
output = "== Search for members =="
menu = """(1) Search for lidnummer
(2) Search for part of name
(3) Search for part of e-mail address
(4) Search for part of username
Enter the number of the option followed by your search entry, separated by a space (e.g. '2 Rogaar').
"""
state = "searchmembers"
elif user_input == "3":
output = "== Search for users =="
menu = """(1) Search for lidnummer
(2) Search for part of name
(3) Search for part of e-mail address
(4) Search for part of username
Enter the number of the option followed by your search entry, separated by a space (e.g. '2 Rogaar').
"""
state = "searchusers"
elif user_input == "4":
dummy, menu, state = begin_it()
output = check_output(["python", "list_users.py"])
elif user_input == "5":
output = "== Make member a user =="
menu = """Enter lidnummer, desired username and desired password type, separated by spaces (e.g. '12345 myuser 3').
Password type is one of the following:
0 Password contains lowercase, uppercase, digits and special characters (shortest)
1 Password contains lowercase and uppercase
2 Password contains only lowercase
3 Password consists of five concatenated Dutch words
Password entropy is always at least 50 bits. Password length is adjusted accordingly.
"""
state = "makeuser"
elif user_input == "6":
output = "== Make member a user (search by name) =="
menu = """Enter the name of the member, followed by the desired username (e.g. 'Firstname Lastname username passwordType').
Password type is one of the following:
0 Password contains lowercase, uppercase, digits and special characters (shortest)
1 Password contains lowercase and uppercase
2 Password contains only lowercase
3 Password consists of five concatenated Dutch words
Password entropy is always at least 50 bits. Password length is adjusted accordingly.
"""
state = "makeuserbyname"
elif user_input == "7":
output = "== Remove user =="
menu = "Enter lidnummer and username, separated by a space (e.g. '12345 myuser')."
state = "removeuser"
elif user_input == "8":
output = "== Reset password for a user =="
menu = """Enter lidnummer or username and desired password type (0-3) for the user (e.g. myuser 3).
Password type is one of the following:
0 Password contains lowercase, uppercase, digits and special characters (shortest)
1 Password contains lowercase and uppercase
2 Password contains only lowercase
3 Password consists of five concatenated Dutch words
Password entropy is always at least 50 bits. Password length is adjusted accordingly.
"""
state = "resetpassword"
elif user_input == "9":
dummy, menu, state = begin_it()
output = check_output(["python", "list_roles.py"])
elif user_input == "0":
output = "== Assign role to user =="
menu = "Enter role and username, separated by a space (e.g. 'role-lb myuser')."
state = "giverole"
elif user_input == "-":
output = "== Remove role from user =="
menu = "Enter role and username, separated by a space (e.g. 'role-lb myuser')."
state = "removerole"
elif user_input == "=":
state = "quit"
return output, menu, state
# Whenever there is user input, we process it
# The appropriate place to process user input depends on the state we are in
# Each state has its own processing function
def process_input(user_input, state):
if state == "main":
return process_input_main(user_input)
elif state == "searchmembers":
return process_input_searchmembers(user_input)
elif state == "searchusers":
return process_input_searchusers(user_input)
elif state == "makeuser":
return process_input_makeuser(user_input)
elif state == "makeuserbyname":
return process_input_makeuserbyname(user_input)
elif state == "resetpassword":
return process_input_resetpassword(user_input)
elif state == "removeuser":
return process_input_removeuser(user_input)
elif state == "giverole":
return process_input_giverole(user_input)
elif state == "removerole":
return process_input_removerole(user_input)
return "", "", "main"
# The main loop
if __name__ == "__main__":
output, menu, state = begin_it()
while state != "quit":
user_input = run_menu(output, menu)
output, menu, state = process_input(user_input, state)