-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
164 lines (155 loc) · 5.68 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
# Author : Daniel Zhou
# Function : Program's central function
import sqlite3
import datetime
import base64
import getpass # Used to hide passwords on input prompts
import hashlib
from DoctorModule import *
from NurseModule import *
from AdminModule import *
# Login Text
def login_menu():
print ("\n")
print("CMPUT 291 - Mini Project 1")
print("Hospital Database System")
print("====")
print("A - Login as Existing User")
print("B - Create New User")
print("C - Exit")
print("====")
print("Choose an option by typing in the corresponding letter.")
# Handles user menu choice
while True:
usr_input = raw_input("OPTION> ")
if usr_input.upper() in ('A','B', 'C'):
return usr_input.upper()
else:
print ("Input value not valid, please retry your option.")
continue
# Make database connection
def create_Connection(dblink):
while True:
try:
conn = sqlite3.connect(dblink)
return conn
except expression as identifier:
print ("The following database could not be found locally")
print (dblink)
print (identifier)
dblink = input("Please input the path to the SQLite 3 DB: ")
# Login Functions
def login_main(conn):
while True:
# Get user input
print("Please provide a username and password.")
username = raw_input("Username: ")
password = str(getpass.getpass("Password (Input Hidden): "))
# Encrypt the password, such that we can compare against the encrypted password.capitalize
password = hashlib.sha224(password).hexdigest()
# Query database for matching profiles.
cursor = conn.cursor()
cursor.execute("SELECT staff_id, name, role, login FROM staff WHERE login = ? AND password = ?", (username,password))
# Allow user to select the login they require.
for row in cursor:
print ("Staff_id: " + str(row[0]) )
print ("Name: " + str(row[1]) )
print ("Role: " + str(row[2]) )
print ("Login: " + str(row[3]) )
while True:
print ("Use this account?")
user_sel = raw_input("OPTION (Y/N)>").upper()
if user_sel == "Y":
return [str(row[0]), str(row[1]), str(row[2]), str(row[3])]
elif user_sel == "N":
break
else:
print ("Input Unrecognized.")
# Should only run if login is unrecognized.
print ("Login Unknown / Invalid or reached end of accounts found.")
print ("Please retry.")
continue
return 0
def login_create(conn):
# Disalllows access to most people.
if "DEMO123" != str(getpass.getpass("Provide Key Phrase: ")):
return 0
print("Please provide a username and password to create a new account.")
# Basic Identification Information
username = raw_input("Username: ")
name = raw_input("Name: ")
# Role Selection
while True:
role = raw_input("Role (D/N/A): ")
if role.upper() in ('D','N','A'):
role = str(role.upper())
break
else:
print("Please select fron the valid roles of (D/N/A)")
continue
# Password Selection with Confirmation
while True:
password = str(getpass.getpass("Password: "))
print ("Please confirm your password (Input Hidden):")
password_confirmation = str(getpass.getpass("Password: "))
if password == password_confirmation:
# Encryption
password = hashlib.sha224(password).hexdigest()
# Find a suitable ID for this entry
query = "SELECT MAX(staff_id) FROM staff"
cursor = conn.cursor()
cursor.execute(query)
ID = int(cursor.fetchone()[0]) + 1
# Execute the query to create new account
Values = (str(ID),str(role),str(name),str(username),str(password))
cursor.execute("INSERT INTO staff values (?,?,?,?,?)",Values)
conn.commit()
print ("Account Creation Sucessful")
break
else:
print ("Password and Password Confirmation were not the same.")
print ("Please retry the Password and Password Confirmation prompt.")
continue
return 0
# Basic Encryption and Decryption, requires base64
def encode(key, clear):
enc = []
for i in range(len(clear)):
key_c = key[i % len(key)]
enc_c = chr((ord(clear[i]) + ord(key_c)) % 256)
enc.append(enc_c)
return base64.urlsafe_b64encode("".join(enc))
def decode(key, enc):
dec = []
enc = base64.urlsafe_b64decode(enc)
for i in range(len(enc)):
key_c = key[i % len(key)]
dec_c = chr((256 + ord(enc[i]) - ord(key_c)) % 256)
dec.append(dec_c)
return "".join(dec)
#
# Main
#
def main():
# Create Inital Database Connection
conn = create_Connection("hospital.db")
# Allow the user to select their inital task
while True:
usr_input = login_menu()
if usr_input == 'A':
staff_id,name,role,username = login_main(conn)
if role == "D":
DOC(conn,staff_id,name)
elif role == "N":
NUR(conn,staff_id,name)
elif role == "A":
ADM(conn,staff_id,name)
else:
print ("Role not recognized")
elif usr_input == 'B':
login_create(conn)
elif usr_input == 'C':
break
# Get associated information, connect to module.
if __name__ == "__main__":
main()