From 1c0854cc527b170dd854a8e6a80f1b0f8d07730d Mon Sep 17 00:00:00 2001 From: Darks Date: Wed, 7 Jun 2023 23:41:27 +0200 Subject: [PATCH] toolbar: enable toolbar only for devs --- .env | 3 +-- app/__init__.py | 5 +++-- app/utils/toolbar.py | 21 +++++++++++++++++++++ config.py | 2 +- 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 app/utils/toolbar.py diff --git a/.env b/.env index ff92252..0a687a1 100644 --- a/.env +++ b/.env @@ -1,3 +1,2 @@ FLASK_APP=V5.py -FLASK_DEBUG=1 -FLASK_ENV=DEVLOPPEMENT +FLASK_DEBUG=False \ No newline at end of file diff --git a/app/__init__.py b/app/__init__.py index 2f960d7..22b50f4 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -44,7 +44,8 @@ from app import processors from app import jobs # Enable flask-debug-toolbar if requested +# If app.config[] is True, will show for everyone if V5Config.ENABLE_FLASK_DEBUG_TOOLBAR: - from flask_debugtoolbar import DebugToolbarExtension + from app.utils.toolbar import CustomDebugToolbar app.config['DEBUG_TB_PROFILER_ENABLED'] = True - toolbar = DebugToolbarExtension(app) + toolbar = CustomDebugToolbar(app) \ No newline at end of file diff --git a/app/utils/toolbar.py b/app/utils/toolbar.py new file mode 100644 index 0000000..bb23e73 --- /dev/null +++ b/app/utils/toolbar.py @@ -0,0 +1,21 @@ +# Override some properties of Flask-Toolbar to allow access to authorized users + +from flask import request +from flask_debugtoolbar import DebugToolbarExtension +from flask_login import current_user + +class CustomDebugToolbar(DebugToolbarExtension): + def _show_toolbar(self): + """Return a boolean to indicate if we need to show the toolbar.""" + if request.blueprint == 'debugtoolbar': + return False + + # Debug mode: visible for everyone + if self.app.debug: + return True + + # Developers: toolbar visible + if current_user.is_authenticated and current_user.priv("misc.dev-infos"): + return True + + return False \ No newline at end of file diff --git a/config.py b/config.py index 063ece9..740923a 100644 --- a/config.py +++ b/config.py @@ -26,7 +26,7 @@ class FlaskApplicationSettings(object): See: https://flask.palletsprojects.com/en/2.1.x/config/ """ - DEBUG = os.environ.get("FLASK_DEBUG") or LocalConfig.FLASK_DEBUG + DEBUG = os.environ.get('FLASK_DEBUG') or False SECRET_KEY = os.environ.get('SECRET_KEY') or LocalConfig.SECRET_KEY SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \