Cleanup code in admin.go

This commit is contained in:
Alex 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

208
admin.go
View file

@ -127,10 +127,12 @@ type AdminLDAPTplData struct {
Props map[string]*PropValues Props map[string]*PropValues
CanDelete bool CanDelete bool
HasMembers bool HasMembers bool
Members []EntryName Members []EntryName
HasGroups bool PossibleNewMembers []EntryName
Groups []EntryName HasGroups bool
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,31 +384,53 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
} }
members := []EntryName{} members := []EntryName{}
mapDnToNameMember := make(map[string]string) possibleNewMembers := []EntryName{}
searchRequest = ldap.NewSearchRequest( if len(members_dn) > 0 || hasMembers {
config.UserBaseDN, // Lookup all existing users in the server
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, // to know the DN -> display name correspondance
fmt.Sprintf("(objectClass=organizationalPerson)"), searchRequest = ldap.NewSearchRequest(
[]string{"dn", "displayname", "description"}, config.UserBaseDN,
nil) ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
sr, err = login.conn.Search(searchRequest) fmt.Sprintf("(objectClass=organizationalPerson)"),
if err != nil { []string{"dn", "displayname", "description"},
http.Error(w, err.Error(), http.StatusInternalServerError) nil)
return sr, err = login.conn.Search(searchRequest)
} if err != nil {
for _, ent := range sr.Entries { http.Error(w, err.Error(), http.StatusInternalServerError)
mapDnToNameMember[ent.DN] = ent.GetAttributeValue("displayname") return
if mapDnToNameMember[ent.DN] == "" { }
mapDnToNameMember[ent.DN] = ent.GetAttributeValue("description")
userMap := make(map[string]string)
for _, ent := range sr.Entries {
userMap[ent.DN] = ent.GetAttributeValue("displayname")
if userMap[ent.DN] == "" {
userMap[ent.DN] = ent.GetAttributeValue("description")
}
}
// Select members with their name and remove them from map
for _, memdn := range members_dn {
members = append(members, EntryName{
DN: 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)
} }
} }
for _, memdn := range members_dn {
members = append(members, EntryName{
DN: memdn,
Name: mapDnToNameMember[memdn],
})
}
// 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,26 +438,50 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
} }
groups := []EntryName{} groups := []EntryName{}
mapDnToNameGroup := make(map[string]string) possibleNewGroups := []EntryName{}
searchRequest = ldap.NewSearchRequest( if len(groups_dn) > 0 || hasGroups {
config.GroupBaseDN, // Lookup all existing groups in the server
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, // to know the DN -> display name correspondance
fmt.Sprintf("(objectClass=groupOfNames)"), searchRequest = ldap.NewSearchRequest(
[]string{"dn", "description"}, config.GroupBaseDN,
nil) ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
sr, err = login.conn.Search(searchRequest) fmt.Sprintf("(objectClass=groupOfNames)"),
if err != nil { []string{"dn", "description"},
http.Error(w, err.Error(), http.StatusInternalServerError) nil)
return sr, err = login.conn.Search(searchRequest)
} if err != nil {
for _, ent := range sr.Entries { http.Error(w, err.Error(), http.StatusInternalServerError)
mapDnToNameGroup[ent.DN] = ent.GetAttributeValue("description") return
} }
for _, grpdn := range groups_dn {
groups = append(groups, EntryName{ groupMap := make(map[string]string)
DN: grpdn, for _, ent := range sr.Entries {
Name: mapDnToNameGroup[grpdn], 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 {
groups = append(groups, EntryName{
DN: 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,
@ -489,12 +523,12 @@ func handleAdminLDAP(w http.ResponseWriter, r *http.Request) {
CanAddChild: dn_last_attr == "ou" || isOrganization, CanAddChild: dn_last_attr == "ou" || isOrganization,
CanDelete: dn != config.BaseDN && len(children) == 0, CanDelete: dn != config.BaseDN && len(children) == 0,
HasMembers: len(members) > 0 || hasMembers, HasMembers: len(members) > 0 || hasMembers,
Members: members, Members: members,
HasGroups: len(groups) > 0 || hasGroups, PossibleNewMembers: possibleNewMembers,
Groups: groups, HasGroups: len(groups) > 0 || hasGroups,
Groups: groups,
ListMemGro: ExistList, PossibleNewGroups: possibleNewGroups,
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>