@@ -91,7 +91,7 @@ def confirm_dialogue(entry: dict) -> bool:
91
91
"""Prompts the user to confirm their changes"""
92
92
print (entry )
93
93
while True :
94
- confirm = input ("Confirm(y/n) that this entry looks correct: " )
94
+ confirm = input ("Confirm that this entry looks correct (y/n) : " )
95
95
if confirm == "y" :
96
96
return True
97
97
@@ -102,22 +102,34 @@ def confirm_dialogue(entry: dict) -> bool:
102
102
print ("Enter y/n" )
103
103
104
104
105
+ def check_client_exists (db , client_id : str ) -> bool :
106
+ """Checks if client id exists in client_permissions"""
107
+ return (
108
+ db .client_permissions .count_documents (
109
+ {"client_id" : client_id }, limit = 1
110
+ )
111
+ != 0
112
+ )
113
+
114
+
105
115
def create_client_credential (db ):
106
116
"""
107
117
Creates new client_id, client_secret pair with max_priority
108
118
and allowed_queues
109
119
"""
110
120
client_id = input ("Enter the new client_id: " )
111
- if (
112
- db .client_permissions .count_documents (
113
- {"client_id" : client_id }, limit = 1
114
- )
115
- == 1
116
- ):
117
- print ("Client id already exists!" )
121
+ if check_client_exists (db , client_id ):
122
+ print ("Client id already exists!\n " )
118
123
return
119
124
120
125
client_secret = getpass .getpass ("Enter the new client_secret: " )
126
+ client_secret_confirm = getpass .getpass (
127
+ "Enter the new client_secret again: "
128
+ )
129
+ if client_secret != client_secret_confirm :
130
+ print ("New client secrets do not match!\n " )
131
+ return
132
+
121
133
client_secret_hash = bcrypt .hashpw (
122
134
client_secret .encode ("utf-8" ), bcrypt .gensalt ()
123
135
).decode ()
@@ -133,7 +145,7 @@ def create_client_credential(db):
133
145
if confirm_dialogue (entry ):
134
146
entry ["client_secret_hash" ] = client_secret_hash
135
147
db .client_permissions .insert_one (entry )
136
- print ("Entry has been created and stored in database" )
148
+ print ("Entry has been created and stored in database\n " )
137
149
138
150
139
151
def edit_client_credential (db ):
@@ -143,6 +155,10 @@ def edit_client_credential(db):
143
155
"""
144
156
145
157
client_id = input ("Enter the client_id you wish to edit: " )
158
+ if not check_client_exists (db , client_id ):
159
+ print ("Client id not in database!\n " )
160
+ return
161
+
146
162
max_priority = handle_max_priority_input ()
147
163
allowed_queues = handle_allowed_queues_input ()
148
164
confirm_output = confirm_dialogue (
@@ -163,39 +179,57 @@ def edit_client_credential(db):
163
179
},
164
180
return_document = ReturnDocument .AFTER ,
165
181
)
166
- print ("Entry updated successfully" )
182
+ print ("Entry updated successfully\n " )
167
183
168
184
169
185
def remove_client_credential (db ):
170
186
"""Removes a client_id and client_secret pair from the database"""
171
187
client_id = input ("Enter the client_id you wish to delete: " )
188
+ if not check_client_exists (db , client_id ):
189
+ print ("Client id not in database!\n " )
190
+ return
191
+
172
192
if confirm_dialogue ({"client_id" : client_id }):
173
193
db .client_permissions .delete_one ({"client_id" : client_id })
174
- print ("Entry deleted successfully" )
194
+ print ("Entry deleted successfully\n " )
195
+
196
+
197
+ def check_queue_exists (db , queue_name : str ) -> bool :
198
+ """Checks if queue is in the restricted_queues collection"""
199
+ return (
200
+ db .restricted_queues .count_documents (
201
+ {"queue_name" : queue_name }, limit = 1
202
+ )
203
+ != 0
204
+ )
175
205
176
206
177
207
def add_restricted_queue (db ):
178
208
"""Adds a restricted queue to the database"""
179
209
queue_name = input ("Enter the name of the restricted queue to add: " )
210
+ if check_queue_exists (db , queue_name ):
211
+ print ("Restricted queue already exists!\n " )
212
+ return
213
+
180
214
queue_entry = {"queue_name" : queue_name }
181
215
if confirm_dialogue (queue_entry ):
182
- queue_exists_num = db .restricted_queues .count_documents (
183
- queue_entry , limit = 1
184
- )
185
- if queue_exists_num != 0 :
186
- print ("Restricted queue already exists!" )
187
- return
188
216
db .restricted_queues .insert_one (queue_entry )
189
- print ("Restricted queue sucessfully added" )
217
+ print ("Restricted queue sucessfully added\n " )
190
218
191
219
192
220
def remove_restricted_queue (db ):
193
221
"""Removes a restricted queue from the database"""
194
- queue_name = input ("Enter the client_id you wish to delete: " )
222
+ queue_name = input (
223
+ "Enter the name of the restricted queue you wish to delete: "
224
+ )
225
+ if not check_queue_exists (db , queue_name ):
226
+ print ("Restricted queue not in database!\n " )
227
+ return
228
+
195
229
queue_entry = {"queue_name" : queue_name }
196
230
if confirm_dialogue (queue_entry ):
197
231
db .restricted_queues .delete_one (queue_entry )
198
- print ("Entry deleted successfully" )
232
+ print ("Entry deleted successfully\n " )
199
233
200
234
201
235
def main ():
@@ -205,11 +239,14 @@ def main():
205
239
"""
206
240
db = setup_database ()
207
241
while True :
208
- user_input = input (
209
- "Do you wish to create(c), edit(e), or remove(r) a client?"
210
- + " You can also add a restricted queue(aq)"
211
- + " or remove a restricted queue(rq). Enter q to quit: "
212
- )
242
+ print ("(c) Create client" )
243
+ print ("(e) Edit client" )
244
+ print ("(r) Remove client" )
245
+ print ("(aq) Add restricted queue" )
246
+ print ("(rq) Remove restricted queue" )
247
+ print ("(q) Quit" )
248
+
249
+ user_input = input ("Enter your selection: " )
213
250
if user_input == "c" :
214
251
create_client_credential (db )
215
252
elif user_input == "e" :
@@ -224,7 +261,8 @@ def main():
224
261
sys .exit ()
225
262
else :
226
263
print (
227
- "Invalid selection. Please enter 'c', 'e', 'r', 'aq', or 'rq'"
264
+ "Invalid selection. Please enter "
265
+ + "'c', 'e', 'r', 'aq', 'rq', or 'q'\n "
228
266
)
229
267
230
268
0 commit comments