2021-08-03 17:26:31 +00:00
|
|
|
from random import vonmisesvariate
|
|
|
|
from sqlalchemy.sql import visitors
|
2021-07-21 01:12:26 +00:00
|
|
|
from werkzeug.wrappers.response import Response as RespObj
|
|
|
|
from .get import *
|
|
|
|
from .alerts import send_notification
|
2021-08-04 15:35:10 +00:00
|
|
|
from files.__main__ import app
|
2021-08-21 11:06:28 +00:00
|
|
|
from files.helpers.const import *
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
|
2021-08-03 16:25:38 +00:00
|
|
|
def get_logged_in_user():
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-31 04:32:42 +00:00
|
|
|
if request.headers.get("Authorization"):
|
|
|
|
token = request.headers.get("Authorization")
|
2021-08-03 17:26:31 +00:00
|
|
|
if not token: return None
|
2021-08-03 17:32:30 +00:00
|
|
|
|
2021-08-03 16:25:38 +00:00
|
|
|
client = g.db.query(ClientAuth).filter(ClientAuth.access_token == token).first()
|
2021-08-03 17:32:30 +00:00
|
|
|
|
|
|
|
x = (client.user, client) if client else (None, None)
|
|
|
|
|
2021-07-31 04:32:42 +00:00
|
|
|
|
2021-07-31 04:33:11 +00:00
|
|
|
else:
|
2021-08-03 17:32:30 +00:00
|
|
|
|
|
|
|
uid = session.get("user_id")
|
2021-07-21 01:12:26 +00:00
|
|
|
nonce = session.get("login_nonce", 0)
|
2021-08-03 17:32:30 +00:00
|
|
|
if not uid: x= (None, None)
|
2021-09-05 22:56:48 +00:00
|
|
|
try:
|
|
|
|
if g.db: v = g.db.query(User).filter_by(id=uid).first()
|
|
|
|
else: v = None
|
|
|
|
except: v = None
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
if v and v.agendaposter_expires_utc and v.agendaposter_expires_utc < g.timestamp:
|
|
|
|
v.agendaposter_expires_utc = 0
|
|
|
|
v.agendaposter = False
|
2021-08-03 17:32:30 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
g.db.add(v)
|
|
|
|
|
2021-08-03 17:32:30 +00:00
|
|
|
if v and (nonce < v.login_nonce):
|
|
|
|
x= (None, None)
|
|
|
|
else:
|
|
|
|
x=(v, None)
|
|
|
|
|
|
|
|
|
|
|
|
if x[0]: x[0].client=x[1]
|
|
|
|
|
|
|
|
return x[0]
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-31 04:30:25 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
def check_ban_evade(v):
|
|
|
|
|
|
|
|
if not v or not v.ban_evade or v.admin_level > 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
if random.randint(0,30) < v.ban_evade:
|
|
|
|
v.ban(reason="ban evasion")
|
2021-08-21 11:06:28 +00:00
|
|
|
send_notification(NOTIFICATIONS_ACCOUNT, v, "Your account has been permanently suspended for the following reason:\n\n> ban evasion")
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
for post in g.db.query(Submission).filter_by(author_id=v.id).all():
|
|
|
|
if post.is_banned:
|
|
|
|
continue
|
|
|
|
|
|
|
|
post.is_banned=True
|
|
|
|
post.ban_reason="ban evasion"
|
|
|
|
g.db.add(post)
|
|
|
|
|
2021-09-13 11:18:19 +00:00
|
|
|
g.db.flush()
|
2021-09-06 15:37:18 +00:00
|
|
|
|
2021-07-21 01:12:26 +00:00
|
|
|
ma=ModAction(
|
|
|
|
kind="ban_post",
|
2021-08-21 11:06:28 +00:00
|
|
|
user_id=AUTOJANNY_ACCOUNT,
|
2021-07-21 01:12:26 +00:00
|
|
|
target_submission_id=post.id,
|
|
|
|
note="ban evasion"
|
|
|
|
)
|
|
|
|
g.db.add(ma)
|
|
|
|
|
2021-08-11 17:01:19 +00:00
|
|
|
g.db.flush()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
for comment in g.db.query(Comment).filter_by(author_id=v.id).all():
|
|
|
|
if comment.is_banned:
|
|
|
|
continue
|
|
|
|
|
|
|
|
comment.is_banned=True
|
|
|
|
comment.ban_reason="ban evasion"
|
|
|
|
g.db.add(comment)
|
|
|
|
|
2021-09-07 02:07:11 +00:00
|
|
|
try:
|
|
|
|
ma=ModAction(
|
2021-07-21 01:12:26 +00:00
|
|
|
kind="ban_comment",
|
2021-08-21 11:06:28 +00:00
|
|
|
user_id=AUTOJANNY_ACCOUNT,
|
2021-07-21 01:12:26 +00:00
|
|
|
target_comment_id=comment.id,
|
|
|
|
note="ban evasion"
|
|
|
|
)
|
2021-09-07 02:07:11 +00:00
|
|
|
g.db.add(ma)
|
|
|
|
except: pass
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-08-11 17:01:19 +00:00
|
|
|
g.db.flush()
|
2021-07-21 01:12:26 +00:00
|
|
|
try: abort(403)
|
|
|
|
except Exception as e: print(e)
|
|
|
|
|
|
|
|
else:
|
|
|
|
v.ban_evade +=1
|
|
|
|
g.db.add(v)
|
2021-08-11 17:01:19 +00:00
|
|
|
g.db.flush()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Wrappers
|
|
|
|
def auth_desired(f):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
2021-08-03 17:28:36 +00:00
|
|
|
v = get_logged_in_user()
|
2021-07-21 01:12:26 +00:00
|
|
|
check_ban_evade(v)
|
|
|
|
|
|
|
|
resp = make_response(f(*args, v=v, **kwargs))
|
|
|
|
return resp
|
|
|
|
|
|
|
|
wrapper.__name__ = f.__name__
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def auth_required(f):
|
|
|
|
# decorator for any view that requires login (ex. settings)
|
|
|
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
2021-08-03 17:28:36 +00:00
|
|
|
v = get_logged_in_user()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
if not v:
|
|
|
|
abort(401)
|
|
|
|
|
|
|
|
check_ban_evade(v)
|
|
|
|
|
|
|
|
g.v = v
|
|
|
|
|
|
|
|
# an ugly hack to make api work
|
|
|
|
resp = make_response(f(*args, v=v, **kwargs))
|
|
|
|
return resp
|
|
|
|
|
|
|
|
wrapper.__name__ = f.__name__
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def is_not_banned(f):
|
|
|
|
# decorator that enforces lack of ban
|
|
|
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
2021-08-03 17:28:36 +00:00
|
|
|
v = get_logged_in_user()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
if not v:
|
|
|
|
abort(401)
|
|
|
|
|
|
|
|
check_ban_evade(v)
|
|
|
|
|
|
|
|
if v.is_suspended:
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
g.v = v
|
|
|
|
|
|
|
|
resp = make_response(f(*args, v=v, **kwargs))
|
|
|
|
return resp
|
|
|
|
|
|
|
|
wrapper.__name__ = f.__name__
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
# this wrapper takes args and is a bit more complicated
|
|
|
|
def admin_level_required(x):
|
|
|
|
|
|
|
|
def wrapper_maker(f):
|
|
|
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
2021-08-03 17:28:36 +00:00
|
|
|
v = get_logged_in_user()
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
if not v:
|
|
|
|
abort(401)
|
|
|
|
|
|
|
|
if v.admin_level < x:
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
g.v = v
|
|
|
|
|
|
|
|
response = f(*args, v=v, **kwargs)
|
|
|
|
|
|
|
|
if isinstance(response, tuple):
|
|
|
|
resp = make_response(response[0])
|
|
|
|
else:
|
|
|
|
resp = make_response(response)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
wrapper.__name__ = f.__name__
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return wrapper_maker
|
|
|
|
|
|
|
|
|
|
|
|
def validate_formkey(f):
|
|
|
|
"""Always use @auth_required or @admin_level_required above @validate_form"""
|
|
|
|
|
|
|
|
def wrapper(*args, v, **kwargs):
|
|
|
|
|
2021-07-31 04:48:47 +00:00
|
|
|
if not request.headers.get("Authorization"):
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-24 14:18:12 +00:00
|
|
|
submitted_key = request.values.get("formkey", None)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-24 14:18:12 +00:00
|
|
|
if not submitted_key: abort(401)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-24 14:18:12 +00:00
|
|
|
elif not v.validate_formkey(submitted_key): abort(401)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
return f(*args, v=v, **kwargs)
|
|
|
|
|
|
|
|
wrapper.__name__ = f.__name__
|
|
|
|
return wrapper
|
|
|
|
|
2021-07-31 06:51:58 +00:00
|
|
|
def api(*scopes, no_ban=False):
|
|
|
|
|
|
|
|
def wrapper_maker(f):
|
|
|
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
|
|
|
if request.path.startswith(('/api/v1','/api/v2')):
|
|
|
|
|
|
|
|
v = kwargs.get('v')
|
|
|
|
|
|
|
|
result = f(*args, **kwargs)
|
|
|
|
|
|
|
|
if isinstance(result, dict):
|
|
|
|
resp = result['api']()
|
|
|
|
else:
|
|
|
|
resp = result
|
|
|
|
|
|
|
|
if not isinstance(resp, RespObj):
|
|
|
|
resp = make_response(resp)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
result = f(*args, **kwargs)
|
|
|
|
|
|
|
|
if not isinstance(result, dict):
|
|
|
|
return result
|
|
|
|
|
|
|
|
try:
|
|
|
|
if request.path.startswith('/inpage/'):
|
|
|
|
return result['inpage']()
|
|
|
|
elif request.path.startswith(('/api/vue/','/test/')):
|
|
|
|
return result['api']()
|
|
|
|
else:
|
|
|
|
return result['html']()
|
|
|
|
except KeyError:
|
|
|
|
return result
|
|
|
|
|
|
|
|
wrapper.__name__ = f.__name__
|
|
|
|
return wrapper
|
|
|
|
|
2021-08-21 11:06:28 +00:00
|
|
|
return wrapper_maker
|