Add banning

Fixes #13
This commit is contained in:
rubenwardy 2018-05-26 01:58:56 +01:00
parent 2abcd8ee47
commit ef0a32524e
No known key found for this signature in database
GPG Key ID: A1E29D52FF81513C
4 changed files with 47 additions and 6 deletions

@ -31,12 +31,13 @@ migrate = Migrate(app, db)
class UserRank(enum.Enum): class UserRank(enum.Enum):
NOT_JOINED = 0 BANNED = 0
NEW_MEMBER = 1 NOT_JOINED = 1
MEMBER = 2 NEW_MEMBER = 2
EDITOR = 3 MEMBER = 3
MODERATOR = 4 EDITOR = 4
ADMIN = 5 MODERATOR = 5
ADMIN = 6
def atLeast(self, min): def atLeast(self, min):
return self.value >= min.value return self.value >= min.value

@ -62,6 +62,10 @@ def _do_login_user(user, remember_me=False):
if not user: if not user:
return False return False
if user.rank == UserRank.BANNED:
flash("You have been banned.", "error")
return False
user.active = True user.active = True
if not user.rank.atLeast(UserRank.NEW_MEMBER): if not user.rank.atLeast(UserRank.NEW_MEMBER):
user.rank = UserRank.NEW_MEMBER user.rank = UserRank.NEW_MEMBER

@ -51,3 +51,10 @@ def flatpage(path):
page = pages.get_or_404(path) page = pages.get_or_404(path)
template = page.meta.get('template', 'flatpage.html') template = page.meta.get('template', 'flatpage.html')
return render_template(template, page=page) 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'))

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