From 81c18b5c7c9d08a7fe3e7be956af677661d563b6 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 31 Jan 2020 13:58:39 -0800 Subject: [PATCH] Simplify player membership checks --- amanuensis/cli/lexicon.py | 6 +++--- amanuensis/lexicon/manage.py | 2 +- amanuensis/server/__init__.py | 2 ++ amanuensis/server/helpers.py | 4 ++-- amanuensis/templates/home/admin.html | 2 +- amanuensis/templates/home/home.html | 2 +- amanuensis/templates/macros.html | 27 ++++++++++++++++++--------- amanuensis/user.py | 12 +++++++++++- 8 files changed, 39 insertions(+), 18 deletions(-) diff --git a/amanuensis/cli/lexicon.py b/amanuensis/cli/lexicon.py index d0e21ba..ba414dd 100644 --- a/amanuensis/cli/lexicon.py +++ b/amanuensis/cli/lexicon.py @@ -134,7 +134,7 @@ def command_player_add(args): from amanuensis.lexicon.manage import add_player # Verify arguments - if args.user.id in args.lexicon.join.joined: + if args.user.in_lexicon(args.lexicon): logger.error('"{0.username}" is already a player in "{1.name}"'.format( args.user, args.lexicon)) return -1 @@ -163,7 +163,7 @@ def command_player_remove(args): from amanuensis.lexicon.manage import remove_player # Verify arguments - if args.user.id not in args.lexicon.join.joined: + if not args.user.in_lexicon(args.lexicon): logger.error('"{0.username}" is not a player in lexicon "{1.name}"' ''.format(args.user, args.lexicon)) return -1 @@ -218,7 +218,7 @@ def command_char_create(args): from amanuensis.user import UserModel # Verify arguments - if args.user.id not in args.lexicon.join.joined: + if not args.user.in_lexicon(args.lexicon): logger.error('"{0.username}" is not a player in lexicon "{1.name}"' ''.format(args.user, args.lexicon)) return -1 diff --git a/amanuensis/lexicon/manage.py b/amanuensis/lexicon/manage.py index 1394a12..fa62d24 100644 --- a/amanuensis/lexicon/manage.py +++ b/amanuensis/lexicon/manage.py @@ -125,7 +125,7 @@ def get_user_lexicons(user): return [ lexicon for lexicon in get_all_lexicons() - if user.id in lexicon.join.joined] + if user.in_lexicon(lexicon)] def valid_add(lex, player, password=None): diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index f9f144d..1df4a2a 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -8,6 +8,7 @@ from amanuensis.server.auth import get_bp as get_auth_bp from amanuensis.server.home import get_bp as get_home_bp from amanuensis.server.helpers import register_custom_filters from amanuensis.server.lexicon import get_bp as get_lex_bp +from amanuensis.user import AnonymousUserModel # Flask app init static_root = os.path.abspath(get("static_root")) @@ -23,6 +24,7 @@ register_custom_filters(app) # Flask-Login init login = LoginManager(app) login.login_view = 'auth.login' +login.anonymous_user = AnonymousUserModel # Blueprint inits auth_bp = get_auth_bp(login) diff --git a/amanuensis/server/helpers.py b/amanuensis/server/helpers.py index e404a14..1772f11 100644 --- a/amanuensis/server/helpers.py +++ b/amanuensis/server/helpers.py @@ -59,7 +59,7 @@ def player_required(route): """ @wraps(route) def player_route(*args, **kwargs): - if current_user.id not in g.lexicon.join.joined: + if not current_user.in_lexicon(g.lexicon): flash("You must be a player to view this page") return (redirect(url_for('lexicon.contents', name=g.lexicon.name)) if g.lexicon.join.public @@ -76,7 +76,7 @@ def player_required_if_not_public(route): @wraps(route) def player_route(*args, **kwargs): if ((not g.lexicon.join.public) - and current_user.id not in g.lexicon.join.joined): + and not current_user.in_lexicon(g.lexicon)): flash("You must be a player to view this page") return redirect(url_for('home.home')) return route(*args, **kwargs) diff --git a/amanuensis/templates/home/admin.html b/amanuensis/templates/home/admin.html index c564161..dbf8eda 100644 --- a/amanuensis/templates/home/admin.html +++ b/amanuensis/templates/home/admin.html @@ -1,5 +1,5 @@ {% extends "page_2col.html" %} -{% import 'macros.html' as macros %} +{% import 'macros.html' as macros with context %} {% block title %}Admin | Amanuensis{% endblock %} {% block header %}

Amanuensis - Admin Dashboard

{% endblock %} diff --git a/amanuensis/templates/home/home.html b/amanuensis/templates/home/home.html index ee4f172..5a58857 100644 --- a/amanuensis/templates/home/home.html +++ b/amanuensis/templates/home/home.html @@ -1,5 +1,5 @@ {% extends "page_1col.html" %} -{% import 'macros.html' as macros %} +{% import 'macros.html' as macros with context %} {% block title %}Home | Amanuensis{% endblock %} {% block header %}

Amanuensis - Home

{% endblock %} diff --git a/amanuensis/templates/macros.html b/amanuensis/templates/macros.html index 4d316eb..21255e3 100644 --- a/amanuensis/templates/macros.html +++ b/amanuensis/templates/macros.html @@ -1,4 +1,4 @@ -{% macro dashboard_lexicon_item(lexicon, anonymize) %} +{% macro dashboard_lexicon_item(lexicon) %}

@@ -8,19 +8,28 @@ [{{ lexicon.status().capitalize() }}]

{{ lexicon.prompt }}

+ {% if current_user.is_authenticated %}

- {% if anonymize %} - Players: {{ lexicon.join.joined|count }}/{{ lexicon.join.max_players }} - {% else %} - Editor: - {{ lexicon.editor|user_attr('username') }} / Players: - {% for uid in lexicon.join.joined[:-1] %} - {{ uid|user_attr('username') }}, + {% + if current_user.in_lexicon(lexicon) + or current_user.is_admin + %} + Editor: {{ lexicon.editor|user_attr('username') }} / + Players: + {% for uid in lexicon.join.joined %} + {{ uid|user_attr('username') }}{% if not loop.last %}, {% endif %} {% endfor %} - {{ lexicon.join.joined[-1]|user_attr('username') }} ({{ lexicon.join.joined|count }}/{{ lexicon.join.max_players }}) + {% else %} + Players: {{ lexicon.join.joined|count }}/{{ lexicon.join.max_players }} + {% if lexicon.join.public and lexicon.join.open %} + / + Join game + + {% endif %} {% endif %}

+ {% endif %}
{% endmacro %} diff --git a/amanuensis/user.py b/amanuensis/user.py index e8a79fc..7c4e433 100644 --- a/amanuensis/user.py +++ b/amanuensis/user.py @@ -3,7 +3,7 @@ import re import time import uuid -from flask_login import UserMixin +from flask_login import UserMixin, AnonymousUserMixin from werkzeug.security import generate_password_hash, check_password_hash from amanuensis.errors import ( @@ -64,6 +64,16 @@ class UserModel(UserMixin): with json_ro(self.config_path) as j: return check_password_hash(j['password'], pw) + def in_lexicon(self, lexicon): + return self.id in lexicon.join.joined + + +class AnonymousUserModel(AnonymousUserMixin): + is_admin = False + + def in_lexicon(self, lexicon): + return False + def valid_username(username): """