forked from jabatrox/faceSec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessmanager.py
130 lines (104 loc) · 3.44 KB
/
accessmanager.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
'''
Reads and writes from the access.json file, containing the database of admin
users and of granted users (authorized to access).
The JSON file has the following format, and any new user/admin should be
added accordingly:
{
"admins": [
{
"googleID": "",
"last-access-human": "",
"last-access-timestamp": "",
"name": ""
}
],
"granted": [
{
"CWID": "",
"cardCode": "",
"cardID": "",
"facilityCode": "",
"last-access-human": "",
"last-access-timestamp": "",
"name": ""
}
]
}
'''
import json
from datetime import datetime
def loadJsonAccessFile(accessFile):
'''
Load the JSON file containing the access data for admins and users.
:param `accessFile`: the JSON file to be loaded.\n
:return The `JSON data` loaded.
'''
try:
with open(accessFile, 'r', encoding='utf-8-sig') as json_file:
json_text = json_file.read()
return json.loads(json_text)
except IOError as e:
# Does not exist or no read permissions for the JSON access file
print("\n[ERROR] Unable to open file "+str(accessFile))
sys.exit(1)
def reloadJsonAccessFile(accessFile):
global accessData
accessData = loadJsonAccessFile(accessFile)
def getAllGrantedCardIDs():
granted_cards = []
for user in accessData["granted"]:
granted_cards.append(user["cardID"])
return granted_cards
def getAllGrantedCWIDs():
granted_CWIDs = []
for user in accessData["granted"]:
granted_CWIDs.append(user["CWID"])
return granted_CWIDs
def getGrantedName(cardID):
for user in accessData["granted"]:
if user["cardID"] == cardID:
return user["name"]
def setGrantedLastAccess(cardID, time):
with open(accessFile, 'r', encoding='utf-8-sig') as json_file:
json_text = json_file.read()
data = json.loads(json_text)
for user in data["granted"]:
if user["cardID"] == cardID:
timeHuman = time.strftime("%Y-%m-%d_%H%M%S.%f")
user["last-access-human"] = timeHuman
user["last-access-timestamp"] = datetime.timestamp(time)
with open(accessFile, "w") as jsonFile:
json.dump(data, jsonFile, indent=4, sort_keys=True)
reloadJsonAccessFile(accessFile)
def getCWIDFromCardID(cardID):
for user in accessData["granted"]:
if user["cardID"] == cardID:
return user["CWID"]
# def isGrantedCWID(cwid):
# for user in accessData["granted"]:
# if user["CWID"] == cwid:
# return True
# return False
def getAllAdminIDs():
admin_users = []
for admin in accessData["admins"]:
admin_users.append(admin["googleID"])
return admin_users
def getAdminName(googleID):
for admin in accessData["admins"]:
if admin["googleID"] == googleID:
return admin["name"]
def setAdminLastAccess(googleID, time):
with open(accessFile, 'r', encoding='utf-8-sig') as json_file:
json_text = json_file.read()
data = json.loads(json_text)
for admin in data["admins"]:
if admin["googleID"] == googleID:
timeHuman = time.strftime("%Y-%m-%d_%H%M%S.%f")
admin["last-access-human"] = timeHuman
admin["last-access-timestamp"] = datetime.timestamp(time)
with open(accessFile, "w") as jsonFile:
json.dump(data, jsonFile, indent=4, sort_keys=True)
reloadJsonAccessFile(accessFile)
accessFile = "access.json"
accessData = loadJsonAccessFile(accessFile)