-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweb.py
448 lines (370 loc) · 12.5 KB
/
web.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import datetime
import hashlib
import json
import logging
import os
from urllib.request import urlopen # Python 3
# import urllib2 # Python 2
# from sets import Set # Built in with Python 3
from flask import Flask, render_template, session, request, abort, redirect, url_for, send_from_directory
from dotenv import load_dotenv
load_dotenv('./.env')
app = Flask(__name__)
app.secret_key = os.getenv("SECRET_KEY")
def api_login(username, password):
url = os.getenv("API_URL")
parameters = "?app=" + os.getenv("APP_KEY")
md5 = hashlib.md5(password).hexdigest()
login_parameters = "&username=" + username + "&password=" + md5
request_url = "" + url + parameters + login_parameters
try:
r = urlopen(request_url)
except:
return False
data = json.loads(r.read())
if 'error' in data:
return False
else:
return True
def check_login():
if os.getenv("AUTH_EN") and os.getenv("AUTH_EN") == 'True':
if 'username' in session:
return True
else:
return False
else:
session['username'] = "dummy"
session['admin'] = False
return True
def check_username(username):
with open('users.json') as f:
data = json.load(f)
result = False
for d in data:
if d['username'] == username:
result = True
return result
def check_admin(username):
with open('users.json') as f:
data = json.load(f)
result = False
for d in data:
if d['username'] == username:
result = d['isAdmin']
if result == "False":
return False
else:
return True
def load_users():
try:
with open('users.json') as f:
data = json.load(f)
except:
print("Failed to open users file.")
return data
def write_users(data):
with open('users.json', 'w') as f:
json.dump(data, f)
def add_user(username, is_admin):
print("Added user " + username + " which is admin: " + is_admin)
a_dict = {"username": username, "isAdmin": is_admin, "channels":[{"channel":"general", "admin":False}]}
data = load_users()
data.append(a_dict)
write_users(data)
def get_all_users():
with open('users.json') as f:
data = json.load(f)
return data
def get_user(username):
allusers = load_users()
i=0
looking=True
result=None
for user in allusers:
if user['username'] == username:
result=allusers[i]
break
else:
i=i+1
return result
def find_user_id(username):
allusers = load_users()
i=0
looking=True
result=-1
for user in allusers:
if user['username'] == username:
result=i
break
else:
i=i+1
return result
def find_channel_id_in_users_access(data, channel):
looking=True
result=-1
i=0
while looking==True:
if data["channels"][i]['channel'] == channel:
result=i
looking=False
else:
if i==len(data["channels"]):
looking=False
i=i+1
return result
def find_channel_memers(channel_look):
data = load_users()
result=[]
for user in data:
for channel in user["channels"]:
if channel["channel"] == channel_look:
result.append(user)
return result
def find_channels_which_user_is_a_member_of(username):
data = load_users()
userid=find_user_id(username)
chlist=[]
for ch in data[userid]["channels"]:
chlist.append(ch["channel"])
return chlist
def create_list_of_channels_which_user_is_admin_of(username):
user=get_user(username)
chlst=[]
for channel in user[channels]:
if channel['admin'] == True:
chlist.append(channel["channel"])
return chlst
def give_admin_to_channel(username, channel):
#A bit misleading - this also reverrses if ran against a user that is already channel admin
userid=find_user_id(username)
if userid == -1:
add_user(username, "False")
allusers=load_users()
channelid=find_channel_id_in_users_access(allusers[userid], channel)
try:
if allusers[userid]["channels"][channelid]['admin'] or allusers[userid]["channels"][channelid]['admin']=="True":
allusers[userid]["channels"][channelid]['admin']=False
else:
allusers[userid]["channels"][channelid]['admin']=True
write_users(allusers)
except:
print("Could not add admin to channel")
def give_user_access_to_channel(username, channel, admin=False):
userid=find_user_id(username)
if userid == -1:
add_user(username, "False")
allusers=load_users()
try:
if admin=="True":
admin=True
elif admin=="False":
admin=False
allusers[userid]["channels"].append({"channel":channel, "admin":admin})
except:
print("Could not add user to channel")
write_users(allusers)
def check_access(username, channel):
user = get_user(username)
r=False
try:
if user['isAdmin'] == "True":
r=True
else:
channelid=find_channel_id_in_users_access(user, channel)
if user["channels"][channelid]['admin'] == True:
r=True
else:
r=False
except Exception as e:
print("ERROR: Failed to retrieve access for "+str(username)+" in channel "+str(channel)+". Error: "+str(e))
return r
def check_access_channel(username, channel):
user_channels = find_channels_which_user_is_a_member_of(username)
rv=False
for ch in user_channels:
if ch == channel:
rv=True
if check_admin(username):
rv=True
return rv
def shutdown_server():
func = request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
def read_json(channel):
with open('logs/' + channel + '.json') as f:
data = json.load(f)
return data
def create_date(date):
wdate = datetime.datetime.strptime(date, '%c').strftime('%y%m%d')
return wdate
def create_date_list(channel):
with open('logs/' + channel + '.json') as f:
data = json.load(f)
# First, we find all uniqe dates.
dates = []
for d in data:
dates.append(create_date(d['date']))
date_set = set(dates)
dates = []
for date in date_set:
readable = datetime.datetime.strptime(date, '%y%m%d').strftime('%A %d. %b %Y')
dates.append([date, readable])
return reversed(sorted(dates, key=lambda dates: dates[0]))
def channel_date(channel, date):
with open('logs/' + channel + '.json') as f:
data = json.load(f)
sort = []
for d in data:
curdate = create_date(d['date'])
if curdate == date:
sort.append(d)
return sort
def get_channels():
ch = []
for file in os.listdir("logs"):
if file.endswith(".json"):
ch.append(file.replace(".json", ""))
return ch
def get_channels_filtered_for_user(username):
user_channels = find_channels_which_user_is_a_member_of(username)
all_channels = get_channels()
if check_admin(username):
return all_channels
else:
return set(all_channels) & set(user_channels)
def do_login(username, password):
if api_login(username, password):
username = username.lower()
if check_username(username):
session['username'] = username
session['admin'] = check_admin(username)
print("Logged in " + username + " with admin: " + str(check_admin(username)))
return True
else:
add_user(username, "False")
session['username'] = username
session['admin'] = check_admin(username)
print("Logged in " + username + " with admin: " + str(check_admin(username)))
return True
else:
return False
@app.route('/users', methods=['GET', 'POST'])
def users():
if check_login():
if session['admin'] is True:
if request.method == 'POST':
add_user(request.form['username'].lower(), request.form['isAdmin'])
return redirect(url_for('users'))
else:
return render_template('users.html', users=get_all_users(), user=get_user(session['username']), check_access=check_access, channelview=None)
else:
abort(403)
else:
abort(403)
@app.route('/users/<channel>')
def users_channel(channel):
if check_login():
if session['admin'] or check_access(session['username'], channel):
return render_template('users.html', users=find_channel_memers(channel), user=get_user(session['username']), check_access=check_access, channelview=channel)
else:
abort(403)
else:
abort(403)
@app.route('/users/<channel>/add-admin/<user>', methods=['GET'])
def web_add_admin_to_channel(channel, user):
if not check_login():
abort(403)
if session['admin'] or check_access(session['username'], channel):
give_admin_to_channel(user, channel)
return redirect(redirect_back())
else:
return abort(403)
def redirect_back(default='index'):
return request.args.get('next') or \
request.referrer or \
url_for(default)
@app.route('/users/<channel>/add-user', methods=['POST'])
def web_add_to_channel(channel):
if not check_login():
abort(403)
if check_access(session['username'], channel):
give_user_access_to_channel(request.form['user'], channel, request.form['admin'])
return redirect(redirect_back())
else:
return abort(403)
@app.route('/users/<user>/add-channel', methods=['post'])
def post_add_to_channel(user):
if not check_login():
abort(403)
channel = request.form['channel']
if check_access(session['username'], channel):
give_user_access_to_channel(user, channel, request.form['admin'])
return redirect(redirect_back())
else:
return abort(403)
@app.route('/logout')
def logout():
session.pop('username', None)
session.pop('admin', None)
return redirect(url_for('index'))
@app.route('/static/<path:path>')
def send_static(path):
return send_from_directory('static', path)
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
if request.form['login']:
state = do_login(request.form['username'].lower(), request.form['password'])
return redirect(url_for('index'))
else:
return redirect(url_for('index'))
else:
if check_login():
return render_template('index.html', result=get_channels_filtered_for_user(session['username']), isAdmin=session['admin'])
else:
return render_template('login.html')
@app.route('/channel')
def gotohomedamnyou():
return redirect(url_for('index'))
@app.route('/channel/<name>')
def show_channel(name):
if check_login():
if check_access_channel(session['username'], name):
return render_template('channel.html', name=name, result=reversed(read_json(name)),
dates=create_date_list(name), check_access=check_access, username=session['username'])
else:
abort(403)
else:
abort(403)
@app.route('/channel/<name>/<date>')
def channel_date_route(name, date):
if check_login():
if check_access_channel(session['username'], name):
readable = datetime.datetime.strptime(date, '%y%m%d').strftime('%A %d. %b %Y')
return render_template('channel.html', name=name, result=reversed(channel_date(name, date)),
dates=create_date_list(name), readable=readable, check_access=check_access, username=session['username'])
else:
abort(403)
else:
abort(403)
@app.errorhandler(404)
def page_not_found(e):
return render_template('error.html')
@app.errorhandler(500)
def page_not_found(e):
return render_template('error.html')
@app.errorhandler(403)
def need_to_login(e):
return redirect(url_for('index'))
def start_web(environ = None, start_response = None):
logger = logging.getLogger('werkzeug')
handler = logging.FileHandler('debug/access.log')
logger.addHandler(handler)
gunicorn_logger = logging.getLogger('gunicorn.erro')
app.logger.handlers = gunicorn_logger.handlers
app.logger.setLevel(gunicorn_logger.level)
app.run(debug=False, host='0.0.0.0', threaded=True)
if __name__ == "__main__":
start_web()