From 2845e0961bd1d6a20a40e33f565280ea4b782e21 Mon Sep 17 00:00:00 2001 From: TLSM Date: Fri, 5 Aug 2022 16:40:48 -0400 Subject: [PATCH] Turn login-gate of 1568ec01624 into admin toggle. --- files/__main__.py | 4 +++- files/classes/mod_logs.py | 10 ++++++++++ files/helpers/wrappers.py | 13 +++++++++++-- files/routes/comments.py | 2 +- files/routes/front.py | 2 +- files/routes/posts.py | 6 +++--- files/routes/subs.py | 2 +- files/routes/users.py | 4 ++-- files/templates/admin/admin_home.html | 5 +++++ 9 files changed, 37 insertions(+), 11 deletions(-) diff --git a/files/__main__.py b/files/__main__.py index a47da1b06..9b3832f92 100644 --- a/files/__main__.py +++ b/files/__main__.py @@ -80,7 +80,9 @@ mail = Mail(app) if not path.isfile(f'/site_settings.json'): with open('/site_settings.json', 'w', encoding='utf_8') as f: - f.write('{"Bots": true, "Fart mode": false, "Read-only mode": false, "Signups": true}') + f.write( + '{"Bots": true, "Fart mode": false, "Read-only mode": false, ' + \ + '"Signups": true, "login_required": false}') @app.before_request def before_request(): diff --git a/files/classes/mod_logs.py b/files/classes/mod_logs.py index 39a56f602..4ac311a8c 100644 --- a/files/classes/mod_logs.py +++ b/files/classes/mod_logs.py @@ -193,6 +193,11 @@ ACTIONTYPES = { "icon": 'fa-users', "color": 'bg-danger' }, + 'disable_login_required': { + "str": 'disabled Login Required', + "icon": 'fa-users', + "color": 'bg-danger' + }, 'disable_under_attack': { "str": 'disabled under attack mode', "icon": 'fa-shield', @@ -243,6 +248,11 @@ ACTIONTYPES = { "icon": 'fa-users', "color": 'bg-success' }, + 'enable_login_required': { + "str": 'enabled Login Required', + "icon": 'fa-users', + "color": 'bg-success' + }, 'enable_under_attack': { "str": 'enabled under attack mode', "icon": 'fa-shield', diff --git a/files/helpers/wrappers.py b/files/helpers/wrappers.py index a6e5db021..8f8068214 100644 --- a/files/helpers/wrappers.py +++ b/files/helpers/wrappers.py @@ -91,7 +91,6 @@ def check_ban_evade(v): def auth_desired(f): def wrapper(*args, **kwargs): - v = get_logged_in_user() check_ban_evade(v) @@ -101,9 +100,19 @@ def auth_desired(f): wrapper.__name__ = f.__name__ return wrapper +def auth_desired_with_logingate(f): + def wrapper(*args, **kwargs): + v = get_logged_in_user() + if app.config['SETTINGS']['login_required'] and not v: abort(401) + + check_ban_evade(v) + + return make_response(f(*args, v=v, **kwargs)) + + wrapper.__name__ = f.__name__ + return wrapper def auth_required(f): - def wrapper(*args, **kwargs): v = get_logged_in_user() if not v: abort(401) diff --git a/files/routes/comments.py b/files/routes/comments.py index 1f2cf22c6..09a88673f 100644 --- a/files/routes/comments.py +++ b/files/routes/comments.py @@ -34,7 +34,7 @@ WORDLE_COLOR_MAPPINGS = {-1: "🟥", 0: "🟨", 1: "🟩"} @app.get("/logged_out/post///") @app.get("/logged_out/h//comment/") @app.get("/logged_out/h//post///") -@auth_required +@auth_desired_with_logingate def post_pid_comment_cid(cid, pid=None, anything=None, v=None, sub=None): if not v and not request.path.startswith('/logged_out'): return redirect(f"/logged_out{request.full_path}#context") diff --git a/files/routes/front.py b/files/routes/front.py index cc83ab3c3..59525fd4d 100644 --- a/files/routes/front.py +++ b/files/routes/front.py @@ -16,7 +16,7 @@ from files.helpers.awards import award_timers @app.get("/logged_out/h/") @app.get("/logged_out/s/") @limiter.limit("3/second;30/minute;5000/hour;10000/day") -@auth_required +@auth_desired_with_logingate def front_all(v, sub=None, subdomain=None): if not v and not request.path.startswith('/logged_out'): diff --git a/files/routes/posts.py b/files/routes/posts.py index ed41d02df..13e81a384 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -103,7 +103,7 @@ def submit_get(v, sub=None): @app.get("/logged_out/post//") @app.get("/logged_out/h//post/") @app.get("/logged_out/h//post//") -@auth_required +@auth_desired_with_logingate def post_id(pid, anything=None, v=None, sub=None): if not v and not request.path.startswith('/logged_out'): return redirect(f"/logged_out{request.full_path}") @@ -233,7 +233,7 @@ def post_id(pid, anything=None, v=None, sub=None): @app.get("/viewmore///") @limiter.limit("1/second;30/minute;200/hour;1000/day") -@auth_required +@auth_desired_with_logingate def viewmore(v, pid, sort, offset): try: pid = int(pid) except: abort(400) @@ -324,7 +324,7 @@ def viewmore(v, pid, sort, offset): @app.get("/morecomments/") @limiter.limit("1/second;30/minute;200/hour;1000/day") -@auth_required +@auth_desired_with_logingate def morecomments(v, cid): try: cid = int(cid) except: abort(400) diff --git a/files/routes/subs.py b/files/routes/subs.py index cb368bea7..6c1c0abb6 100644 --- a/files/routes/subs.py +++ b/files/routes/subs.py @@ -433,7 +433,7 @@ def sub_sidebar(v, sub): return redirect(f'/h/{sub.name}/settings') @app.get("/holes") -@auth_required +@auth_desired_with_logingate def subs(v): subs = g.db.query(Sub, func.count(Submission.sub)).outerjoin(Submission, Sub.name == Submission.sub).group_by(Sub.name).order_by(func.count(Submission.sub).desc()).all() return render_template('sub/subs.html', v=v, subs=subs) diff --git a/files/routes/users.py b/files/routes/users.py index 7522065e2..001262bff 100644 --- a/files/routes/users.py +++ b/files/routes/users.py @@ -894,7 +894,7 @@ def visitors(v): @app.get("/@") @app.get("/@.json") @app.get("/logged_out/@") -@auth_required +@auth_desired_with_logingate def u_username(username, v=None): if not v and not request.path.startswith('/logged_out'): @@ -1006,7 +1006,7 @@ def u_username(username, v=None): @app.get("/@/comments") @app.get("/@/comments.json") @app.get("/logged_out/@/comments") -@auth_required +@auth_desired_with_logingate def u_username_comments(username, v=None): if not v and not request.path.startswith('/logged_out'): diff --git a/files/templates/admin/admin_home.html b/files/templates/admin/admin_home.html index 2ed7ad5c4..ba418c031 100644 --- a/files/templates/admin/admin_home.html +++ b/files/templates/admin/admin_home.html @@ -84,6 +84,11 @@ +
+ + +
+