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 e15e23f038
commit 12d831a321
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_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 request, session
from CTFd.cache import cache
from CTFd.models import Fails, Users, db
from CTFd.utils import get_config
@ -32,14 +33,20 @@ def get_current_user_type(fallback=None):
return fallback
@cache.memoize()
def get_user_type(user_id):
user = Users.query.filter_by(id=user_id).first()
return user.type
def authed():
return bool(session.get("id", False))
def is_admin():
if authed():
user = get_current_user()
return user.type == "admin"
user_type = get_user_type(user_id=session["id"])
return user_type == "admin"
else:
return False