Skip to content

Commit

Permalink
fix: sort objects by CN
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevMinerTV committed Oct 11, 2023
1 parent 3b36b88 commit 3cc5a9f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
7 changes: 7 additions & 0 deletions internal/web/computers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package web

import (
"net/url"
"sort"

"github.com/gofiber/fiber/v2"
)
Expand All @@ -18,6 +19,9 @@ func (a *App) computersHandler(c *fiber.Ctx) error {

showDisabled := c.Query("show-disabled", "0") == "1"
computers := a.ldapCache.FindComputers(showDisabled)
sort.SliceStable(computers, func(i, j int) bool {
return computers[i].CN() < computers[j].CN()
})

return c.Render("views/computers", fiber.Map{
"session": sess,
Expand Down Expand Up @@ -50,6 +54,9 @@ func (a *App) computerHandler(c *fiber.Ctx) error {
}

computer := a.ldapCache.PopulateGroupsForComputer(thinComputer)
sort.SliceStable(computer.Groups, func(i, j int) bool {
return computer.Groups[i].CN() < computer.Groups[j].CN()
})

return c.Render("views/computer", fiber.Map{
"session": sess,
Expand Down
22 changes: 22 additions & 0 deletions internal/web/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package web

import (
"net/url"
"sort"

"github.com/gofiber/fiber/v2"
"github.com/netresearch/ldap-manager/internal/ldap_cache"
Expand All @@ -19,6 +20,9 @@ func (a *App) groupsHandler(c *fiber.Ctx) error {
}

groups := a.ldapCache.FindGroups()
sort.SliceStable(groups, func(i, j int) bool {
return groups[i].CN() < groups[j].CN()
})

return c.Render("views/groups", fiber.Map{
"session": sess,
Expand Down Expand Up @@ -52,7 +56,13 @@ func (a *App) groupHandler(c *fiber.Ctx) error {

showDisabledUsers := c.Query("show-disabled", "0") == "1"
group := a.ldapCache.PopulateUsersForGroup(thinGroup, showDisabledUsers)
sort.SliceStable(group.Members, func(i, j int) bool {
return group.Members[i].CN() < group.Members[j].CN()
})
unassignedUsers := a.findUnassignedUsers(group)
sort.SliceStable(unassignedUsers, func(i, j int) bool {
return unassignedUsers[i].CN() < unassignedUsers[j].CN()
})

return c.Render("views/group", fiber.Map{
"session": sess,
Expand Down Expand Up @@ -106,7 +116,13 @@ func (a *App) groupModifyHandler(c *fiber.Ctx) error {

showDisabledUsers := c.Query("show-disabled", "0") == "1"
group := a.ldapCache.PopulateUsersForGroup(thinGroup, showDisabledUsers)
sort.SliceStable(group.Members, func(i, j int) bool {
return group.Members[i].CN() < group.Members[j].CN()
})
unassignedUsers := a.findUnassignedUsers(group)
sort.SliceStable(unassignedUsers, func(i, j int) bool {
return unassignedUsers[i].CN() < unassignedUsers[j].CN()
})

if form.AddUser != nil {
if err := l.AddUserToGroup(*form.AddUser, thinGroup.DN()); err != nil {
Expand Down Expand Up @@ -145,7 +161,13 @@ func (a *App) groupModifyHandler(c *fiber.Ctx) error {
}

group = a.ldapCache.PopulateUsersForGroup(thinGroup, showDisabledUsers)
sort.SliceStable(group.Members, func(i, j int) bool {
return group.Members[i].CN() < group.Members[j].CN()
})
unassignedUsers = a.findUnassignedUsers(group)
sort.SliceStable(unassignedUsers, func(i, j int) bool {
return unassignedUsers[i].CN() < unassignedUsers[j].CN()
})

return c.Render("views/group", fiber.Map{
"session": sess,
Expand Down
23 changes: 22 additions & 1 deletion internal/web/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package web

import (
"net/url"
"sort"

"github.com/gofiber/fiber/v2"
"github.com/netresearch/ldap-manager/internal/ldap_cache"
Expand All @@ -20,6 +21,9 @@ func (a *App) usersHandler(c *fiber.Ctx) error {

showDisabled := c.Query("show-disabled", "0") == "1"
users := a.ldapCache.FindUsers(showDisabled)
sort.SliceStable(users, func(i, j int) bool {
return users[i].CN() < users[j].CN()
})

return c.Render("views/users", fiber.Map{
"session": sess,
Expand Down Expand Up @@ -53,8 +57,13 @@ func (a *App) userHandler(c *fiber.Ctx) error {
}

user := a.ldapCache.PopulateGroupsForUser(thinUser)

sort.SliceStable(user.Groups, func(i, j int) bool {
return user.Groups[i].CN() < user.Groups[j].CN()
})
unassignedGroups := a.findUnassignedGroups(user)
sort.SliceStable(unassignedGroups, func(i, j int) bool {
return unassignedGroups[i].CN() < unassignedGroups[j].CN()
})

return c.Render("views/user", fiber.Map{
"session": sess,
Expand Down Expand Up @@ -112,7 +121,13 @@ func (a *App) userModifyHandler(c *fiber.Ctx) error {
}

user := a.ldapCache.PopulateGroupsForUser(thinUser)
sort.SliceStable(user.Groups, func(i, j int) bool {
return user.Groups[i].CN() < user.Groups[j].CN()
})
unassignedGroups := a.findUnassignedGroups(user)
sort.SliceStable(unassignedGroups, func(i, j int) bool {
return unassignedGroups[i].CN() < unassignedGroups[j].CN()
})

if form.AddGroup != nil {
if err := l.AddUserToGroup(userDN, *form.AddGroup); err != nil {
Expand Down Expand Up @@ -151,7 +166,13 @@ func (a *App) userModifyHandler(c *fiber.Ctx) error {
}

user = a.ldapCache.PopulateGroupsForUser(thinUser)
sort.SliceStable(user.Groups, func(i, j int) bool {
return user.Groups[i].CN() < user.Groups[j].CN()
})
unassignedGroups = a.findUnassignedGroups(user)
sort.SliceStable(unassignedGroups, func(i, j int) bool {
return unassignedGroups[i].CN() < unassignedGroups[j].CN()
})

return c.Render("views/user", fiber.Map{
"session": sess,
Expand Down

0 comments on commit 3cc5a9f

Please sign in to comment.