Turn login-gate of 1568ec0162 into admin toggle.

remotes/1693045480750635534/spooky-22
Snakes 2022-08-05 16:40:48 -04:00
parent 846a7f4537
commit 2845e0961b
9 changed files with 37 additions and 11 deletions

View File

@ -80,7 +80,9 @@ mail = Mail(app)
if not path.isfile(f'/site_settings.json'): if not path.isfile(f'/site_settings.json'):
with open('/site_settings.json', 'w', encoding='utf_8') as f: 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 @app.before_request
def before_request(): def before_request():

View File

@ -193,6 +193,11 @@ ACTIONTYPES = {
"icon": 'fa-users', "icon": 'fa-users',
"color": 'bg-danger' "color": 'bg-danger'
}, },
'disable_login_required': {
"str": 'disabled Login Required',
"icon": 'fa-users',
"color": 'bg-danger'
},
'disable_under_attack': { 'disable_under_attack': {
"str": 'disabled under attack mode', "str": 'disabled under attack mode',
"icon": 'fa-shield', "icon": 'fa-shield',
@ -243,6 +248,11 @@ ACTIONTYPES = {
"icon": 'fa-users', "icon": 'fa-users',
"color": 'bg-success' "color": 'bg-success'
}, },
'enable_login_required': {
"str": 'enabled Login Required',
"icon": 'fa-users',
"color": 'bg-success'
},
'enable_under_attack': { 'enable_under_attack': {
"str": 'enabled under attack mode', "str": 'enabled under attack mode',
"icon": 'fa-shield', "icon": 'fa-shield',

View File

@ -91,7 +91,6 @@ def check_ban_evade(v):
def auth_desired(f): def auth_desired(f):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
v = get_logged_in_user() v = get_logged_in_user()
check_ban_evade(v) check_ban_evade(v)
@ -101,9 +100,19 @@ def auth_desired(f):
wrapper.__name__ = f.__name__ wrapper.__name__ = f.__name__
return wrapper 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 auth_required(f):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
v = get_logged_in_user() v = get_logged_in_user()
if not v: abort(401) if not v: abort(401)

View File

@ -34,7 +34,7 @@ WORDLE_COLOR_MAPPINGS = {-1: "🟥", 0: "🟨", 1: "🟩"}
@app.get("/logged_out/post/<pid>/<anything>/<cid>") @app.get("/logged_out/post/<pid>/<anything>/<cid>")
@app.get("/logged_out/h/<sub>/comment/<cid>") @app.get("/logged_out/h/<sub>/comment/<cid>")
@app.get("/logged_out/h/<sub>/post/<pid>/<anything>/<cid>") @app.get("/logged_out/h/<sub>/post/<pid>/<anything>/<cid>")
@auth_required @auth_desired_with_logingate
def post_pid_comment_cid(cid, pid=None, anything=None, v=None, sub=None): 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") if not v and not request.path.startswith('/logged_out'): return redirect(f"/logged_out{request.full_path}#context")

View File

@ -16,7 +16,7 @@ from files.helpers.awards import award_timers
@app.get("/logged_out/h/<sub>") @app.get("/logged_out/h/<sub>")
@app.get("/logged_out/s/<sub>") @app.get("/logged_out/s/<sub>")
@limiter.limit("3/second;30/minute;5000/hour;10000/day") @limiter.limit("3/second;30/minute;5000/hour;10000/day")
@auth_required @auth_desired_with_logingate
def front_all(v, sub=None, subdomain=None): def front_all(v, sub=None, subdomain=None):
if not v and not request.path.startswith('/logged_out'): if not v and not request.path.startswith('/logged_out'):

View File

@ -103,7 +103,7 @@ def submit_get(v, sub=None):
@app.get("/logged_out/post/<pid>/<anything>") @app.get("/logged_out/post/<pid>/<anything>")
@app.get("/logged_out/h/<sub>/post/<pid>") @app.get("/logged_out/h/<sub>/post/<pid>")
@app.get("/logged_out/h/<sub>/post/<pid>/<anything>") @app.get("/logged_out/h/<sub>/post/<pid>/<anything>")
@auth_required @auth_desired_with_logingate
def post_id(pid, anything=None, v=None, sub=None): 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}") 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/<pid>/<sort>/<offset>") @app.get("/viewmore/<pid>/<sort>/<offset>")
@limiter.limit("1/second;30/minute;200/hour;1000/day") @limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required @auth_desired_with_logingate
def viewmore(v, pid, sort, offset): def viewmore(v, pid, sort, offset):
try: pid = int(pid) try: pid = int(pid)
except: abort(400) except: abort(400)
@ -324,7 +324,7 @@ def viewmore(v, pid, sort, offset):
@app.get("/morecomments/<cid>") @app.get("/morecomments/<cid>")
@limiter.limit("1/second;30/minute;200/hour;1000/day") @limiter.limit("1/second;30/minute;200/hour;1000/day")
@auth_required @auth_desired_with_logingate
def morecomments(v, cid): def morecomments(v, cid):
try: cid = int(cid) try: cid = int(cid)
except: abort(400) except: abort(400)

View File

@ -433,7 +433,7 @@ def sub_sidebar(v, sub):
return redirect(f'/h/{sub.name}/settings') return redirect(f'/h/{sub.name}/settings')
@app.get("/holes") @app.get("/holes")
@auth_required @auth_desired_with_logingate
def subs(v): 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() 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) return render_template('sub/subs.html', v=v, subs=subs)

View File

@ -894,7 +894,7 @@ def visitors(v):
@app.get("/@<username>") @app.get("/@<username>")
@app.get("/@<username>.json") @app.get("/@<username>.json")
@app.get("/logged_out/@<username>") @app.get("/logged_out/@<username>")
@auth_required @auth_desired_with_logingate
def u_username(username, v=None): def u_username(username, v=None):
if not v and not request.path.startswith('/logged_out'): if not v and not request.path.startswith('/logged_out'):
@ -1006,7 +1006,7 @@ def u_username(username, v=None):
@app.get("/@<username>/comments") @app.get("/@<username>/comments")
@app.get("/@<username>/comments.json") @app.get("/@<username>/comments.json")
@app.get("/logged_out/@<username>/comments") @app.get("/logged_out/@<username>/comments")
@auth_required @auth_desired_with_logingate
def u_username_comments(username, v=None): def u_username_comments(username, v=None):
if not v and not request.path.startswith('/logged_out'): if not v and not request.path.startswith('/logged_out'):

View File

@ -84,6 +84,11 @@
<label class="custom-control-label" for="signups">Signups</label> <label class="custom-control-label" for="signups">Signups</label>
</div> </div>
<div class="custom-control custom-switch">
<input autocomplete="off" type="checkbox" class="custom-control-input" id="login_required" {% if site_settings['login_required'] %}checked{% endif %} onchange="post_toast(this,'/admin/site_settings/login_required');">
<label class="custom-control-label" for="login_required">Login Required</label>
</div>
<div class="custom-control custom-switch"> <div class="custom-control custom-switch">
<input autocomplete="off" type="checkbox" class="custom-control-input" id="bots" {% if site_settings['Bots'] %}checked{% endif %} onchange="post_toast(this,'/admin/site_settings/Bots');"> <input autocomplete="off" type="checkbox" class="custom-control-input" id="bots" {% if site_settings['Bots'] %}checked{% endif %} onchange="post_toast(this,'/admin/site_settings/Bots');">
<label class="custom-control-label" for="bots">Bots</label> <label class="custom-control-label" for="bots">Bots</label>