feat(usermanager): add User delete view

This commit is contained in:
Julian Lobbes 2022-11-19 15:13:44 +01:00
parent 7cb519a89f
commit 851a180815
4 changed files with 86 additions and 4 deletions

View File

@ -0,0 +1,35 @@
{% extends 'base.html' %}
{% block content %}
<div class="row justify-content-sm-center">
<div class="col-sm text-center align-self-center m-2">
<p><b>Are you sure</b> you want to <b class="text-danger">delete</b> the user '{{ username }}'?</p>
</div>
</div>
{% if deleted_groups %}
<div class="row justify-content-sm-center border border-warning rounded m-2">
<div class="col-sm text-center align-self-center m-2">
<p><u class="text-warning">Warning:</u> Since {{ username }} is their only member, the following <b>groups</b> will be deleted as well:</p>
</div>
<div class="col-sm text-center align-self-center m-2">
<ul class="list-group">
{% for groupname in deleted_groups %}
<li class="list-group-item">{{ groupname }}</li>
{% endfor %}
</ul>
</div>
</div>
{% endif %}
<div class="row justify-content-sm-center">
<div class="col-sm text-center align-self-center m-2">
<a class="btn btn-secondary"
href="{{ url_for('usermanager.user_view', username=username) }}"
role="button">No, take me back</a>
</div>
<div class="col-sm text-center align-self-center m-2">
<form method="post">
<input class="btn btn-danger" type="submit" value="Yes, delete this user">
</form>
</div>
</div>
{% endblock content %}

View File

@ -13,6 +13,7 @@
Create a new user
</a>
</div>
{% if users %}
<div class="table-responsive">
<table class="table table-striped table-hover">
<thead>
@ -47,4 +48,9 @@
</tbody>
</table>
</div>
{% else %}
<div class="text-center">
<p class="text-muted">There are currently no users.</p>
</div>
{% endif %}
{% endblock content %}

View File

@ -2,26 +2,26 @@
{% block content %}
<div class="row justify-content-sm-center">
<div class="col-sm-4 text-center align-self-center m-2">
<div class="col-sm-2 text-center align-self-center m-2">
<img src="{{ url_for('static', filename='images/users/' + user.username + '/full.jpg') }}"
alt="profile picture for user {{ user.username }}"
class="img-thumbnail"
style="max-width: 150px"
>
</div>
<div class="col-sm-auto vstack gap-1 align-self-center m-2">
<div class="col-sm-4 vstack gap-1 align-self-center m-2">
<div><b>Username:</b> {{ user.username }}</div>
<div><b>Email:</b> {{ user.email }}</div>
<div><b>First Name:</b> {{ user.first_name }}</div>
<div><b>Last Name:</b> {{ user.last_name }}</div>
<div><b>Nickname:</b> {{ user.display_name }}</div>
</div>
<div class="col-sm-4 vstack gap-1 align-self-center m-2">
<div class="col-sm-auto vstack gap-1 align-self-center m-2">
<a class="btn btn-primary"
href="{{ url_for('usermanager.user_update', username=user.username) }}"
role="button">Edit</a>
<a class="btn btn-danger"
href="#"
href="{{ url_for('usermanager.user_delete', username=user.username) }}"
role="button">Delete</a>
</div>
</div>

View File

@ -259,3 +259,44 @@ def user_update(username: str):
heading=f"Edit user: {user.username}",
is_update=True,
)
@bp.route("/users/delete/<string:username>", methods=("GET", "POST"))
def user_delete(username: str):
"""Deletion view for a specific User.
Provides a form prompting the confirmation of the specified user.
If the user is the sole member of any groups, the groups which get deleted
implicitly by the lumi.ldap.delete_user() operation will be listed here as
well.
"""
try:
conn = ldap.get_connection()
except Exception:
abort(500)
try:
user = ldap.get_user(conn, username)
except ldap.EntryNotFoundException:
conn.unbind()
abort(404)
deleted_groups = set()
for group in ldap.get_groups_of_user(conn, user):
if len(group.members) == 1:
deleted_groups.add(group.groupname)
if request.method == 'POST':
ldap.delete_user(conn, user.username)
conn.unbind()
flash(f"The user '{user.username}' was deleted.")
for groupname in deleted_groups:
flash(f"The group '{groupname}' was deleted.")
return redirect(url_for('usermanager.user_list'))
return render_template(
'usermanager/user_delete.html',
username=user.username,
deleted_groups=deleted_groups,
)