2023-08-15 14:17:19 +00:00
|
|
|
import binascii
|
2023-03-17 23:38:56 +00:00
|
|
|
import datetime
|
2021-12-12 14:55:00 +00:00
|
|
|
import io
|
2022-01-01 10:56:48 +00:00
|
|
|
from dataclasses import astuple
|
|
|
|
from dataclasses import dataclass
|
2023-08-26 14:50:01 +00:00
|
|
|
from importlib import metadata
|
2021-12-20 22:57:27 +00:00
|
|
|
|
2021-12-01 11:19:28 +00:00
|
|
|
import wtforms
|
2024-03-15 18:58:06 +00:00
|
|
|
from flask import Blueprint
|
|
|
|
from flask import abort
|
|
|
|
from flask import current_app
|
|
|
|
from flask import flash
|
|
|
|
from flask import g
|
|
|
|
from flask import redirect
|
|
|
|
from flask import request
|
|
|
|
from flask import send_file
|
|
|
|
from flask import session
|
|
|
|
from flask import url_for
|
|
|
|
from werkzeug.datastructures import CombinedMultiDict
|
|
|
|
from werkzeug.datastructures import FileStorage
|
|
|
|
|
2023-04-09 13:52:55 +00:00
|
|
|
from canaille.app import b64_to_obj
|
2023-07-20 16:43:28 +00:00
|
|
|
from canaille.app import build_hash
|
2023-04-09 13:52:55 +00:00
|
|
|
from canaille.app import default_fields
|
2023-04-09 09:37:04 +00:00
|
|
|
from canaille.app import models
|
2023-04-09 13:52:55 +00:00
|
|
|
from canaille.app import obj_to_b64
|
|
|
|
from canaille.app.flask import render_htmx_template
|
|
|
|
from canaille.app.flask import request_is_htmx
|
|
|
|
from canaille.app.flask import smtp_needed
|
|
|
|
from canaille.app.flask import user_needed
|
2023-11-22 10:30:30 +00:00
|
|
|
from canaille.app.forms import IDToModel
|
2024-03-15 18:58:06 +00:00
|
|
|
from canaille.app.forms import TableForm
|
2024-12-23 09:35:39 +00:00
|
|
|
from canaille.app.forms import compromised_password_validator
|
2023-07-24 16:07:35 +00:00
|
|
|
from canaille.app.forms import is_readonly
|
2024-12-23 09:35:39 +00:00
|
|
|
from canaille.app.forms import password_length_validator
|
|
|
|
from canaille.app.forms import password_too_long_validator
|
2023-07-24 16:07:35 +00:00
|
|
|
from canaille.app.forms import set_readonly
|
|
|
|
from canaille.app.forms import set_writable
|
2023-09-01 08:46:56 +00:00
|
|
|
from canaille.app.i18n import gettext as _
|
|
|
|
from canaille.app.i18n import reload_translations
|
2024-11-06 08:10:43 +00:00
|
|
|
from canaille.app.session import current_user
|
|
|
|
from canaille.app.session import login_user
|
|
|
|
from canaille.app.session import logout_user
|
2024-12-22 14:58:45 +00:00
|
|
|
from canaille.app.templating import render_template
|
2024-04-16 20:42:29 +00:00
|
|
|
from canaille.backends import Backend
|
2021-12-20 22:57:27 +00:00
|
|
|
|
2023-12-25 23:23:47 +00:00
|
|
|
from ..mails import send_confirmation_email
|
|
|
|
from ..mails import send_invitation_mail
|
|
|
|
from ..mails import send_password_initialization_mail
|
|
|
|
from ..mails import send_password_reset_mail
|
|
|
|
from ..mails import send_registration_mail
|
2023-07-20 16:43:28 +00:00
|
|
|
from .forms import EmailConfirmationForm
|
2021-12-20 22:57:27 +00:00
|
|
|
from .forms import InvitationForm
|
2023-06-05 08:38:24 +00:00
|
|
|
from .forms import JoinForm
|
2024-12-17 13:45:10 +00:00
|
|
|
from .forms import PasswordResetForm
|
2024-03-15 18:58:06 +00:00
|
|
|
from .forms import build_profile_form
|
2020-08-14 11:18:08 +00:00
|
|
|
|
2021-05-24 15:43:15 +00:00
|
|
|
bp = Blueprint("account", __name__)
|
2020-08-14 11:18:08 +00:00
|
|
|
|
|
|
|
|
2020-08-19 14:20:57 +00:00
|
|
|
@bp.route("/")
|
|
|
|
def index():
|
2022-04-05 15:16:09 +00:00
|
|
|
user = current_user()
|
|
|
|
|
|
|
|
if not user:
|
2023-08-15 10:39:54 +00:00
|
|
|
return redirect(url_for("core.auth.login"))
|
2022-04-05 15:16:09 +00:00
|
|
|
|
|
|
|
if user.can_edit_self or user.can_manage_users:
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.profile_edition", edited_user=user))
|
2022-04-05 15:16:09 +00:00
|
|
|
|
2024-12-05 11:20:26 +00:00
|
|
|
if current_app.features.has_oidc and user.can_use_oidc:
|
2022-04-05 15:16:09 +00:00
|
|
|
return redirect(url_for("oidc.consents.consents"))
|
|
|
|
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.about"))
|
2020-08-19 14:20:57 +00:00
|
|
|
|
|
|
|
|
2023-05-08 09:47:23 +00:00
|
|
|
@bp.route("/join", methods=("GET", "POST"))
|
|
|
|
def join():
|
2024-05-14 21:04:32 +00:00
|
|
|
if not current_app.features.has_registration:
|
2023-08-15 14:17:19 +00:00
|
|
|
abort(404)
|
|
|
|
|
2023-12-18 17:06:03 +00:00
|
|
|
if not current_app.config["CANAILLE"]["EMAIL_CONFIRMATION"]:
|
2023-08-15 14:17:19 +00:00
|
|
|
return redirect(url_for(".registration"))
|
|
|
|
|
2023-05-08 09:47:23 +00:00
|
|
|
if current_user():
|
2023-08-15 14:17:19 +00:00
|
|
|
abort(403)
|
|
|
|
|
2023-05-08 09:47:23 +00:00
|
|
|
form = JoinForm(request.form or None)
|
|
|
|
if request.form and form.validate():
|
2024-04-16 20:42:29 +00:00
|
|
|
if Backend.instance.query(models.User, emails=form.email.data):
|
2023-08-15 14:17:19 +00:00
|
|
|
flash(
|
|
|
|
_(
|
|
|
|
"You will receive soon an email to continue the registration process."
|
|
|
|
),
|
|
|
|
"success",
|
|
|
|
)
|
2024-12-18 23:16:58 +00:00
|
|
|
return render_template("core/join.html", form=form)
|
2023-08-15 14:17:19 +00:00
|
|
|
|
|
|
|
payload = RegistrationPayload(
|
|
|
|
creation_date_isoformat=datetime.datetime.now(
|
|
|
|
datetime.timezone.utc
|
|
|
|
).isoformat(),
|
|
|
|
user_name="",
|
|
|
|
user_name_editable=True,
|
|
|
|
email=form.email.data,
|
|
|
|
groups=[],
|
2023-05-08 09:47:23 +00:00
|
|
|
)
|
2023-08-15 14:17:19 +00:00
|
|
|
|
2023-05-08 09:47:23 +00:00
|
|
|
registration_url = url_for(
|
2023-08-15 14:17:19 +00:00
|
|
|
"core.account.registration",
|
|
|
|
data=payload.b64(),
|
|
|
|
hash=payload.build_hash(),
|
2023-05-08 09:47:23 +00:00
|
|
|
_external=True,
|
|
|
|
)
|
|
|
|
|
2023-08-15 14:17:19 +00:00
|
|
|
if send_registration_mail(form.email.data, registration_url):
|
2023-06-05 08:38:24 +00:00
|
|
|
flash(
|
2023-08-15 14:17:19 +00:00
|
|
|
_(
|
|
|
|
"You will receive soon an email to continue the registration process."
|
|
|
|
),
|
2023-06-05 08:38:24 +00:00
|
|
|
"success",
|
2023-05-08 09:47:23 +00:00
|
|
|
)
|
2023-08-15 14:17:19 +00:00
|
|
|
else:
|
|
|
|
flash(
|
|
|
|
_(
|
|
|
|
"An error happened while sending your registration mail. "
|
|
|
|
"Please try again in a few minutes. "
|
|
|
|
"If this still happens, please contact the administrators."
|
|
|
|
),
|
|
|
|
"error",
|
|
|
|
)
|
2023-05-08 09:47:23 +00:00
|
|
|
|
2024-12-18 23:16:58 +00:00
|
|
|
return render_template("core/join.html", form=form)
|
2023-05-08 09:47:23 +00:00
|
|
|
|
|
|
|
|
2020-11-13 09:45:01 +00:00
|
|
|
@bp.route("/about")
|
|
|
|
def about():
|
2023-08-26 14:50:01 +00:00
|
|
|
version = metadata.version("canaille")
|
2024-12-18 23:16:58 +00:00
|
|
|
return render_template("core/about.html", version=version)
|
2020-11-13 09:45:01 +00:00
|
|
|
|
|
|
|
|
2023-02-25 17:11:19 +00:00
|
|
|
@bp.route("/users", methods=["GET", "POST"])
|
2024-12-17 13:45:10 +00:00
|
|
|
@user_needed("manage_users")
|
2020-11-01 10:33:56 +00:00
|
|
|
def users(user):
|
2023-04-09 09:37:04 +00:00
|
|
|
table_form = TableForm(
|
2024-03-31 10:39:53 +00:00
|
|
|
models.User,
|
2024-04-07 13:21:32 +00:00
|
|
|
fields=user.readable_fields | user.writable_fields,
|
2024-03-31 10:39:53 +00:00
|
|
|
formdata=request.form,
|
2023-04-09 09:37:04 +00:00
|
|
|
)
|
2023-02-25 17:11:19 +00:00
|
|
|
if request.form and not table_form.validate():
|
|
|
|
abort(404)
|
|
|
|
|
2023-03-09 16:41:26 +00:00
|
|
|
return render_htmx_template(
|
2024-12-18 23:16:58 +00:00
|
|
|
"core/users.html",
|
2023-02-25 17:11:19 +00:00
|
|
|
menuitem="users",
|
|
|
|
table_form=table_form,
|
|
|
|
)
|
2020-11-01 10:33:56 +00:00
|
|
|
|
|
|
|
|
2022-01-01 10:56:48 +00:00
|
|
|
@dataclass
|
2023-08-15 14:17:19 +00:00
|
|
|
class VerificationPayload:
|
2022-01-01 10:56:48 +00:00
|
|
|
creation_date_isoformat: str
|
|
|
|
|
|
|
|
@property
|
|
|
|
def creation_date(self):
|
2023-03-17 23:38:56 +00:00
|
|
|
return datetime.datetime.fromisoformat(self.creation_date_isoformat)
|
2022-01-01 10:56:48 +00:00
|
|
|
|
|
|
|
def has_expired(self):
|
2023-03-17 23:38:56 +00:00
|
|
|
return datetime.datetime.now(
|
|
|
|
datetime.timezone.utc
|
|
|
|
) - self.creation_date > datetime.timedelta(
|
2023-12-18 17:06:03 +00:00
|
|
|
seconds=current_app.config["CANAILLE"]["INVITATION_EXPIRATION"]
|
2022-01-01 10:56:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def b64(self):
|
|
|
|
return obj_to_b64(astuple(self))
|
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
def build_hash(self):
|
|
|
|
return build_hash(*astuple(self))
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2023-08-15 14:17:19 +00:00
|
|
|
class EmailConfirmationPayload(VerificationPayload):
|
2023-07-20 16:43:28 +00:00
|
|
|
identifier: str
|
|
|
|
email: str
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
2023-08-15 14:17:19 +00:00
|
|
|
class RegistrationPayload(VerificationPayload):
|
2023-07-20 16:43:28 +00:00
|
|
|
user_name: str
|
|
|
|
user_name_editable: bool
|
|
|
|
email: str
|
2024-10-28 08:13:00 +00:00
|
|
|
groups: list[str]
|
2022-01-01 10:56:48 +00:00
|
|
|
|
|
|
|
|
2021-12-01 11:19:28 +00:00
|
|
|
@bp.route("/invite", methods=["GET", "POST"])
|
2021-12-07 15:39:18 +00:00
|
|
|
@smtp_needed()
|
2024-12-17 13:45:10 +00:00
|
|
|
@user_needed("manage_users")
|
2021-12-01 11:19:28 +00:00
|
|
|
def user_invitation(user):
|
|
|
|
form = InvitationForm(request.form or None)
|
2023-02-05 17:57:18 +00:00
|
|
|
email_sent = None
|
2021-12-01 11:19:28 +00:00
|
|
|
registration_url = None
|
2021-12-07 17:32:37 +00:00
|
|
|
form_validated = False
|
2021-12-01 11:19:28 +00:00
|
|
|
if request.form and form.validate():
|
2021-12-07 17:32:37 +00:00
|
|
|
form_validated = True
|
2023-08-15 14:17:19 +00:00
|
|
|
payload = RegistrationPayload(
|
2023-03-17 23:38:56 +00:00
|
|
|
datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
2023-02-05 17:57:18 +00:00
|
|
|
form.user_name.data,
|
|
|
|
form.user_name_editable.data,
|
|
|
|
form.email.data,
|
2023-11-22 10:30:30 +00:00
|
|
|
[group.id for group in form.groups.data],
|
2022-01-01 10:56:48 +00:00
|
|
|
)
|
2021-12-01 11:19:28 +00:00
|
|
|
registration_url = url_for(
|
2023-08-14 11:52:24 +00:00
|
|
|
"core.account.registration",
|
2023-08-15 14:17:19 +00:00
|
|
|
data=payload.b64(),
|
|
|
|
hash=payload.build_hash(),
|
2021-12-01 11:19:28 +00:00
|
|
|
_external=True,
|
|
|
|
)
|
|
|
|
|
2021-12-07 17:47:18 +00:00
|
|
|
if request.form["action"] == "send":
|
2023-02-05 17:57:18 +00:00
|
|
|
email_sent = send_invitation_mail(form.email.data, registration_url)
|
2021-12-01 11:19:28 +00:00
|
|
|
|
|
|
|
return render_template(
|
2024-12-18 23:16:58 +00:00
|
|
|
"core/invite.html",
|
2021-12-01 11:19:28 +00:00
|
|
|
form=form,
|
|
|
|
menuitems="users",
|
2021-12-07 17:32:37 +00:00
|
|
|
form_validated=form_validated,
|
2023-02-05 17:57:18 +00:00
|
|
|
email_sent=email_sent,
|
2021-12-01 11:19:28 +00:00
|
|
|
registration_url=registration_url,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-08-15 14:17:19 +00:00
|
|
|
@bp.route("/register", methods=["GET", "POST"])
|
2021-12-01 11:19:28 +00:00
|
|
|
@bp.route("/register/<data>/<hash>", methods=["GET", "POST"])
|
2023-08-15 14:17:19 +00:00
|
|
|
def registration(data=None, hash=None):
|
|
|
|
if not data:
|
|
|
|
payload = None
|
2023-12-18 17:06:03 +00:00
|
|
|
if (
|
2024-05-14 21:04:32 +00:00
|
|
|
not current_app.features.has_registration
|
|
|
|
or current_app.features.has_email_confirmation
|
2023-12-18 17:06:03 +00:00
|
|
|
):
|
2023-08-15 14:17:19 +00:00
|
|
|
abort(403)
|
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
payload = RegistrationPayload(*b64_to_obj(data))
|
|
|
|
except binascii.Error:
|
|
|
|
flash(
|
|
|
|
_("The registration link that brought you here was invalid."),
|
|
|
|
"error",
|
|
|
|
)
|
|
|
|
return redirect(url_for("core.account.index"))
|
2021-12-01 11:19:28 +00:00
|
|
|
|
2023-08-15 14:17:19 +00:00
|
|
|
if payload.has_expired():
|
|
|
|
flash(
|
|
|
|
_("The registration link that brought you here has expired."),
|
|
|
|
"error",
|
|
|
|
)
|
|
|
|
return redirect(url_for("core.account.index"))
|
2022-01-01 10:56:48 +00:00
|
|
|
|
2024-04-16 20:42:29 +00:00
|
|
|
if payload.user_name and Backend.instance.get(
|
2024-04-14 15:30:59 +00:00
|
|
|
models.User, user_name=payload.user_name
|
|
|
|
):
|
2023-08-15 14:17:19 +00:00
|
|
|
flash(
|
|
|
|
_("Your account has already been created."),
|
|
|
|
"error",
|
|
|
|
)
|
|
|
|
return redirect(url_for("core.account.index"))
|
|
|
|
|
|
|
|
if hash != payload.build_hash():
|
|
|
|
flash(
|
|
|
|
_("The registration link that brought you here was invalid."),
|
|
|
|
"error",
|
|
|
|
)
|
|
|
|
return redirect(url_for("core.account.index"))
|
2020-12-31 17:16:35 +00:00
|
|
|
|
2024-10-25 07:51:01 +00:00
|
|
|
user = current_user()
|
|
|
|
if user:
|
2021-12-01 11:50:00 +00:00
|
|
|
flash(
|
|
|
|
_("You are already logged in, you cannot create an account."),
|
|
|
|
"error",
|
|
|
|
)
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.index"))
|
2020-11-01 10:33:56 +00:00
|
|
|
|
2023-08-15 14:17:19 +00:00
|
|
|
if payload:
|
|
|
|
data = {
|
|
|
|
"user_name": payload.user_name,
|
|
|
|
"emails": [payload.email],
|
2024-04-14 15:30:59 +00:00
|
|
|
"groups": [
|
2024-04-16 20:42:29 +00:00
|
|
|
Backend.instance.get(models.Group, id=group_id)
|
2024-04-14 15:30:59 +00:00
|
|
|
for group_id in payload.groups
|
|
|
|
],
|
2023-08-15 14:17:19 +00:00
|
|
|
}
|
2020-11-01 10:33:56 +00:00
|
|
|
|
2024-05-14 20:53:47 +00:00
|
|
|
emails_readonly = current_app.features.has_email_confirmation
|
2021-12-08 09:00:36 +00:00
|
|
|
readable_fields, writable_fields = default_fields()
|
2021-12-02 17:23:14 +00:00
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
form = build_profile_form(writable_fields, readable_fields)
|
2023-08-15 14:17:19 +00:00
|
|
|
if "groups" not in form and payload and payload.groups:
|
2022-03-04 17:13:57 +00:00
|
|
|
form["groups"] = wtforms.SelectMultipleField(
|
|
|
|
_("Groups"),
|
2024-04-10 13:44:11 +00:00
|
|
|
choices=[
|
|
|
|
(group, group.display_name)
|
2024-04-16 20:42:29 +00:00
|
|
|
for group in Backend.instance.query(models.Group)
|
2024-04-10 13:44:11 +00:00
|
|
|
],
|
2023-11-22 10:30:30 +00:00
|
|
|
coerce=IDToModel("Group"),
|
2022-03-04 17:13:57 +00:00
|
|
|
)
|
2023-07-24 16:07:35 +00:00
|
|
|
set_readonly(form["groups"])
|
2021-12-01 11:19:28 +00:00
|
|
|
form.process(CombinedMultiDict((request.files, request.form)) or None, data=data)
|
|
|
|
|
2023-08-15 14:17:19 +00:00
|
|
|
if is_readonly(form["user_name"]) and (not payload or payload.user_name_editable):
|
2023-07-24 16:07:35 +00:00
|
|
|
set_writable(form["user_name"])
|
2022-01-01 17:41:04 +00:00
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
if not is_readonly(form["emails"]) and emails_readonly:
|
|
|
|
set_readonly(form["emails"])
|
2022-01-01 17:41:04 +00:00
|
|
|
|
2024-12-23 09:35:39 +00:00
|
|
|
form["password1"].validators = [
|
|
|
|
wtforms.validators.DataRequired(),
|
|
|
|
password_length_validator,
|
|
|
|
password_too_long_validator,
|
|
|
|
compromised_password_validator,
|
|
|
|
]
|
|
|
|
form["password2"].validators = [
|
|
|
|
wtforms.validators.DataRequired(),
|
|
|
|
wtforms.validators.EqualTo(
|
|
|
|
"password1", message=_("Password and confirmation do not match.")
|
|
|
|
),
|
|
|
|
]
|
|
|
|
form["password1"].flags.required = True
|
|
|
|
form["password2"].flags.required = True
|
2021-12-01 11:19:28 +00:00
|
|
|
|
2023-06-22 14:54:05 +00:00
|
|
|
if not request.form or form.form_control():
|
|
|
|
return render_template(
|
2024-12-18 23:16:58 +00:00
|
|
|
"core/profile_add.html",
|
2023-06-22 14:54:05 +00:00
|
|
|
form=form,
|
|
|
|
menuitem="users",
|
|
|
|
)
|
2021-12-01 11:19:28 +00:00
|
|
|
|
2023-06-22 14:54:05 +00:00
|
|
|
if not form.validate():
|
|
|
|
flash(_("User account creation failed."), "error")
|
|
|
|
return render_template(
|
2024-12-18 23:16:58 +00:00
|
|
|
"core/profile_add.html",
|
2023-06-22 14:54:05 +00:00
|
|
|
form=form,
|
|
|
|
menuitem="users",
|
|
|
|
)
|
2020-11-01 10:33:56 +00:00
|
|
|
|
2023-06-22 14:54:05 +00:00
|
|
|
user = profile_create(current_app, form)
|
2023-08-23 12:56:56 +00:00
|
|
|
login_user(user)
|
2023-06-22 14:54:05 +00:00
|
|
|
flash(_("Your account has been created successfully."), "success")
|
2023-08-31 16:49:31 +00:00
|
|
|
return redirect(
|
|
|
|
session.pop(
|
|
|
|
"redirect-after-login",
|
|
|
|
url_for("core.account.profile_edition", edited_user=user),
|
|
|
|
)
|
|
|
|
)
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
@bp.route("/email-confirmation/<data>/<hash>")
|
|
|
|
def email_confirmation(data, hash):
|
|
|
|
try:
|
2023-08-15 14:17:19 +00:00
|
|
|
confirmation_obj = EmailConfirmationPayload(*b64_to_obj(data))
|
2023-07-20 16:43:28 +00:00
|
|
|
except:
|
|
|
|
flash(
|
|
|
|
_("The email confirmation link that brought you here is invalid."),
|
|
|
|
"error",
|
|
|
|
)
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.index"))
|
2023-07-20 16:43:28 +00:00
|
|
|
|
|
|
|
if confirmation_obj.has_expired():
|
|
|
|
flash(
|
|
|
|
_("The email confirmation link that brought you here has expired."),
|
|
|
|
"error",
|
|
|
|
)
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.index"))
|
2023-07-20 16:43:28 +00:00
|
|
|
|
|
|
|
if hash != confirmation_obj.build_hash():
|
|
|
|
flash(
|
|
|
|
_("The invitation link that brought you here was invalid."),
|
|
|
|
"error",
|
|
|
|
)
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.index"))
|
2023-07-20 16:43:28 +00:00
|
|
|
|
2024-04-16 20:42:29 +00:00
|
|
|
user = Backend.instance.get(models.User, confirmation_obj.identifier)
|
2023-07-20 16:43:28 +00:00
|
|
|
if not user:
|
|
|
|
flash(
|
|
|
|
_("The email confirmation link that brought you here is invalid."),
|
|
|
|
"error",
|
|
|
|
)
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.index"))
|
2023-07-20 16:43:28 +00:00
|
|
|
|
|
|
|
if confirmation_obj.email in user.emails:
|
|
|
|
flash(
|
|
|
|
_("This address email have already been confirmed."),
|
|
|
|
"error",
|
|
|
|
)
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.index"))
|
2023-07-20 16:43:28 +00:00
|
|
|
|
2024-04-16 20:42:29 +00:00
|
|
|
if Backend.instance.query(models.User, emails=confirmation_obj.email):
|
2023-07-20 16:43:28 +00:00
|
|
|
flash(
|
|
|
|
_("This address email is already associated with another account."),
|
|
|
|
"error",
|
|
|
|
)
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.index"))
|
2023-07-20 16:43:28 +00:00
|
|
|
|
|
|
|
user.emails = user.emails + [confirmation_obj.email]
|
2024-04-16 20:42:29 +00:00
|
|
|
Backend.instance.save(user)
|
2023-07-20 16:43:28 +00:00
|
|
|
flash(_("Your email address have been confirmed."), "success")
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.index"))
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/profile", methods=("GET", "POST"))
|
2024-12-17 13:45:10 +00:00
|
|
|
@user_needed("manage_users")
|
2023-03-16 17:39:28 +00:00
|
|
|
def profile_creation(user):
|
2024-04-07 13:21:32 +00:00
|
|
|
form = build_profile_form(user.writable_fields, user.readable_fields)
|
2023-03-16 17:39:28 +00:00
|
|
|
form.process(CombinedMultiDict((request.files, request.form)) or None)
|
|
|
|
|
|
|
|
for field in form:
|
2023-07-24 16:07:35 +00:00
|
|
|
if is_readonly(field):
|
|
|
|
set_writable(field)
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-06-22 14:54:05 +00:00
|
|
|
if not request.form or form.form_control():
|
|
|
|
return render_template(
|
2024-12-18 23:16:58 +00:00
|
|
|
"core/profile_add.html",
|
2023-06-22 14:54:05 +00:00
|
|
|
form=form,
|
|
|
|
menuitem="users",
|
|
|
|
)
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-06-22 14:54:05 +00:00
|
|
|
if not form.validate():
|
|
|
|
flash(_("User account creation failed."), "error")
|
|
|
|
return render_template(
|
2024-12-18 23:16:58 +00:00
|
|
|
"core/profile_add.html",
|
2023-06-22 14:54:05 +00:00
|
|
|
form=form,
|
|
|
|
menuitem="users",
|
|
|
|
)
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-06-22 14:54:05 +00:00
|
|
|
user = profile_create(current_app, form)
|
2023-08-15 14:17:19 +00:00
|
|
|
flash(_("User account creation succeed."), "success")
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.profile_edition", edited_user=user))
|
2020-11-01 10:33:56 +00:00
|
|
|
|
|
|
|
|
2021-12-01 11:19:28 +00:00
|
|
|
def profile_create(current_app, form):
|
2023-04-09 09:37:04 +00:00
|
|
|
user = models.User()
|
2021-12-01 11:19:28 +00:00
|
|
|
for attribute in form:
|
2023-11-15 17:20:13 +00:00
|
|
|
if attribute.name in models.User.attributes:
|
2021-12-01 11:19:28 +00:00
|
|
|
if isinstance(attribute.data, FileStorage):
|
|
|
|
data = attribute.data.stream.read()
|
|
|
|
else:
|
|
|
|
data = attribute.data
|
|
|
|
|
2023-05-11 21:08:39 +00:00
|
|
|
setattr(user, attribute.name, data)
|
2021-12-01 11:19:28 +00:00
|
|
|
|
2023-02-05 17:57:18 +00:00
|
|
|
if "photo" in form and form["photo_delete"].data:
|
2023-11-21 13:57:28 +00:00
|
|
|
user.photo = None
|
2021-12-08 17:06:50 +00:00
|
|
|
|
2023-11-15 17:20:13 +00:00
|
|
|
given_name = user.given_name if user.given_name else ""
|
|
|
|
family_name = user.family_name if user.family_name else ""
|
2023-11-22 10:44:44 +00:00
|
|
|
user.formatted_name = f"{given_name} {family_name}".strip()
|
2024-04-16 20:42:29 +00:00
|
|
|
Backend.instance.save(user)
|
2021-12-01 12:22:21 +00:00
|
|
|
|
2022-12-24 01:52:05 +00:00
|
|
|
if form["password1"].data:
|
2024-04-16 20:42:29 +00:00
|
|
|
Backend.instance.set_user_password(user, form["password1"].data)
|
|
|
|
Backend.instance.save(user)
|
2022-12-24 01:52:05 +00:00
|
|
|
|
2021-12-01 11:19:28 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
def profile_edition_main_form(user, edited_user, emails_readonly):
|
2023-03-16 17:39:28 +00:00
|
|
|
available_fields = {
|
2023-02-05 17:57:18 +00:00
|
|
|
"formatted_name",
|
2023-03-16 17:39:28 +00:00
|
|
|
"title",
|
2023-02-05 17:57:18 +00:00
|
|
|
"given_name",
|
|
|
|
"family_name",
|
|
|
|
"display_name",
|
2023-06-22 13:14:07 +00:00
|
|
|
"emails",
|
2023-06-22 13:33:03 +00:00
|
|
|
"phone_numbers",
|
2023-02-05 17:57:18 +00:00
|
|
|
"formatted_address",
|
2023-03-16 17:39:28 +00:00
|
|
|
"street",
|
2023-02-05 17:57:18 +00:00
|
|
|
"postal_code",
|
|
|
|
"locality",
|
|
|
|
"region",
|
|
|
|
"photo",
|
|
|
|
"photo_delete",
|
|
|
|
"employee_number",
|
|
|
|
"department",
|
|
|
|
"profile_url",
|
|
|
|
"preferred_language",
|
|
|
|
"organization",
|
2023-03-16 17:39:28 +00:00
|
|
|
}
|
2023-07-20 16:43:28 +00:00
|
|
|
if emails_readonly:
|
|
|
|
available_fields.remove("emails")
|
|
|
|
|
2024-04-07 13:21:32 +00:00
|
|
|
readable_fields = user.readable_fields & available_fields
|
|
|
|
writable_fields = user.writable_fields & available_fields
|
2023-03-16 17:39:28 +00:00
|
|
|
data = {
|
2023-11-21 15:07:11 +00:00
|
|
|
field: getattr(edited_user, field)
|
2023-07-20 16:43:28 +00:00
|
|
|
for field in writable_fields | readable_fields
|
2023-11-21 15:07:11 +00:00
|
|
|
if hasattr(edited_user, field) and getattr(edited_user, field)
|
2023-03-16 17:39:28 +00:00
|
|
|
}
|
2023-07-20 16:43:28 +00:00
|
|
|
request_data = CombinedMultiDict((request.files, request.form))
|
|
|
|
profile_form = build_profile_form(writable_fields, readable_fields)
|
|
|
|
profile_form.process(request_data or None, data=data)
|
|
|
|
profile_form.user = edited_user
|
2024-12-18 23:16:58 +00:00
|
|
|
profile_form.render_field_macro_file = "core/partial/profile_field.html"
|
2023-07-20 16:43:28 +00:00
|
|
|
profile_form.render_field_extra_context = {
|
2023-06-30 14:20:36 +00:00
|
|
|
"user": user,
|
|
|
|
"edited_user": edited_user,
|
2023-03-16 17:39:28 +00:00
|
|
|
}
|
2023-07-20 16:43:28 +00:00
|
|
|
return profile_form
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
def profile_edition_main_form_validation(user, edited_user, profile_form):
|
2023-11-24 11:47:09 +00:00
|
|
|
for field in profile_form:
|
2024-04-07 13:21:32 +00:00
|
|
|
if field.name in edited_user.attributes and field.name in user.writable_fields:
|
2023-11-24 11:47:09 +00:00
|
|
|
if isinstance(field, wtforms.FieldList):
|
|
|
|
# too bad wtforms cannot sanitize the list itself
|
|
|
|
data = [value for value in field.data if value] or None
|
|
|
|
elif isinstance(field.data, FileStorage):
|
|
|
|
data = field.data.stream.read()
|
2023-06-22 14:54:05 +00:00
|
|
|
else:
|
2023-11-24 11:47:09 +00:00
|
|
|
data = field.data
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-11-24 11:47:09 +00:00
|
|
|
setattr(edited_user, field.name, data)
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
if "photo" in profile_form and profile_form["photo_delete"].data:
|
2023-11-21 13:57:28 +00:00
|
|
|
edited_user.photo = None
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-06-22 14:54:05 +00:00
|
|
|
if "preferred_language" in request.form:
|
|
|
|
# Refresh the babel cache in case the lang is updated
|
2023-09-01 08:46:56 +00:00
|
|
|
reload_translations()
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
if profile_form["preferred_language"].data == "auto":
|
2023-06-28 15:56:49 +00:00
|
|
|
edited_user.preferred_language = None
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2024-04-16 20:42:29 +00:00
|
|
|
Backend.instance.save(edited_user)
|
|
|
|
Backend.instance.reload(g.user)
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
def profile_edition_emails_form(user, edited_user, has_smtp):
|
|
|
|
emails_form = EmailConfirmationForm(
|
|
|
|
request.form or None, data={"old_emails": edited_user.emails}
|
|
|
|
)
|
|
|
|
emails_form.add_email_button = has_smtp
|
|
|
|
return emails_form
|
|
|
|
|
|
|
|
|
|
|
|
def profile_edition_add_email(user, edited_user, emails_form):
|
2023-08-15 14:17:19 +00:00
|
|
|
email_confirmation = EmailConfirmationPayload(
|
2023-07-20 16:43:28 +00:00
|
|
|
datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
|
|
|
edited_user.identifier,
|
|
|
|
emails_form.new_email.data,
|
2023-03-16 17:39:28 +00:00
|
|
|
)
|
2023-07-20 16:43:28 +00:00
|
|
|
email_confirmation_url = url_for(
|
2023-08-14 11:52:24 +00:00
|
|
|
"core.account.email_confirmation",
|
2023-07-20 16:43:28 +00:00
|
|
|
data=email_confirmation.b64(),
|
|
|
|
hash=email_confirmation.build_hash(),
|
|
|
|
_external=True,
|
|
|
|
)
|
|
|
|
current_app.logger.debug(
|
|
|
|
f"Attempt to send a verification mail with link: {email_confirmation_url}"
|
|
|
|
)
|
|
|
|
return send_confirmation_email(emails_form.new_email.data, email_confirmation_url)
|
|
|
|
|
|
|
|
|
|
|
|
def profile_edition_remove_email(user, edited_user, email):
|
|
|
|
if email not in edited_user.emails:
|
|
|
|
return False
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
if len(edited_user.emails) == 1:
|
|
|
|
return False
|
2023-03-16 17:39:28 +00:00
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
edited_user.emails = [m for m in edited_user.emails if m != email]
|
2024-04-16 20:42:29 +00:00
|
|
|
Backend.instance.save(edited_user)
|
2023-07-20 16:43:28 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/profile/<user:edited_user>", methods=("GET", "POST"))
|
2023-03-16 17:39:28 +00:00
|
|
|
@user_needed()
|
2023-07-20 16:43:28 +00:00
|
|
|
def profile_edition(user, edited_user):
|
2022-04-05 15:16:09 +00:00
|
|
|
if not user.can_manage_users and not (
|
2023-08-13 20:08:28 +00:00
|
|
|
user.can_edit_self and edited_user.id == user.id
|
2022-04-05 15:16:09 +00:00
|
|
|
):
|
2023-07-20 16:43:28 +00:00
|
|
|
abort(404)
|
|
|
|
|
2024-10-14 12:04:39 +00:00
|
|
|
request_ip = request.remote_addr or "unknown IP"
|
2023-08-13 20:08:28 +00:00
|
|
|
menuitem = "profile" if edited_user.id == user.id else "users"
|
2024-05-14 20:53:47 +00:00
|
|
|
emails_readonly = (
|
|
|
|
current_app.features.has_email_confirmation and not user.can_manage_users
|
2023-07-20 16:43:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
profile_form = profile_edition_main_form(user, edited_user, emails_readonly)
|
|
|
|
emails_form = (
|
2024-05-14 20:53:47 +00:00
|
|
|
profile_edition_emails_form(user, edited_user, current_app.features.has_smtp)
|
2023-07-20 16:43:28 +00:00
|
|
|
if emails_readonly
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
|
2024-10-21 09:17:55 +00:00
|
|
|
has_email_changed = "emails" in profile_form and set(
|
|
|
|
profile_form["emails"].data
|
|
|
|
) != set(user.emails)
|
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
render_context = {
|
|
|
|
"menuitem": menuitem,
|
|
|
|
"edited_user": edited_user,
|
|
|
|
"profile_form": profile_form,
|
|
|
|
"emails_form": emails_form,
|
|
|
|
}
|
|
|
|
|
|
|
|
if not request.form or profile_form.form_control():
|
2024-12-18 23:16:58 +00:00
|
|
|
return render_template("core/profile_edit.html", **render_context)
|
2023-07-20 16:43:28 +00:00
|
|
|
|
2023-09-11 16:28:47 +00:00
|
|
|
if request_is_htmx() or request.form.get("action") == "edit-profile":
|
2023-07-20 16:43:28 +00:00
|
|
|
if not profile_form.validate():
|
|
|
|
flash(_("Profile edition failed."), "error")
|
2024-12-18 23:16:58 +00:00
|
|
|
return render_template("core/profile_edit.html", **render_context)
|
2020-11-01 10:33:56 +00:00
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
profile_edition_main_form_validation(user, edited_user, profile_form)
|
2024-10-14 12:04:39 +00:00
|
|
|
|
2024-10-21 09:17:55 +00:00
|
|
|
if has_email_changed:
|
|
|
|
current_app.logger.security(
|
|
|
|
f"Updated email for {edited_user.user_name} from {request_ip}"
|
2024-10-14 12:04:39 +00:00
|
|
|
)
|
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
flash(_("Profile updated successfully."), "success")
|
2024-10-14 12:04:39 +00:00
|
|
|
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(
|
|
|
|
url_for("core.account.profile_edition", edited_user=edited_user)
|
|
|
|
)
|
2023-07-20 16:43:28 +00:00
|
|
|
|
|
|
|
if request.form.get("action") == "add_email":
|
|
|
|
if not emails_form.validate():
|
|
|
|
flash(_("Email addition failed."), "error")
|
2024-12-18 23:16:58 +00:00
|
|
|
return render_template("core/profile_edit.html", **render_context)
|
2023-07-20 16:43:28 +00:00
|
|
|
|
|
|
|
if profile_edition_add_email(user, edited_user, emails_form):
|
|
|
|
flash(
|
|
|
|
_(
|
|
|
|
"An email has been sent to the email address. "
|
|
|
|
"Please check your inbox and click on the verification link it contains"
|
|
|
|
),
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
flash(_("Could not send the verification email"), "error")
|
|
|
|
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(
|
|
|
|
url_for("core.account.profile_edition", edited_user=edited_user)
|
|
|
|
)
|
2023-07-20 16:43:28 +00:00
|
|
|
|
|
|
|
if request.form.get("email_remove"):
|
|
|
|
if not profile_edition_remove_email(
|
|
|
|
user, edited_user, request.form.get("email_remove")
|
|
|
|
):
|
|
|
|
flash(_("Email deletion failed."), "error")
|
2024-12-18 23:16:58 +00:00
|
|
|
return render_template("core/profile_edit.html", **render_context)
|
2023-07-20 16:43:28 +00:00
|
|
|
|
|
|
|
flash(_("The email have been successfully deleted."), "success")
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(
|
|
|
|
url_for("core.account.profile_edition", edited_user=edited_user)
|
|
|
|
)
|
2023-07-20 16:43:28 +00:00
|
|
|
|
|
|
|
abort(400, f"bad form action: {request.form.get('action')}")
|
2023-03-16 17:39:28 +00:00
|
|
|
|
|
|
|
|
2023-06-28 15:56:49 +00:00
|
|
|
@bp.route("/profile/<user:edited_user>/settings", methods=("GET", "POST"))
|
2023-03-16 17:39:28 +00:00
|
|
|
@user_needed()
|
2023-06-28 15:56:49 +00:00
|
|
|
def profile_settings(user, edited_user):
|
2023-08-13 20:08:28 +00:00
|
|
|
if not user.can_manage_users and not (
|
|
|
|
user.can_edit_self and edited_user.id == user.id
|
|
|
|
):
|
2023-03-22 07:52:00 +00:00
|
|
|
abort(404)
|
|
|
|
|
2023-03-30 21:14:39 +00:00
|
|
|
if (
|
|
|
|
request.method == "GET"
|
2023-07-30 21:08:17 +00:00
|
|
|
or request.form.get("action") == "edit-settings"
|
2023-03-30 21:14:39 +00:00
|
|
|
or request_is_htmx()
|
|
|
|
):
|
2023-03-22 07:52:00 +00:00
|
|
|
return profile_settings_edit(user, edited_user)
|
2020-11-01 10:33:56 +00:00
|
|
|
|
2023-09-28 16:27:43 +00:00
|
|
|
if request.form.get("action") == "confirm-delete":
|
2024-12-18 23:16:58 +00:00
|
|
|
return render_template(
|
|
|
|
"core/modals/delete-account.html", edited_user=edited_user
|
|
|
|
)
|
2023-07-06 16:43:37 +00:00
|
|
|
|
2020-11-01 10:33:56 +00:00
|
|
|
if request.form.get("action") == "delete":
|
2023-03-22 07:52:00 +00:00
|
|
|
return profile_delete(user, edited_user)
|
2020-11-01 10:33:56 +00:00
|
|
|
|
2021-01-06 16:19:44 +00:00
|
|
|
if request.form.get("action") == "password-initialization-mail":
|
2023-11-28 14:27:08 +00:00
|
|
|
statuses = [
|
2023-06-29 15:47:01 +00:00
|
|
|
send_password_initialization_mail(edited_user, email)
|
|
|
|
for email in edited_user.emails
|
2023-11-28 14:27:08 +00:00
|
|
|
]
|
|
|
|
success = all(statuses)
|
2023-06-29 15:47:01 +00:00
|
|
|
if success:
|
2021-01-06 16:19:44 +00:00
|
|
|
flash(
|
|
|
|
_(
|
2023-07-12 09:59:57 +00:00
|
|
|
"A password initialization link has been sent at the user email address. "
|
|
|
|
"It should be received within a few minutes."
|
2021-01-06 16:19:44 +00:00
|
|
|
),
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
flash(_("Could not send the password initialization email"), "error")
|
|
|
|
|
2023-03-22 07:52:00 +00:00
|
|
|
return profile_settings_edit(user, edited_user)
|
2021-01-06 16:19:44 +00:00
|
|
|
|
2021-01-22 17:26:53 +00:00
|
|
|
if request.form.get("action") == "password-reset-mail":
|
2023-11-28 14:27:08 +00:00
|
|
|
statuses = [
|
2023-06-29 15:47:01 +00:00
|
|
|
send_password_reset_mail(edited_user, email) for email in edited_user.emails
|
2023-11-28 14:27:08 +00:00
|
|
|
]
|
|
|
|
success = all(statuses)
|
2023-06-29 15:47:01 +00:00
|
|
|
if success:
|
2021-01-22 17:26:53 +00:00
|
|
|
flash(
|
|
|
|
_(
|
2023-07-12 09:59:57 +00:00
|
|
|
"A password reset link has been sent at the user email address. "
|
|
|
|
"It should be received within a few minutes."
|
2021-01-22 17:26:53 +00:00
|
|
|
),
|
|
|
|
"success",
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
flash(_("Could not send the password reset email"), "error")
|
|
|
|
|
2023-03-22 07:52:00 +00:00
|
|
|
return profile_settings_edit(user, edited_user)
|
2021-01-22 17:26:53 +00:00
|
|
|
|
2023-07-06 16:43:37 +00:00
|
|
|
if (
|
|
|
|
request.form.get("action") == "confirm-lock"
|
2024-05-14 20:53:47 +00:00
|
|
|
and current_app.features.has_account_lockability
|
2023-07-06 16:43:37 +00:00
|
|
|
and not edited_user.locked
|
|
|
|
):
|
2024-12-18 23:16:58 +00:00
|
|
|
return render_template("core/modals/lock-account.html", edited_user=edited_user)
|
2023-07-06 16:43:37 +00:00
|
|
|
|
2022-11-01 11:25:21 +00:00
|
|
|
if (
|
|
|
|
request.form.get("action") == "lock"
|
2024-05-14 20:53:47 +00:00
|
|
|
and current_app.features.has_account_lockability
|
2022-11-01 11:25:21 +00:00
|
|
|
and not edited_user.locked
|
|
|
|
):
|
|
|
|
flash(_("The account has been locked"), "success")
|
2023-05-26 15:44:15 +00:00
|
|
|
edited_user.lock_date = datetime.datetime.now(datetime.timezone.utc)
|
2024-04-16 20:42:29 +00:00
|
|
|
Backend.instance.save(edited_user)
|
2022-11-01 11:25:21 +00:00
|
|
|
|
|
|
|
return profile_settings_edit(user, edited_user)
|
|
|
|
|
|
|
|
if (
|
|
|
|
request.form.get("action") == "unlock"
|
2024-05-14 20:53:47 +00:00
|
|
|
and current_app.features.has_account_lockability
|
2022-11-01 11:25:21 +00:00
|
|
|
and edited_user.locked
|
|
|
|
):
|
|
|
|
flash(_("The account has been unlocked"), "success")
|
2023-11-21 13:57:28 +00:00
|
|
|
edited_user.lock_date = None
|
2024-04-16 20:42:29 +00:00
|
|
|
Backend.instance.save(edited_user)
|
2022-11-01 11:25:21 +00:00
|
|
|
|
|
|
|
return profile_settings_edit(user, edited_user)
|
|
|
|
|
2024-11-14 08:49:19 +00:00
|
|
|
if (
|
2024-11-18 13:16:38 +00:00
|
|
|
request.form.get("action") == "confirm-reset-otp"
|
2024-11-14 08:49:19 +00:00
|
|
|
and current_app.features.has_otp
|
|
|
|
):
|
2024-12-18 23:16:58 +00:00
|
|
|
return render_template("core/modals/reset-otp.html", edited_user=edited_user)
|
2024-11-14 08:49:19 +00:00
|
|
|
|
2024-11-18 13:16:38 +00:00
|
|
|
if request.form.get("action") == "reset-otp" and current_app.features.has_otp:
|
|
|
|
flash(_("One-time password authentication has been reset"), "success")
|
|
|
|
request_ip = request.remote_addr or "unknown IP"
|
|
|
|
current_app.logger.security(
|
|
|
|
f"Reset one-time password authentication for {edited_user.user_name} by {user.user_name} from {request_ip}"
|
|
|
|
)
|
2024-11-14 08:49:19 +00:00
|
|
|
edited_user.initialize_otp()
|
|
|
|
Backend.instance.save(edited_user)
|
|
|
|
|
|
|
|
return profile_settings_edit(user, edited_user)
|
|
|
|
|
2023-07-06 16:43:37 +00:00
|
|
|
abort(400, f"bad form action: {request.form.get('action')}")
|
2020-10-31 16:41:24 +00:00
|
|
|
|
2020-11-01 10:33:56 +00:00
|
|
|
|
2023-03-22 07:52:00 +00:00
|
|
|
def profile_settings_edit(editor, edited_user):
|
|
|
|
menuitem = "profile" if editor.id == editor.id else "users"
|
2024-04-07 13:21:32 +00:00
|
|
|
fields = editor.readable_fields | editor.writable_fields
|
2024-10-09 13:55:01 +00:00
|
|
|
request_ip = request.remote_addr or "unknown IP"
|
2022-04-04 15:49:50 +00:00
|
|
|
|
2022-11-01 11:25:21 +00:00
|
|
|
available_fields = {"password", "groups", "user_name", "lock_date"}
|
2020-10-20 09:44:45 +00:00
|
|
|
data = {
|
2023-03-16 17:39:28 +00:00
|
|
|
k: getattr(edited_user, k)[0]
|
|
|
|
if getattr(edited_user, k) and isinstance(getattr(edited_user, k), list)
|
|
|
|
else getattr(edited_user, k) or ""
|
2020-11-26 14:29:14 +00:00
|
|
|
for k in fields
|
2023-03-16 17:39:28 +00:00
|
|
|
if hasattr(edited_user, k) and k in available_fields
|
2020-10-20 09:44:45 +00:00
|
|
|
}
|
2021-07-01 09:36:35 +00:00
|
|
|
|
2025-01-07 15:58:39 +00:00
|
|
|
data["groups"] = edited_user.groups
|
2021-07-01 09:36:35 +00:00
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
form = build_profile_form(
|
2024-04-07 13:21:32 +00:00
|
|
|
editor.writable_fields & available_fields,
|
|
|
|
editor.readable_fields & available_fields,
|
2024-03-31 10:39:53 +00:00
|
|
|
edited_user,
|
2023-03-27 21:16:32 +00:00
|
|
|
)
|
2020-12-31 17:16:35 +00:00
|
|
|
form.process(CombinedMultiDict((request.files, request.form)) or None, data=data)
|
2023-07-30 21:08:17 +00:00
|
|
|
if (
|
|
|
|
request.form
|
|
|
|
and request.form.get("action") == "edit-settings"
|
|
|
|
or request_is_htmx()
|
|
|
|
):
|
2020-10-20 09:44:45 +00:00
|
|
|
if not form.validate():
|
2020-10-22 15:37:01 +00:00
|
|
|
flash(_("Profile edition failed."), "error")
|
2020-10-20 09:44:45 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
for attribute in form:
|
2024-04-07 13:21:32 +00:00
|
|
|
if attribute.name in available_fields & editor.writable_fields:
|
2023-05-26 15:44:15 +00:00
|
|
|
setattr(edited_user, attribute.name, attribute.data)
|
2021-12-08 17:06:50 +00:00
|
|
|
|
2021-01-06 16:19:44 +00:00
|
|
|
if (
|
2022-12-24 01:52:05 +00:00
|
|
|
"password1" in request.form
|
|
|
|
and form["password1"].data
|
2023-07-30 21:08:17 +00:00
|
|
|
and request.form["action"] == "edit-settings"
|
2022-12-24 01:52:05 +00:00
|
|
|
):
|
2024-04-16 20:42:29 +00:00
|
|
|
Backend.instance.set_user_password(edited_user, form["password1"].data)
|
2024-10-21 09:17:55 +00:00
|
|
|
current_app.logger.security(
|
|
|
|
f"Changed password in settings for {edited_user.user_name} from {request_ip}"
|
2024-10-09 13:55:01 +00:00
|
|
|
)
|
2021-12-02 17:23:14 +00:00
|
|
|
|
2024-04-16 20:42:29 +00:00
|
|
|
Backend.instance.save(edited_user)
|
2023-05-30 07:44:11 +00:00
|
|
|
flash(_("Profile updated successfully."), "success")
|
2023-03-22 07:52:00 +00:00
|
|
|
return redirect(
|
2023-08-14 11:52:24 +00:00
|
|
|
url_for("core.account.profile_settings", edited_user=edited_user)
|
2023-03-22 07:52:00 +00:00
|
|
|
)
|
2020-10-20 09:44:45 +00:00
|
|
|
|
2020-11-01 10:33:56 +00:00
|
|
|
return render_template(
|
2024-12-18 23:16:58 +00:00
|
|
|
"core/profile_settings.html",
|
2021-01-01 15:42:13 +00:00
|
|
|
form=form,
|
|
|
|
menuitem=menuitem,
|
2023-03-16 17:39:28 +00:00
|
|
|
edited_user=edited_user,
|
|
|
|
self_deletion=edited_user.can_delete_account,
|
2020-11-01 10:33:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-03-22 07:52:00 +00:00
|
|
|
def profile_delete(user, edited_user):
|
|
|
|
self_deletion = user.id == edited_user.id
|
2020-11-01 10:33:56 +00:00
|
|
|
if self_deletion:
|
2023-08-23 12:56:56 +00:00
|
|
|
logout_user()
|
2022-04-04 15:49:50 +00:00
|
|
|
|
2023-03-22 07:52:00 +00:00
|
|
|
flash(
|
2023-04-07 20:45:42 +00:00
|
|
|
_(
|
2024-09-11 07:33:42 +00:00
|
|
|
"The user %(user)s has been successfully deleted",
|
2023-11-15 17:20:13 +00:00
|
|
|
user=edited_user.formatted_name,
|
2023-04-07 20:45:42 +00:00
|
|
|
),
|
2023-03-22 07:52:00 +00:00
|
|
|
"success",
|
|
|
|
)
|
2024-04-16 20:42:29 +00:00
|
|
|
Backend.instance.delete(edited_user)
|
2020-11-01 10:33:56 +00:00
|
|
|
|
|
|
|
if self_deletion:
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.index"))
|
|
|
|
return redirect(url_for("core.account.users"))
|
2020-10-22 15:37:01 +00:00
|
|
|
|
|
|
|
|
2023-06-28 15:56:49 +00:00
|
|
|
@bp.route("/impersonate/<user:puppet>")
|
2024-12-17 13:45:10 +00:00
|
|
|
@user_needed("impersonate_users")
|
2023-06-28 15:56:49 +00:00
|
|
|
def impersonate(user, puppet):
|
2024-04-12 10:12:08 +00:00
|
|
|
if puppet.locked:
|
|
|
|
abort(403, _("Locked users cannot be impersonated."))
|
|
|
|
|
2023-08-23 12:56:56 +00:00
|
|
|
login_user(puppet)
|
2023-05-11 14:50:06 +00:00
|
|
|
flash(
|
2023-11-15 17:20:13 +00:00
|
|
|
_("Connection successful. Welcome %(user)s", user=puppet.formatted_name),
|
2023-05-11 14:50:06 +00:00
|
|
|
"success",
|
|
|
|
)
|
2023-08-14 11:52:24 +00:00
|
|
|
return redirect(url_for("core.account.index"))
|
2020-10-22 15:37:01 +00:00
|
|
|
|
|
|
|
|
2023-06-28 15:56:49 +00:00
|
|
|
@bp.route("/profile/<user:user>/<field>")
|
|
|
|
def photo(user, field):
|
2023-02-05 17:57:18 +00:00
|
|
|
if field.lower() != "photo":
|
2021-12-12 14:55:00 +00:00
|
|
|
abort(404)
|
|
|
|
|
2023-03-17 23:51:09 +00:00
|
|
|
etag = None
|
2023-02-05 17:57:18 +00:00
|
|
|
if request.if_modified_since and request.if_modified_since >= user.last_modified:
|
2023-03-17 23:51:09 +00:00
|
|
|
return "", 304
|
|
|
|
|
2023-07-20 16:43:28 +00:00
|
|
|
etag = build_hash(user.identifier, user.last_modified.isoformat())
|
2023-03-17 23:51:09 +00:00
|
|
|
if request.if_none_match and etag in request.if_none_match:
|
|
|
|
return "", 304
|
|
|
|
|
2023-11-15 17:20:13 +00:00
|
|
|
photo = getattr(user, field)
|
|
|
|
if not photo:
|
2022-12-22 16:12:24 +00:00
|
|
|
abort(404)
|
|
|
|
|
2023-11-15 17:20:13 +00:00
|
|
|
stream = io.BytesIO(photo)
|
2023-03-17 23:51:09 +00:00
|
|
|
return send_file(
|
2023-02-05 17:57:18 +00:00
|
|
|
stream, mimetype="image/jpeg", last_modified=user.last_modified, etag=etag
|
2023-03-17 23:51:09 +00:00
|
|
|
)
|
2024-12-17 13:45:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/reset/<user:user>", methods=["GET", "POST"])
|
|
|
|
def reset(user):
|
|
|
|
form = PasswordResetForm(request.form)
|
|
|
|
if user != current_user() or not user.has_expired_password():
|
|
|
|
abort(403)
|
|
|
|
|
|
|
|
if request.form and form.validate():
|
|
|
|
Backend.instance.set_user_password(user, form.password.data)
|
|
|
|
login_user(user)
|
|
|
|
flash(_("Your password has been updated successfully"), "success")
|
|
|
|
return redirect(
|
|
|
|
session.pop(
|
|
|
|
"redirect-after-login",
|
|
|
|
url_for("core.account.profile_edition", edited_user=user),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-12-19 11:25:38 +00:00
|
|
|
return render_template("core/reset-password.html", form=form, user=user, hash=None)
|