contentdb/app/utils/user.py

86 lines
2.6 KiB
Python
Raw Normal View History

2021-01-30 19:25:00 +01:00
# ContentDB
# Copyright (C) 2018-21 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from functools import wraps
2022-01-07 22:46:16 +01:00
from flask_babel import gettext
2021-01-30 19:25:00 +01:00
from flask_login import login_user, current_user
from passlib.handlers.bcrypt import bcrypt
from flask import redirect, url_for, abort, flash
2021-01-30 19:25:00 +01:00
from app.utils import is_safe_url
2023-06-19 20:32:36 +02:00
from app.models import User, UserRank, UserNotificationPreferences, db
2021-01-30 19:25:00 +01:00
def check_password_hash(stored, given):
if stored is None or stored == "":
return False
return bcrypt.verify(given.encode("UTF-8"), stored)
def make_flask_login_password(plaintext):
return bcrypt.hash(plaintext.encode("UTF-8"))
def post_login(user: User, next_url):
if next_url and is_safe_url(next_url):
return redirect(next_url)
if not current_user.password:
return redirect(url_for("users.set_password"))
notif_count = len(user.notifications)
if notif_count > 0:
if notif_count >= 10:
2022-01-07 22:46:16 +01:00
flash(gettext("You have a lot of notifications, you should either read or clear them"), "info")
return redirect(url_for("notifications.list_all"))
if user.notification_preferences is None:
2022-01-07 22:46:16 +01:00
flash(gettext("Please consider enabling email notifications, you can customise how much is sent"), "info")
return redirect(url_for("users.email_notifications", username=user.username))
return redirect(url_for("homepage.home"))
def login_user_set_active(user: User, next_url: str = None, *args, **kwargs):
2021-01-30 19:25:00 +01:00
if user.rank == UserRank.NOT_JOINED and user.email is None:
2022-08-23 03:24:12 +02:00
user.rank = UserRank.NEW_MEMBER
2021-01-30 19:25:00 +01:00
user.notification_preferences = UserNotificationPreferences(user)
user.is_active = True
db.session.commit()
if login_user(user, *args, **kwargs):
return post_login(user, next_url)
return None
2021-01-30 19:25:00 +01:00
def rank_required(rank):
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if not current_user.is_authenticated:
return redirect(url_for("users.login"))
2023-06-19 22:27:49 +02:00
if not current_user.rank.at_least(rank):
2021-01-30 19:25:00 +01:00
abort(403)
return f(*args, **kwargs)
return decorated_function
return decorator