-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
140 lines (117 loc) · 4.06 KB
/
auth.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
import os
class DoorUser(object):
def __init__(self, name):
self.username = name
self.pin = None
def addPin(self, pin):
if len(str(int(pin))) >= 4:
self.pin = int(pin)
print("{}:Meets requirements".format(self.username))
else:
print("{}:Pin does not meet requirements".format(self.username))
print("{}:Pin should be 4 or more digits".format(self.username))
raise Exception
class ConfigFile(object):
def __init__(self, filepath):
self.users = []
self.filepath = filepath
if os.path.exists(filepath):
with open(filepath) as openfile:
for line in openfile:
splitline = line.split(',')
newuser = DoorUser(splitline[0])
newuser.addPin(splitline[1])
self.users.append(newuser)
def writefile(self):
with open(self.filepath, mode='w') as writefile:
for user in self.users:
writefile.write("{},{}\n".format(user.username, user.pin))
def list_users(self):
list=[]
for user in self.users:
list.append(user.username)
return list
def list_pins(self):
list=[]
for user in self.users:
list.append(user.pin)
return list
def getuserfrompin(self, pin):
for user in self.users:
if user.pin == int(pin):
return user
def getuserfromname(self, name):
for user in self.users:
if user.username == name:
return user
def add_user(self, name, pin):
if name not in self.list_users():
newuser = DoorUser(name)
try:
newuser.addPin(pin)
except:
"User was not added!"
return
self.users.append(newuser)
self.writefile()
else:
print("User is already in the database")
def remove_user(self, name):
self.users.remove(self.getuserfromname(name))
self.writefile()
def change_pin(self, name, pin):
for user in self.users:
if user.name == name:
user.pin = user.addPin(pin)
else:
print("User is not in the list!")
def print_users(self):
print("USERS ARE:")
for user in self.users:
print (str(user.username)+":"+str(user.pin))
def cleardata(self):
if os.path.exists(self.filepath):
os.remove(self.filepath)
self.users = []
def checkpin_from_dict(self, dict):
return self.checkpin(dict['pin_num'])
def checkpin(self, checkpin):
if int(checkpin) in self.list_pins():
user = self.getuserfrompin(int(checkpin))
print("Opening door: welcome {}".format(user.username))
return 1, user.username
else:
print("Sorry you aren't welcome here")
return 0, ""
## Testing
def main():
# configfile.cleardata()
# configfile.print_users()
# configfile.checkpin(7896)
# configfile.checkpin()
configfile = ConfigFile("userdata.txt")
print ("Welcome to the administration interface for auth module V1.0")
while (1):
command = raw_input()
if command == 'n':
new_username = raw_input("Please enter a username:")
new_pin = raw_input("Please enter a pin")
configfile.add_user(new_username, new_pin)
if command == 'r':
user_to_remove = raw_input("Remove user:")
configfile.remove_user(user_to_remove)
if command == 'l':
while (1):
checkpin = raw_input("What's your pin?")
if checkpin != '':
configfile.checkpin(checkpin)
else:
break
if command == 'e':
break
if command == 'list':
configfile.print_users()
else:
print("You can add_new(n), list(list) or login(l)")
if __name__ == "__main__":
main()