diff --git a/amanuensis/server/auth.py b/amanuensis/server/auth.py index 4ee6b3b..9ab642e 100644 --- a/amanuensis/server/auth.py +++ b/amanuensis/server/auth.py @@ -4,7 +4,7 @@ from flask import Blueprint, render_template, redirect, url_for, flash from flask_wtf import FlaskForm from wtforms import StringField, PasswordField, BooleanField, SubmitField from wtforms.validators import DataRequired -from flask_login import current_user, login_user, logout_user, login_required +from flask_login import login_user, logout_user, login_required from amanuensis.config import logger, json_rw from amanuensis.user import UserModel diff --git a/amanuensis/server/helpers.py b/amanuensis/server/helpers.py new file mode 100644 index 0000000..f25ea95 --- /dev/null +++ b/amanuensis/server/helpers.py @@ -0,0 +1,40 @@ +# Standard library imports +from functools import wraps + +# Third party imports +from flask import g, flash, redirect, url_for +from flask_login import current_user + +# Module imports +from amanuensis.lexicon import LexiconModel + +def lexicon_param(route): + """Wrapper for loading a route's lexicon""" + @wraps(route) + def with_lexicon(name): + g.lexicon = LexiconModel.by(name=name) + if g.lexicon is None: + flash("Couldn't find a lexicon with the name '{}'".format(name)) + return redirect(url_for("home.home")) + return route(name) + return with_lexicon + +def admin_required(route): + """Requires the user to be an admin to load this page""" + @wraps(route) + def admin_route(*args, **kwargs): + if not current_user.is_admin: + flash("You must be an admin to view this page") + return redirect(url_for('home.home')) + return route(*args, **kwargs) + return admin_route + +def player_required(route): + """Requires the user to be a player in the lexicon to load this page""" + @wraps(route) + def player_route(*args, **kwargs): + if current_user.id not in g.lexicon.join.joined: + flash("You must be a player to view this page") + return redirect(url_for('home.home')) + return route(*args, **kwargs) + return player_route \ No newline at end of file diff --git a/amanuensis/server/home.py b/amanuensis/server/home.py index b16d223..116add3 100644 --- a/amanuensis/server/home.py +++ b/amanuensis/server/home.py @@ -8,20 +8,10 @@ from wtforms import TextAreaField, SubmitField, StringField from amanuensis.config import json_ro from amanuensis.lexicon import LexiconModel +from amanuensis.server.helpers import admin_required from amanuensis.user import UserModel -def admin_required(route): - """Requires the user to be an admin""" - @wraps(route) - def admin_route(*args, **kwargs): - if not current_user.is_admin: - flash("Access is forbidden") - 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') diff --git a/amanuensis/server/lexicon.py b/amanuensis/server/lexicon.py index 5e9abc2..79ef1cb 100644 --- a/amanuensis/server/lexicon.py +++ b/amanuensis/server/lexicon.py @@ -9,20 +9,10 @@ from wtforms import TextAreaField, SubmitField from amanuensis.config import json_ro, open_ex from amanuensis.config.loader import ReadOnlyOrderedDict from amanuensis.lexicon import LexiconModel +from amanuensis.server.helpers import lexicon_param, player_required from amanuensis.user import UserModel -def lexicon_param(route): - @wraps(route) - def with_lexicon(name): - g.lexicon = LexiconModel.by(name=name) - if g.lexicon is None: - flash("Couldn't find a lexicon with the name '{}'".format(name)) - return redirect(url_for("home.home")) - return route(name) - return with_lexicon - - class LexiconConfigForm(FlaskForm): configText = TextAreaField("Config file") submit = SubmitField("Submit") diff --git a/amanuensis/templates/home/home.html b/amanuensis/templates/home/home.html index 3318b10..2022688 100644 --- a/amanuensis/templates/home/home.html +++ b/amanuensis/templates/home/home.html @@ -4,9 +4,11 @@ {% block main %}

Welcome to Amanuensis!

-

- Amanuensis is a hub for playing Lexicon, the encyclopedia RPG. Log in to access your Lexicon games. If you do not have an account, contact the administrator. -

+

Amanuensis is a hub for playing Lexicon, the encyclopedia RPG. Log in to access your Lexicon games. If you do not have an account, contact the administrator.

+ +{% for message in get_flashed_messages() %} +{{ message }}
+{% endfor %} {% if current_user.is_authenticated %} {% set lexicons = current_user.lexicons_in() %}