Skip to content

Commit

Permalink
Add team attrs and cache banning SQL query
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdHeat committed Apr 30, 2020
1 parent 0d8b0ee commit 3f3109f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
20 changes: 20 additions & 0 deletions CTFd/constants/teams.py
Original file line number Diff line number Diff line change
@@ -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",
],
)
2 changes: 1 addition & 1 deletion CTFd/constants/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
"team_id",
"created",
],
)
)
14 changes: 11 additions & 3 deletions CTFd/utils/initialization/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@
)
from CTFd.utils.security.auth import login_user, logout_user, lookup_user_token
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):
Expand Down Expand Up @@ -191,8 +199,8 @@ def banned():
return

if authed():
user = get_current_user()
team = get_current_team()
user = get_current_user_attrs()
team = get_current_team_attrs()

if user and user.banned:
return (
Expand Down
22 changes: 21 additions & 1 deletion CTFd/utils/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from CTFd.cache import cache
from CTFd.constants.users import UserAttrs
from CTFd.constants.teams import TeamAttrs
from CTFd.models import Fails, Users, db, Teams
from CTFd.utils import get_config

Expand Down Expand Up @@ -33,7 +34,7 @@ def get_user_attrs(user_id):
for field in UserAttrs._fields:
d[field] = getattr(user, field)
return UserAttrs(**d)
return user
return None


def get_current_team():
Expand All @@ -44,6 +45,25 @@ def get_current_team():
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):
if authed():
user = get_current_user_attrs()
Expand Down
5 changes: 1 addition & 4 deletions serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
"enabled": app.config["DEBUG"],
"storage": {"engine": "sqlite"},
"basicAuth": {"enabled": False},
"ignore": [
"^/themes/.*",
"^/events",
]
"ignore": ["^/themes/.*", "^/events"],
}
flask_profiler.init_app(app)
app.config["DEBUG_TB_PROFILER_ENABLED"] = True
Expand Down

0 comments on commit 3f3109f

Please sign in to comment.