Make CTFd.utils.user.is_admin wrap a cached function so we avoid a DB hit on page loads

is_admin_func_cache_hit
Kevin Chung 2020-04-29 03:15:20 -04:00
parent a9aa80af89
commit e1e22eddb3
2 changed files with 14 additions and 2 deletions

View File

@ -44,3 +44,8 @@ def clear_pages():
cache.delete_memoized(get_pages) cache.delete_memoized(get_pages)
cache.delete_memoized(get_page) cache.delete_memoized(get_page)
def clear_user_session(user_id):
from CTFd.utils.user import get_user_type
cache.delete_memoized(get_user_type, user_id=user_id)

View File

@ -4,6 +4,7 @@ import re
from flask import current_app as app from flask import current_app as app
from flask import request, session from flask import request, session
from CTFd.cache import cache
from CTFd.models import Fails, Users, db from CTFd.models import Fails, Users, db
from CTFd.utils import get_config from CTFd.utils import get_config
@ -32,14 +33,20 @@ def get_current_user_type(fallback=None):
return fallback return fallback
@cache.memoize()
def get_user_type(user_id):
user = Users.query.filter_by(id=user_id).first()
return user.type
def authed(): def authed():
return bool(session.get("id", False)) return bool(session.get("id", False))
def is_admin(): def is_admin():
if authed(): if authed():
user = get_current_user() user_type = get_user_type(user_id=session["id"])
return user.type == "admin" return user_type == "admin"
else: else:
return False return False