2023-08-14 16:59:38 -04:00
|
|
|
|
from flask import flash, redirect, render_template, request, url_for
|
2019-07-01 15:22:08 +01:00
|
|
|
|
from flask_login import current_user
|
2018-07-06 11:10:14 +01:00
|
|
|
|
|
2018-07-06 16:16:21 +01:00
|
|
|
|
from app import user_api_client
|
2025-07-21 16:37:55 -07:00
|
|
|
|
from app.enums import ServicePermission
|
2019-05-22 14:05:19 +01:00
|
|
|
|
from app.event_handlers import create_archive_user_event
|
2018-07-06 11:10:14 +01:00
|
|
|
|
from app.main import main
|
2022-03-15 10:50:18 +00:00
|
|
|
|
from app.main.forms import AdminSearchUsersByEmailForm, AuthTypeForm
|
2019-05-23 15:27:35 +01:00
|
|
|
|
from app.models.user import User
|
2021-06-09 13:19:05 +01:00
|
|
|
|
from app.utils.user import user_is_platform_admin
|
2025-06-10 11:40:14 -07:00
|
|
|
|
from notifications_python_client.errors import HTTPError
|
2018-07-06 11:10:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@main.route("/find-users-by-email", methods=["GET", "POST"])
|
2018-07-06 11:10:14 +01:00
|
|
|
|
@user_is_platform_admin
|
|
|
|
|
|
def find_users_by_email():
|
2022-03-15 10:50:18 +00:00
|
|
|
|
form = AdminSearchUsersByEmailForm()
|
2018-07-06 16:16:21 +01:00
|
|
|
|
users_found = None
|
|
|
|
|
|
if form.validate_on_submit():
|
2023-08-25 09:12:23 -07:00
|
|
|
|
users_found = user_api_client.find_users_by_full_or_partial_email(
|
|
|
|
|
|
form.search.data
|
|
|
|
|
|
)["data"]
|
2018-07-06 11:10:14 +01:00
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"views/find-users/find-users-by-email.html", form=form, users_found=users_found
|
2019-08-16 11:20:36 +01:00
|
|
|
|
)
|
2018-07-10 17:24:20 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@main.route("/users/<uuid:user_id>", methods=["GET"])
|
2018-07-10 17:24:20 +01:00
|
|
|
|
@user_is_platform_admin
|
|
|
|
|
|
def user_information(user_id):
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"views/find-users/user-information.html",
|
2019-06-07 09:26:11 +01:00
|
|
|
|
user=User.from_id(user_id),
|
2018-07-10 17:24:20 +01:00
|
|
|
|
)
|
2019-05-22 11:38:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@main.route("/users/<uuid:user_id>/archive", methods=["GET", "POST"])
|
2019-05-22 11:38:47 +01:00
|
|
|
|
@user_is_platform_admin
|
|
|
|
|
|
def archive_user(user_id):
|
2023-08-25 09:12:23 -07:00
|
|
|
|
if request.method == "POST":
|
2020-04-21 17:40:47 +01:00
|
|
|
|
try:
|
|
|
|
|
|
user_api_client.archive_user(user_id)
|
|
|
|
|
|
except HTTPError as e:
|
2025-07-21 16:37:55 -07:00
|
|
|
|
if e.status_code == 400 and ServicePermission.MANAGE_SETTINGS in e.message:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
flash(
|
|
|
|
|
|
"User can’t be removed from a service - "
|
|
|
|
|
|
"check all services have another team member with manage_settings"
|
|
|
|
|
|
)
|
|
|
|
|
|
return redirect(url_for("main.user_information", user_id=user_id))
|
2021-07-12 15:29:53 +01:00
|
|
|
|
create_archive_user_event(user_id=str(user_id), archived_by_id=current_user.id)
|
2019-05-22 14:05:19 +01:00
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return redirect(url_for(".user_information", user_id=user_id))
|
2019-05-22 11:38:47 +01:00
|
|
|
|
else:
|
2023-08-25 09:12:23 -07:00
|
|
|
|
flash(
|
|
|
|
|
|
"There's no way to reverse this! Are you sure you want to archive this user?",
|
|
|
|
|
|
"delete",
|
|
|
|
|
|
)
|
2019-05-22 11:38:47 +01:00
|
|
|
|
return user_information(user_id)
|
2022-02-23 19:57:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
2023-08-25 09:12:23 -07:00
|
|
|
|
@main.route("/users/<uuid:user_id>/change_auth", methods=["GET", "POST"])
|
2022-02-23 19:57:27 +00:00
|
|
|
|
@user_is_platform_admin
|
|
|
|
|
|
def change_user_auth(user_id):
|
|
|
|
|
|
user = User.from_id(user_id)
|
|
|
|
|
|
|
|
|
|
|
|
form = AuthTypeForm(auth_type=user.auth_type)
|
|
|
|
|
|
|
|
|
|
|
|
if form.validate_on_submit():
|
|
|
|
|
|
user.update(auth_type=form.auth_type.data)
|
2023-08-25 09:12:23 -07:00
|
|
|
|
return redirect(url_for(".user_information", user_id=user_id))
|
2022-02-23 19:57:27 +00:00
|
|
|
|
|
|
|
|
|
|
return render_template(
|
2023-08-25 09:12:23 -07:00
|
|
|
|
"views/find-users/auth_type.html",
|
2022-02-23 19:57:27 +00:00
|
|
|
|
form=form,
|
|
|
|
|
|
user=user,
|
|
|
|
|
|
)
|