feat(usermanager): add group edit view

This commit is contained in:
Julian Lobbes 2022-11-19 23:46:08 +01:00
parent 851a180815
commit 5c56e2d1de
2 changed files with 94 additions and 1 deletions

View File

@ -0,0 +1,46 @@
{% extends 'base.html' %}
{% block content %}
<div class="row">
<div class="col">
<h1>Editing group: {{ group.groupname }}</h1>
</div>
</div>
<form method="post">
{{ form.csrf_token }}
<div class="row mb-3">
<div class="col-sm">
{{ form.members.label(class="form-label") }}
{{ form.members(class="form-control" + (" is-invalid" if form.members.errors else "")) }}
{% if form.members.errors %}
{% for error in form.members.errors %}
<div class="invalid-feedback">{{ error }}</div>
{% endfor %}
{% endif %}
<div id="membersHelp" class="form-text">
List of members of this group.
</div>
</div>
<div class="col-sm">
{{ form.non_members.label(class="form-label") }}
{{ form.non_members(class="form-control" + (" is-invalid" if form.non_members.errors else "")) }}
{% if form.non_members.errors %}
{% for error in form.non_members.errors %}
<div class="invalid-feedback">{{ error }}</div>
{% endfor %}
{% endif %}
<div id="non_membersHelp" class="form-text">
List of users who are not members of this group.
</div>
</div>
</div>
<div class="row mb-3">
<div class="col-sm">
<a class="btn btn-secondary"
href="#"
role="button">Reset</a>
{{ form.submit(class_="btn btn-primary") }}
</div>
</div>
</form>
{% endblock content %}

View File

@ -9,7 +9,7 @@ from flask import (
from PIL import Image, UnidentifiedImageError
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from wtforms import ValidationError, StringField, PasswordField, SubmitField
from wtforms import ValidationError, StringField, PasswordField, SubmitField, SelectMultipleField
from wtforms.validators import InputRequired, Email, EqualTo
import lumi2.ldap as ldap
@ -300,3 +300,50 @@ def user_delete(username: str):
username=user.username,
deleted_groups=deleted_groups,
)
class GroupUpdateForm(FlaskForm):
members = SelectMultipleField(
'Group members',
validators=[InputRequired()],
)
non_members = SelectMultipleField(
'Other users',
)
submit = SubmitField(
'Update',
)
@bp.route("/groups/update/<string:groupname>")
def group_update(groupname: str):
"""Detail and Update view for a group.
Shows a form allowing the modification of user memberships for this group.
"""
try:
conn = ldap.get_connection()
except Exception:
abort(500)
try:
group = ldap.get_group(conn, groupname)
except ldap.EntryNotFoundException:
conn.unbind()
abort(404)
form = GroupUpdateForm()
form.members.choices = sorted({user.username for user in group.members})
form.non_members.choices = sorted(
{user.username for user in ldap.get_users(conn)} - set(form.members.choices)
)
conn.unbind()
return render_template(
'usermanager/group_edit.html',
form=form,
group=group,
)