Cleanup code in admin.go

This commit is contained in:
Alex Auvolat 2021-08-16 12:53:35 +02:00
parent 3d7b4a9f68
commit 0a1ac27efb
No known key found for this signature in database
GPG Key ID: EDABF9711E244EB1
2 changed files with 125 additions and 91 deletions

124
admin.go
View File

@ -129,8 +129,10 @@ type AdminLDAPTplData struct {
HasMembers bool HasMembers bool
Members []EntryName Members []EntryName
PossibleNewMembers []EntryName
HasGroups bool HasGroups bool
Groups []EntryName Groups []EntryName
PossibleNewGroups []EntryName
ListMemGro map[string]string ListMemGro map[string]string
@ -321,6 +323,7 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
object := sr.Entries[0] object := sr.Entries[0]
// Read object properties and prepare appropriate form fields
props := make(map[string]*PropValues) props := make(map[string]*PropValues)
for _, attr := range object.Attributes { for _, attr := range object.Attributes {
name_lower := strings.ToLower(attr.Name) name_lower := strings.ToLower(attr.Name)
@ -355,6 +358,25 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
} }
} }
// Check objectclass to determine object type
objectClass := []string{}
if val, ok := props["objectclass"]; ok {
objectClass = val.Values
}
hasMembers, hasGroups, isOrganization := false, false, false
for _, oc := range objectClass {
if strings.EqualFold(oc, "organizationalperson") || strings.EqualFold(oc, "person") {
hasGroups = true
}
if strings.EqualFold(oc, "groupOfNames") {
hasMembers = true
}
if strings.EqualFold(oc, "organization") {
isOrganization = true
}
}
// Parse member list and prepare form section
members_dn := []string{} members_dn := []string{}
if mp, ok := props["member"]; ok { if mp, ok := props["member"]; ok {
members_dn = mp.Values members_dn = mp.Values
@ -362,7 +384,10 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
} }
members := []EntryName{} members := []EntryName{}
mapDnToNameMember := make(map[string]string) possibleNewMembers := []EntryName{}
if len(members_dn) > 0 || hasMembers {
// Lookup all existing users in the server
// to know the DN -> display name correspondance
searchRequest = ldap.NewSearchRequest( searchRequest = ldap.NewSearchRequest(
config.UserBaseDN, config.UserBaseDN,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
@ -374,19 +399,38 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
userMap := make(map[string]string)
for _, ent := range sr.Entries { for _, ent := range sr.Entries {
mapDnToNameMember[ent.DN] = ent.GetAttributeValue("displayname") userMap[ent.DN] = ent.GetAttributeValue("displayname")
if mapDnToNameMember[ent.DN] == "" { if userMap[ent.DN] == "" {
mapDnToNameMember[ent.DN] = ent.GetAttributeValue("description") userMap[ent.DN] = ent.GetAttributeValue("description")
} }
} }
// Select members with their name and remove them from map
for _, memdn := range members_dn { for _, memdn := range members_dn {
members = append(members, EntryName{ members = append(members, EntryName{
DN: memdn, DN: memdn,
Name: mapDnToNameMember[memdn], Name: userMap[memdn],
}) })
delete(userMap, memdn)
} }
// Create list of members that can be added
for dn, name := range userMap {
entry := EntryName{
DN: dn,
Name: name,
}
if entry.Name == "" {
entry.Name = entry.DN
}
possibleNewMembers = append(possibleNewMembers, entry)
}
}
// Parse group list and prepare form section
groups_dn := []string{} groups_dn := []string{}
if gp, ok := props["memberof"]; ok { if gp, ok := props["memberof"]; ok {
groups_dn = gp.Values groups_dn = gp.Values
@ -394,7 +438,10 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
} }
groups := []EntryName{} groups := []EntryName{}
mapDnToNameGroup := make(map[string]string) possibleNewGroups := []EntryName{}
if len(groups_dn) > 0 || hasGroups {
// Lookup all existing groups in the server
// to know the DN -> display name correspondance
searchRequest = ldap.NewSearchRequest( searchRequest = ldap.NewSearchRequest(
config.GroupBaseDN, config.GroupBaseDN,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
@ -406,14 +453,35 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
groupMap := make(map[string]string)
for _, ent := range sr.Entries { for _, ent := range sr.Entries {
mapDnToNameGroup[ent.DN] = ent.GetAttributeValue("description") groupMap[ent.DN] = ent.GetAttributeValue("displayname")
if groupMap[ent.DN] == "" {
groupMap[ent.DN] = ent.GetAttributeValue("description")
} }
}
// Calculate list of current groups
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: groupMap[grpdn],
}) })
delete(groupMap, grpdn)
}
// Calculate list of possible new groups
for dn, name := range groupMap {
entry := EntryName{
DN: dn,
Name: name,
}
if entry.Name == "" {
entry.Name = entry.DN
}
possibleNewGroups = append(possibleNewGroups, entry)
}
} }
// Get children // Get children
@ -445,41 +513,7 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
}) })
} }
// Checkup objectclass // Run template, finally!
objectClass := []string{}
if val, ok := props["objectclass"]; ok {
objectClass = val.Values
}
hasMembers, hasGroups, isOrganization := false, false, false
for _, oc := range objectClass {
if strings.EqualFold(oc, "organizationalperson") || strings.EqualFold(oc, "person") {
hasGroups = true
}
if strings.EqualFold(oc, "groupOfNames") {
hasMembers = true
}
if strings.EqualFold(oc, "organization") {
isOrganization = true
}
}
//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,
@ -491,10 +525,10 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
HasMembers: len(members) > 0 || hasMembers, HasMembers: len(members) > 0 || hasMembers,
Members: members, Members: members,
PossibleNewMembers: possibleNewMembers,
HasGroups: len(groups) > 0 || hasGroups, HasGroups: len(groups) > 0 || hasGroups,
Groups: groups, Groups: groups,
PossibleNewGroups: possibleNewGroups,
ListMemGro: ExistList,
Error: dError, Error: dError,
Success: dSuccess, Success: dSuccess,

View File

@ -148,8 +148,8 @@
<div class="col-md-5"> <div class="col-md-5">
<input class="form-control" type="text" list="users" name="values" placeholder="Utilisateur..." /> <input class="form-control" type="text" list="users" name="values" placeholder="Utilisateur..." />
<datalist id="users"> <datalist id="users">
{{ range $key, $value := .ListMemGro}} {{range .PossibleNewMembers}}
<option value={{$key}}>{{$value}}</option> <option value="{{.DN}}">{{.Name}}</option>
{{end}} {{end}}
</datalist> </datalist>
</div> </div>
@ -189,8 +189,8 @@
<div class="col-md-5"> <div class="col-md-5">
<input class="form-control" list="groups" type="text" name="group" placeholder="Groupe..." /> <input class="form-control" list="groups" type="text" name="group" placeholder="Groupe..." />
<datalist id="groups"> <datalist id="groups">
{{ range $key, $value := .ListMemGro}} {{range .PossibleNewGroups}}
<option value={{$key}}>{{$value}}</option> <option value="{{.DN}}">{{.Name}}</option>
{{end}} {{end}}
</datalist> </datalist>
</div> </div>