Add Datalist to help search users or group in LDAP admin page

This commit is contained in:
MrArmonius 2021-07-12 17:08:56 +02:00 committed by Gitea
parent 353f9600f3
commit 3d7b4a9f68
2 changed files with 75 additions and 48 deletions

109
admin.go
View file

@ -132,6 +132,8 @@ type AdminLDAPTplData struct {
HasGroups bool HasGroups bool
Groups []EntryName Groups []EntryName
ListMemGro map[string]string
Error string Error string
Success bool Success bool
} }
@ -360,32 +362,30 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
} }
members := []EntryName{} members := []EntryName{}
if len(members_dn) > 0 { mapDnToNameMember := make(map[string]string)
mapDnToName := make(map[string]string) searchRequest = ldap.NewSearchRequest(
searchRequest = ldap.NewSearchRequest( config.UserBaseDN,
config.UserBaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, fmt.Sprintf("(objectClass=organizationalPerson)"),
fmt.Sprintf("(objectClass=organizationalPerson)"), []string{"dn", "displayname", "description"},
[]string{"dn", "displayname", "description"}, nil)
nil) sr, err = login.conn.Search(searchRequest)
sr, err := login.conn.Search(searchRequest) if err != nil {
if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, err.Error(), http.StatusInternalServerError) return
return }
} for _, ent := range sr.Entries {
for _, ent := range sr.Entries { mapDnToNameMember[ent.DN] = ent.GetAttributeValue("displayname")
mapDnToName[ent.DN] = ent.GetAttributeValue("displayname") if mapDnToNameMember[ent.DN] == "" {
if mapDnToName[ent.DN] == "" { mapDnToNameMember[ent.DN] = ent.GetAttributeValue("description")
mapDnToName[ent.DN] = ent.GetAttributeValue("description")
}
}
for _, memdn := range members_dn {
members = append(members, EntryName{
DN: memdn,
Name: mapDnToName[memdn],
})
} }
} }
for _, memdn := range members_dn {
members = append(members, EntryName{
DN: memdn,
Name: mapDnToNameMember[memdn],
})
}
groups_dn := []string{} groups_dn := []string{}
if gp, ok := props["memberof"]; ok { if gp, ok := props["memberof"]; ok {
@ -394,28 +394,26 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
} }
groups := []EntryName{} groups := []EntryName{}
if len(groups_dn) > 0 { mapDnToNameGroup := make(map[string]string)
mapDnToName := make(map[string]string) searchRequest = ldap.NewSearchRequest(
searchRequest = ldap.NewSearchRequest( config.GroupBaseDN,
config.GroupBaseDN, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, fmt.Sprintf("(objectClass=groupOfNames)"),
fmt.Sprintf("(objectClass=groupOfNames)"), []string{"dn", "description"},
[]string{"dn", "description"}, nil)
nil) sr, err = login.conn.Search(searchRequest)
sr, err := login.conn.Search(searchRequest) if err != nil {
if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError)
http.Error(w, err.Error(), http.StatusInternalServerError) return
return }
} for _, ent := range sr.Entries {
for _, ent := range sr.Entries { mapDnToNameGroup[ent.DN] = ent.GetAttributeValue("description")
mapDnToName[ent.DN] = ent.GetAttributeValue("description") }
} for _, grpdn := range groups_dn {
for _, grpdn := range groups_dn { groups = append(groups, EntryName{
groups = append(groups, EntryName{ DN: grpdn,
DN: grpdn, Name: mapDnToNameGroup[grpdn],
Name: mapDnToName[grpdn], })
})
}
} }
// Get children // Get children
@ -465,6 +463,23 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
} }
} }
//Get the members or groups existing
var ExistList map[string]string
if hasMembers {
ExistList = mapDnToNameMember
//Suppress the members already in the group
for _, value := range members {
delete(ExistList, value.DN)
}
} else if hasGroups {
ExistList = mapDnToNameGroup
//Delete the groups where the user is already a member
for _, value := range groups {
delete(ExistList, value.DN)
}
}
templateAdminLDAP.Execute(w, &AdminLDAPTplData{ templateAdminLDAP.Execute(w, &AdminLDAPTplData{
DN: dn, DN: dn,
@ -479,6 +494,8 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
HasGroups: len(groups) > 0 || hasGroups, HasGroups: len(groups) > 0 || hasGroups,
Groups: groups, Groups: groups,
ListMemGro: ExistList,
Error: dError, Error: dError,
Success: dSuccess, Success: dSuccess,
}) })

View file

@ -146,7 +146,12 @@
<div class="col-md-3"><strong>Ajouter au groupe :</strong> <div class="col-md-3"><strong>Ajouter au groupe :</strong>
</div> </div>
<div class="col-md-5"> <div class="col-md-5">
<input class="form-control" type="text" name="values" placeholder="Utilisateur..." /> <input class="form-control" type="text" list="users" name="values" placeholder="Utilisateur..." />
<datalist id="users">
{{ range $key, $value := .ListMemGro}}
<option value={{$key}}>{{$value}}</option>
{{end}}
</datalist>
</div> </div>
<div class="col-md-2"> <div class="col-md-2">
<input type="submit" value="Ajouter" class="form-control btn btn-success btn-sm" /> <input type="submit" value="Ajouter" class="form-control btn btn-success btn-sm" />
@ -182,7 +187,12 @@
<div class="col-md-3"><strong>Nouveau groupe :</strong> <div class="col-md-3"><strong>Nouveau groupe :</strong>
</div> </div>
<div class="col-md-5"> <div class="col-md-5">
<input class="form-control" type="text" name="group" placeholder="Groupe..." /> <input class="form-control" list="groups" type="text" name="group" placeholder="Groupe..." />
<datalist id="groups">
{{ range $key, $value := .ListMemGro}}
<option value={{$key}}>{{$value}}</option>
{{end}}
</datalist>
</div> </div>
<div class="col-md-2"> <div class="col-md-2">
<input type="submit" value="Ajouter" class="form-control btn btn-success btn-sm" /> <input type="submit" value="Ajouter" class="form-control btn btn-success btn-sm" />