-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
326 lines (295 loc) · 11 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
from flask import Flask, session, redirect, url_for, render_template, request, json
import os
import time
import sqlite3
import hashlib
import userModule
import devicesModule
app = Flask(__name__)
app.secret_key = os.urandom(24)
@app.route('/_stuff' , methods = ['GET'])
def stuff():
data = devicesModule.checkOnline()
result = app.response_class(
response=json.dumps(data),
status=200,
mimetype='application/json'
)
return result
def checkIfUserExist(uId , uPwdHash):
conn = sqlite3.connect('database')
c = conn.cursor()
res = c.execute("SELECT * FROM users WHERE userId=(?)",[uId])
for i in res:
if uPwdHash in i:
return True , i[3]
return False , 0
#Main Login screen of NMS
@app.route('/')
def index():
return render_template('index.html')
#Dashboard of whole web app to navigate
@app.route('/dashBoard')
def dashBoard():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
return render_template('dashboard.html',userName = session['userId'] , userType = session['uType'])
#Login Event
@app.route('/login' , methods=['POST'])
def login():
uId = request.form['uId']
uPwd = request.form['uPwd']
uPwdHash = hashlib.sha384(uPwd.encode()).hexdigest()
result,uType = checkIfUserExist(uId,uPwdHash)
if result:
session['userId'] = uId
session['uType'] = uType
return redirect(url_for('dashBoard'))
else:
return redirect(url_for('index' , wrongCredentials = True))
#Logout event
@app.route('/logout')
def logout():
session.pop('userId' , None)
session.pop('uType' , None)
return redirect(url_for('index'))
#Manage Users DashBoard
@app.route('/users')
def users():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
return render_template('users.html',userName = session['userId'])
#Add new User in database
@app.route('/addNewUser' , methods=['POST'])
def addNewUser():
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
uId = request.form['uId']
uPwd = request.form['uPwd']
uType = request.form['uType']
status = userModule.addUser(uId,uPwd,uType)
if status:
return render_template('users.html', addedNew = True)
else:
return render_template('users.html', addedNew = False)
#Add new user web interface
@app.route('/addUser')
def addUser():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
return render_template('addUser.html',userName = session['userId'])
#Delete user Interface
@app.route('/deleteUser')
def deleteUser():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
allUsers = userModule.fetchUserList()
return render_template('deleteUser.html',userList = allUsers)
#Delete User Activity
@app.route('/delete/<id>')
def delete(id):
result = userModule.deleteUser(id)
if result:
return render_template('users.html' , isDeleted = result)
else:
return render_template('users.html' , isDeleted = False)
#View User interface
@app.route("/viewUsers")
def viewUsers():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
allUsers = userModule.fetchUserList()
return render_template('viewUsers.html',userList = allUsers)
#Change User Interface
@app.route('/changeUser')
def changeUser():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
allUsers = userModule.fetchUserList()
return render_template('changeUser.html',userList = allUsers)
#update userType activty
@app.route('/updateUser/<id>' , methods =['POST'])
def updateUser(id):
res = userModule.updateUser(request.form['newType'],id)
if res:
return render_template('users.html' , isUpdated = res)
else:
return render_template('users.html' , isUpdated = False)
@app.route('/devices')
def devices():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
return render_template('manageDevices.html')
#Add New Device Interface
@app.route('/addDevice')
def addDevice():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
deviceTypeList = devicesModule.fetchDeviceTypeList()
return render_template('addDevice.html' , dTypeList = deviceTypeList)
#Add new Device Interface
@app.route('/addDeviceType')
def addDeviceType():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
deviceTypeList = devicesModule.fetchDeviceTypeList()
return render_template('addDeviceType.html',deviceList = deviceTypeList)
#Add new Device Activity
@app.route('/addNewDeviceActivity',methods=['POST'])
def addNewDeviceActivity():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
dType = request.form['dType']
print(dType)
res = devicesModule.addDeviceType(dType)
if res == True:
return redirect(url_for('addDeviceType',status="Added"))
else:
return redirect(url_for('addDeviceType',status="NotAdded"))
#Delete Device Type Activity
@app.route('/deleteDeviceType/<id>')
def deleteDeviceType(id):
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
res = devicesModule.deleteDeviceType(id)
if res == True:
return redirect(url_for('addDeviceType',status="deleted"))
else:
return redirect(url_for('addDeviceType',status="notDeleted"))
#Add new network device activity
@app.route('/addNewNetworkDeviceActivity' , methods= ['POST'])
def addNewNetworkDeviceActivity():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
dIp = request.form['dIp']
dType = request.form['dType']
dName = request.form['dName']
dParent = request.form['dParent']
res = devicesModule.addDevice(dIp,dName,dType,dParent)
return redirect(url_for('addDevice',deviceAdded=str(res)))
#View all network device List
@app.route('/viewDevices')
def viewDevices():
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
deviceList = devicesModule.fetchDeviceList()
return render_template('viewDevices.html',device = deviceList)
#Delete network device from network device list
@app.route('/deleteNetworkDevice/<id>')
def deleteNetworkDevice(id):
if 'userId' not in session:
return redirect(url_for('index' , isUser = False))
try:
if session['userId'] == None:
return redirect(url_for('index' , isUser = False))
except:
return redirect(url_for('index' , isUser = False))
if session['uType'] != "admin":
return redirect(url_for('dashBoard'))
res = devicesModule.deleteDevice(id)
return redirect(url_for('viewDevices',deleted=str(res)))
#Create database at __init__
def createDB():
conn = sqlite3.connect('database')
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS users(ID INTEGER PRIMARY KEY AUTOINCREMENT , userId TEXT NOT NULL UNIQUE , userPass TEXT NOT NULL , userAuth TEXT NOT NULL)')
c.execute('CREATE TABLE IF NOT EXISTS deviceList(ID INTEGER PRIMARY KEY AUTOINCREMENT , deviceIp TEXT NOT NULL UNIQUE , deviceName TEXT NOT NULL , deviceType TEXT NOT NULL,parent TEXT NOT NULL)')
c.execute('CREATE TABLE IF NOT EXISTS deviceType(ID INTEGER PRIMARY KEY AUTOINCREMENT , deviceTypeName TEXT NOT NULL UNIQUE)')
conn.commit()
#Error Handler if Page not found
@app.errorhandler(404)
def page_not_found(e):
return 'page under Development right now.'
#main Diver of program
if __name__ == "__main__":
createDB()
app.run(debug = True)