diff --git a/app/models.py b/app/models.py index aa67c6a8..142b61cd 100644 --- a/app/models.py +++ b/app/models.py @@ -31,12 +31,13 @@ migrate = Migrate(app, db) class UserRank(enum.Enum): - NOT_JOINED = 0 - NEW_MEMBER = 1 - MEMBER = 2 - EDITOR = 3 - MODERATOR = 4 - ADMIN = 5 + BANNED = 0 + NOT_JOINED = 1 + NEW_MEMBER = 2 + MEMBER = 3 + EDITOR = 4 + MODERATOR = 5 + ADMIN = 6 def atLeast(self, min): return self.value >= min.value diff --git a/app/utils.py b/app/utils.py index 9be70d6c..5fd7cae9 100644 --- a/app/utils.py +++ b/app/utils.py @@ -62,6 +62,10 @@ def _do_login_user(user, remember_me=False): if not user: return False + if user.rank == UserRank.BANNED: + flash("You have been banned.", "error") + return False + user.active = True if not user.rank.atLeast(UserRank.NEW_MEMBER): user.rank = UserRank.NEW_MEMBER diff --git a/app/views/__init__.py b/app/views/__init__.py index abaacd71..8bfb178c 100644 --- a/app/views/__init__.py +++ b/app/views/__init__.py @@ -51,3 +51,10 @@ def flatpage(path): page = pages.get_or_404(path) template = page.meta.get('template', 'flatpage.html') return render_template(template, page=page) + +@app.before_request +def do_something_whenever_a_request_comes_in(): + if current_user.is_authenticated and current_user.rank == UserRank.BANNED: + flash("You have been banned.", "error") + logout_user() + return redirect(url_for('user.login')) diff --git a/migrations/versions/ea5a023711e0_.py b/migrations/versions/ea5a023711e0_.py new file mode 100644 index 00000000..4596be53 --- /dev/null +++ b/migrations/versions/ea5a023711e0_.py @@ -0,0 +1,29 @@ +"""empty message + +Revision ID: ea5a023711e0 +Revises: fa12fadbdb40 +Create Date: 2018-05-26 01:55:09.745881 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'ea5a023711e0' +down_revision = 'fa12fadbdb40' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + conn = op.get_bind() + conn.execute("ALTER TYPE userrank ADD VALUE 'BANNED'") + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + pass + # ### end Alembic commands ###