Add team attrs and cache banning SQL query

is_admin_func_cache_hit
Kevin Chung 2020-04-29 20:33:51 -04:00
parent 0d8b0ee966
commit 3f3109f589
5 changed files with 54 additions and 9 deletions

20
CTFd/constants/teams.py Normal file
View File

@ -0,0 +1,20 @@
from collections import namedtuple
TeamAttrs = namedtuple(
"TeamAttrs",
[
"id",
"oauth_id",
"name",
"email",
"secret",
"website",
"affiliation",
"country",
"bracket",
"hidden",
"banned",
"captain_id",
"created",
],
)

View File

@ -19,4 +19,4 @@ UserAttrs = namedtuple(
"team_id", "team_id",
"created", "created",
], ],
) )

View File

@ -38,7 +38,15 @@ from CTFd.utils.plugins import (
) )
from CTFd.utils.security.auth import login_user, logout_user, lookup_user_token from CTFd.utils.security.auth import login_user, logout_user, lookup_user_token
from CTFd.utils.security.csrf import generate_nonce from CTFd.utils.security.csrf import generate_nonce
from CTFd.utils.user import authed, get_current_team, get_current_user, get_ip, is_admin from CTFd.utils.user import (
authed,
get_current_team,
get_current_user,
get_current_user_attrs,
get_current_team_attrs,
get_ip,
is_admin,
)
def init_template_filters(app): def init_template_filters(app):
@ -191,8 +199,8 @@ def init_request_processors(app):
return return
if authed(): if authed():
user = get_current_user() user = get_current_user_attrs()
team = get_current_team() team = get_current_team_attrs()
if user and user.banned: if user and user.banned:
return ( return (

View File

@ -6,6 +6,7 @@ from flask import request, session
from CTFd.cache import cache from CTFd.cache import cache
from CTFd.constants.users import UserAttrs from CTFd.constants.users import UserAttrs
from CTFd.constants.teams import TeamAttrs
from CTFd.models import Fails, Users, db, Teams from CTFd.models import Fails, Users, db, Teams
from CTFd.utils import get_config from CTFd.utils import get_config
@ -33,7 +34,7 @@ def get_user_attrs(user_id):
for field in UserAttrs._fields: for field in UserAttrs._fields:
d[field] = getattr(user, field) d[field] = getattr(user, field)
return UserAttrs(**d) return UserAttrs(**d)
return user return None
def get_current_team(): def get_current_team():
@ -44,6 +45,25 @@ def get_current_team():
return None return None
def get_current_team_attrs():
if authed():
user = get_user_attrs(user_id=session["id"])
if user.team_id:
return get_team_attrs(team_id=user.team_id)
return None
@cache.memoize()
def get_team_attrs(team_id):
team = Teams.query.filter_by(id=team_id).first()
if team:
d = {}
for field in TeamAttrs._fields:
d[field] = getattr(user, field)
return TeamAttrs(**d)
return None
def get_current_user_type(fallback=None): def get_current_user_type(fallback=None):
if authed(): if authed():
user = get_current_user_attrs() user = get_current_user_attrs()

View File

@ -18,10 +18,7 @@ if args.profile:
"enabled": app.config["DEBUG"], "enabled": app.config["DEBUG"],
"storage": {"engine": "sqlite"}, "storage": {"engine": "sqlite"},
"basicAuth": {"enabled": False}, "basicAuth": {"enabled": False},
"ignore": [ "ignore": ["^/themes/.*", "^/events"],
"^/themes/.*",
"^/events",
]
} }
flask_profiler.init_app(app) flask_profiler.init_app(app)
app.config["DEBUG_TB_PROFILER_ENABLED"] = True app.config["DEBUG_TB_PROFILER_ENABLED"] = True