implement group member management

This commit is contained in:
Franzi 2021-12-21 10:20:16 +01:00
parent 6446c09a01
commit 8de81d8ab1
Signed by: kunsi
GPG key ID: 12E3D2136B818350
2 changed files with 56 additions and 3 deletions

View file

@ -2,7 +2,7 @@ from json import load
from os import environ from os import environ
from flask import Flask, flash, redirect, request, session, url_for from flask import Flask, flash, redirect, request, session, url_for
from ldap3 import ALL_ATTRIBUTES from ldap3 import ALL_ATTRIBUTES, MODIFY_ADD, MODIFY_DELETE
from ldap3.core.exceptions import LDAPException from ldap3.core.exceptions import LDAPException
from .helpers.flask import template from .helpers.flask import template
@ -154,12 +154,45 @@ def groups(ldap):
def group_edit(ldap, ou): def group_edit(ldap, ou):
if request.method == "POST": if request.method == "POST":
if request.form.get("remove"): if request.form.get("remove"):
flash( ldap.modify(
f"did not remove {request.form['remove']} because not yet implemented" APP_CONFIG["template"]["group_dn"].format(ou),
{
"member": [
(
MODIFY_DELETE,
APP_CONFIG["template"]["user_dn"].format(
request.form["remove"]
),
)
]
},
) )
flash(f"{request.form['remove']} was removed from {ou}")
elif request.form.get("add"):
ldap.modify(
APP_CONFIG["template"]["group_dn"].format(ou),
{
"member": [
(
MODIFY_ADD,
APP_CONFIG["template"]["user_dn"].format(
request.form["add"]
),
)
]
},
)
flash(f"{request.form['add']} was added to {ou}")
return redirect(url_for("group_edit", ou=ou)) return redirect(url_for("group_edit", ou=ou))
ldap.search(
APP_CONFIG["ldap"]["user_base"],
APP_CONFIG["template"]["group_nonmembers"].format(ou),
attributes=["cn", "uid"],
)
users = ldap.entries
ldap.search( ldap.search(
APP_CONFIG["ldap"]["user_base"], APP_CONFIG["ldap"]["user_base"],
APP_CONFIG["template"]["group_members"].format(ou), APP_CONFIG["template"]["group_members"].format(ou),
@ -171,4 +204,5 @@ def group_edit(ldap, ou):
"groups/members.html", "groups/members.html",
members=ldap.entries, members=ldap.entries,
ou=ou, ou=ou,
other_users=users,
) )

View file

@ -24,4 +24,23 @@
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
<form action="{{ url_for("group_edit", ou=ou) }}" method="post" class="row g-3 needs-validation">
<fieldset>
<legend>add user to group</legend>
<div class="row mb-3">
<label for="add" class="form-label col-sm-2">user</label>
<div class="col-sm-10">
<select name="add" id="add" class="form-select">
<option value="" selected>select user ...</option>
{% for user in other_users|sort %}
<option value="{{ user["uid"] }}">{{ user["cn"] }} ({{ user["uid"] }})</option>
{% endfor %}
</select>
</div>
</div>
<input type="submit" value="add user" class="btn btn-primary mb-3"><br>
</fieldset>
</form>
{% endblock %} {% endblock %}