From 69524d03d74dc6b74df766fe3887f6aefb4637ac Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 17 Jan 2020 16:05:06 -0800 Subject: [PATCH] Refactor admin check into a wrapper --- amanuensis/server/home.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/amanuensis/server/home.py b/amanuensis/server/home.py index 45d283a..f5fa421 100644 --- a/amanuensis/server/home.py +++ b/amanuensis/server/home.py @@ -1,4 +1,6 @@ -from flask import Blueprint, render_template, url_for +from functools import wraps + +from flask import Blueprint, render_template, url_for, redirect from flask_login import login_required, current_user from flask_wtf import FlaskForm from wtforms import TextAreaField, SubmitField @@ -10,6 +12,15 @@ class DashboardForm(FlaskForm): admin_config_text = TextAreaField() submit = SubmitField("Submit") +def admin_required(route): + """Requires the user to be an admin""" + @wraps(route) + def admin_route(*args, **kwargs): + if not current_user.is_admin: + return redirect(url_for('home.home')) + return route(*args, **kwargs) + return admin_route + def get_bp(): """Create a blueprint for pages outside of a specific lexicon""" bp = Blueprint('home', __name__, url_prefix='/home') @@ -20,11 +31,8 @@ def get_bp(): return render_template('home/home.html') @bp.route('/admin/', methods=['GET']) - @login_required + @admin_required def admin(): - if not current_user.is_admin: - return redirect(url_for('home.home')) - with config.json_ro('config.json') as j: global_config = j import json