Add player_requires

This commit is contained in:
Tim Van Baak 2020-01-24 13:30:21 -08:00
parent 45b7e43df4
commit 817e74f6d7
5 changed files with 48 additions and 26 deletions

View File

@ -4,7 +4,7 @@ from flask import Blueprint, render_template, redirect, url_for, flash
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired 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.config import logger, json_rw
from amanuensis.user import UserModel from amanuensis.user import UserModel

View File

@ -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

View File

@ -8,20 +8,10 @@ from wtforms import TextAreaField, SubmitField, StringField
from amanuensis.config import json_ro from amanuensis.config import json_ro
from amanuensis.lexicon import LexiconModel from amanuensis.lexicon import LexiconModel
from amanuensis.server.helpers import admin_required
from amanuensis.user import UserModel 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(): def get_bp():
"""Create a blueprint for pages outside of a specific lexicon""" """Create a blueprint for pages outside of a specific lexicon"""
bp = Blueprint('home', __name__, url_prefix='/home') bp = Blueprint('home', __name__, url_prefix='/home')

View File

@ -9,20 +9,10 @@ from wtforms import TextAreaField, SubmitField
from amanuensis.config import json_ro, open_ex from amanuensis.config import json_ro, open_ex
from amanuensis.config.loader import ReadOnlyOrderedDict from amanuensis.config.loader import ReadOnlyOrderedDict
from amanuensis.lexicon import LexiconModel from amanuensis.lexicon import LexiconModel
from amanuensis.server.helpers import lexicon_param, player_required
from amanuensis.user import UserModel 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): class LexiconConfigForm(FlaskForm):
configText = TextAreaField("Config file") configText = TextAreaField("Config file")
submit = SubmitField("Submit") submit = SubmitField("Submit")

View File

@ -4,9 +4,11 @@
{% block main %} {% block main %}
<h1>Welcome to Amanuensis!</h1> <h1>Welcome to Amanuensis!</h1>
<p> <p>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.</p>
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.
</p> {% for message in get_flashed_messages() %}
<span style="color:#ff0000">{{ message }}</span><br>
{% endfor %}
{% if current_user.is_authenticated %} {% if current_user.is_authenticated %}
{% set lexicons = current_user.lexicons_in() %} {% set lexicons = current_user.lexicons_in() %}