-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
356 lines (275 loc) · 9.01 KB
/
main.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
from os import system, name
from time import sleep
import datetime
import sys
import ibm_db, ibm_db_dbi
import pandas as pd
import re
# Dsn Variables
dsn_uid = 'YOUR_USER_ID'
dsn_pwd = 'YOUR_PASSWORD'
dsn_hostname = 'dashdb-txn-sbox-yp-dal09-10.services.dal.bluemix.net'
dsn_driver = '{IBM DB2 ODBC DRIVER}'
dsn_database = 'BLUDB'
dsn_port = '50000'
dsn_protocol = 'TCPIP'
# Dsn string
dsn = (
"DRIVER={0};"
"DATABASE={1};"
"HOSTNAME={2};"
"PORT={3};"
"PROTOCOL={4};"
"UID={5};"
"PWD={6};").format(dsn_driver, dsn_database, dsn_hostname, dsn_port, dsn_protocol, dsn_uid, dsn_pwd)
# Trying to connect
system("mode 77, 26")
try:
conn = ibm_db.connect(dsn, "", "")
print('\n' * 12,"Connected to the database!".center(76))
sleep(1)
except:
print('\n' * 11, "Unable to connect!".center(76))
print("Check your connection or try again later!".center(76))
sleep(2)
sys.exit()
def clear():
# for windows
if name == 'nt':
_ = system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = system('clear')
def print_intro(txt, size=38):
clear()
print('=-'*size, end='\n\n')
print(f"{txt}".center(size*2 - 1), end='\n\n')
print('-='*size)
def print_credits():
print('\n' * 5)
print('-='*38)
print("Thanks for using my program!".rjust(76))
print("Follow me on GitHub: @mateusvictor".rjust(76))
def read_integer(text):
count = 0
while count < 2:
try:
integer = int(input(text))
except:
print(" Some error ocurred! Please try again.\n")
count += 1
continue
else:
return integer
break
return -1
def read_option(options, text=' ==> '):
count = 0
while count < 2:
chosen_option = read_integer(text)
if chosen_option == -1:
break
if chosen_option in options:
return chosen_option
else:
print(f" Please, only numbers between {options[0]} and {options[-1]}\n")
count += 1
continue
return False
def current_datetime():
current_datetime = datetime.datetime.now().strftime("%d-%m-%Y %H:%M:%S")
return current_datetime
def is_user(user):
where_query = f"SELECT * FROM USERS WHERE USERNAME = '{user}'"
where_stmt = ibm_db.exec_immediate(conn, where_query)
return ibm_db.fetch_both(where_stmt) != False
def is_email(email):
where_query = f"SELECT * FROM USERS WHERE EMAIL = '{email}'"
where_stmt = ibm_db.exec_immediate(conn, where_query)
return ibm_db.fetch_both(where_stmt) != False
def new_id():
year_month = datetime.datetime.now().strftime("%Y%m")
max_id_query = "SELECT MAX(ID) FROM USERS"
max_id_stmt = ibm_db.exec_immediate(conn, max_id_query)
# Select only the 4 last characters from the max id
max_id = str(ibm_db.fetch_both(max_id_stmt)[0])
max_id = str(int(max_id[7:]))
new_id = int(f"{year_month}{max_id.zfill(6)}") + 1
return new_id
def check_email(email):
regex_email = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
if re.search(regex_email, email):
return True
return False
def validate_credentials(user, key='', email=''):
invalid_chars = '\'\"´~^`;'
if len(user) < 5 or len(user) > 100:
return False
# Validating email
if email != '':
if check_email(email) == False or len(email) > 100:
return False
# Validating user
for c in user:
if c in invalid_chars or c == ' ':
return False
# Validating password
if key != '':
if len(key) < 4 or len(key) > 100:
return False
for c in key:
if c in invalid_chars or c == ' ':
return False
return True
def update_last_login(user):
update_query = f"UPDATE USERS SET LAST_LOGIN = ? WHERE USERNAME = ? OR EMAIL = ?"
update_stmt = ibm_db.prepare(conn, update_query)
ibm_db.bind_param(update_stmt, 1, current_datetime())
ibm_db.bind_param(update_stmt, 2, user)
ibm_db.bind_param (update_stmt, 3, user)
ibm_db.execute(update_stmt)
def login_authenticator(user, key):
if validate_credentials(user, key) == False:
return False
where_query = f"SELECT * FROM USERS WHERE (USERNAME = ? OR EMAIL = ?) AND PASSWORD = ?;"
where_stmt = ibm_db.prepare(conn, where_query)
ibm_db.bind_param(where_stmt, 1, user)
ibm_db.bind_param(where_stmt, 2, user)
ibm_db.bind_param(where_stmt, 3, key)
ibm_db.execute(where_stmt)
result = ibm_db.fetch_both(where_stmt)
if result != False:
update_last_login(user)
return True
else:
return False
def register_user(user, key, email):
if validate_credentials(user, key, email) == False:
return False
if is_user(user) or is_email(email):
return False
insert_query = f"INSERT INTO USERS VALUES (?, ?, ?, ?, ?)"
insert_stmt = ibm_db.prepare(conn, insert_query)
ibm_db.bind_param(insert_stmt, 1, new_id())
ibm_db.bind_param(insert_stmt, 2, email)
ibm_db.bind_param(insert_stmt, 3, user)
ibm_db.bind_param(insert_stmt, 4, key)
ibm_db.bind_param(insert_stmt, 5, current_datetime())
ibm_db.execute(insert_stmt)
return True
def print_users():
select_query = "SELECT ID, USERNAME, LAST_LOGIN FROM USERS"
pandas_conn = ibm_db_dbi.Connection(conn)
dataframe = (pd.read_sql(select_query, pandas_conn).to_string()).split('\n')
print()
for line in dataframe:
print(f"{line}".center(99))
def delete_user(user):
if validate_credentials(user) == False:
return False
if is_user(user) == False:
return False
delete_query = "DELETE FROM USERS WHERE USERNAME = ?"
delete_stmt = ibm_db.prepare(conn, delete_query)
ibm_db.bind_param(delete_stmt, 1, user)
ibm_db.execute(delete_stmt)
return True
menu = '''
[ 1 ] - Login
[ 2 ] - Register a new user
[ 3 ] - See all users*
[ 4 ] - Delete a user*
[ 5 ] - Exit
* require ADM credentials
'''
while True:
system("mode 77, 26")
print_intro('Login System')
print(menu)
choice = read_option([n for n in range(1, 5 + 1)])
if choice == False:
continue
if choice == 1:
print_intro('[ 1 ] Login Mode')
print('\n Type a user and a password existent.\n')
user_login = str(input(' User or email: ')).strip()
password_login = str(input(' Password: ')).strip()
print_intro('[ 1 ] Login Mode')
print('\n Type a user and a password existent.\n')
print(f' User or email: {user_login}')
print(f' Password: {(len(password_login))*"*"}')
print('\n Verifying...\n')
sleep(0.5)
if login_authenticator(user_login, password_login):
print(' Success: Congratulations! You are registered in our database!')
else:
print(' Erro: User or password invalid! Try again or register a new user!')
print('\n '*10, end='')
if choice == 2:
print_intro('[ 2 ] Register Mode')
print('\n Type a new user, email and a password.\n')
email_register = str(input(' Email: ')).strip().lower()
user_register = str(input(' User: ')).strip()
password_register = str(input(' Password: ')).strip()
print_intro('[ 2 ] Register Mode')
print('\n Type a new user, email and a password.\n')
print(f' Email: {email_register}')
print(f' User: {user_register}')
print(f' Password: {(len(password_register))*"*"}')
print('\n Registering...\n')
sleep(0.5)
if register_user(user_register, password_register, email_register):
print(f" Success: User '{user_register}' successfully registered!\n\n\n\n")
else:
print(f" Erro: Invalid or in use credentials")
print(f" User and password must have between 5 and 100 characters.")
print(f" User and password can not have spaces or the characters: [' \" ´ ~ ` ^ ;]")
print(f"\n Try again with a new and valid username, email and password.")
print('\n '*5, end='')
if choice == 3:
print_intro('[ 3 ] ADM Mode')
print('\n Type the user and the password.\n')
user_adm = str(input(' User (ADM): ')).strip()
password_adm = str(input(' Password (ADM): ')).strip()
print_intro('[ 3 ] ADM Mode')
print('\n Type the user and the password.\n')
print(f' User (ADM): {user_adm}')
print(f' Password (ADM): {(len(password_adm))*"*"}')
print('\n Verifying...\n')
sleep(1)
if user_adm == dsn_uid and password_adm == dsn_pwd:
system("mode 99, 99")
print_intro('[ 3 ] ADM Mode', 49)
print_users()
print('\n ', end='')
else:
print(' Access Denied!')
print('\n '*10, end='')
if choice == 4:
print_intro('[ 3 ] ADM Mode')
print('\n Type the user and the password.\n')
user_adm = str(input(' User (ADM): ')).strip()
password_adm = str(input(' Password (ADM): ')).strip()
print_intro('[ 3 ] ADM Mode')
print('\n Type the user and the password.\n')
print(f' User (ADM): {user_adm}')
print(f' Password (ADM): {(len(password_adm))*"*"}')
print('\n Verifying...\n')
sleep(1)
if user_adm == dsn_uid and password_adm == dsn_pwd:
print_intro('[ 4 ] ADM Mode')
print('\n Type the username you want to delete from the database.\n')
user_delete = str(input(' User to delete: '))
if delete_user(user_delete):
print(f"\n Sucess: User '{user_delete}' deleted successfully!")
else:
print(f"\n Erro: No user '{user_delete}' registered in our database!")
print('\n '*13, end='')
else:
print(' Access Denied!')
print('\n '*10, end='')
if choice == 5:
print_credits()
ibm_db.close(conn)
break
system('pause')