Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Use more consistent ordering on realm selection and lists pages (#632)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo authored Sep 22, 2020
1 parent 3396b91 commit ce15731
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 5 additions & 2 deletions pkg/database/realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func (r *Realm) FindAuthorizedApp(db *Database, id interface{}) (*AuthorizedApp,
if err := db.db.
Unscoped().
Model(AuthorizedApp{}).
Order("LOWER(name)").
Order("LOWER(name) ASC").
Where("id = ? AND realm_id = ?", id, r.ID).
First(&app).
Error; err != nil {
Expand Down Expand Up @@ -615,7 +615,10 @@ func (db *Database) FindRealm(id interface{}) (*Realm, error) {

func (db *Database) GetRealms() ([]*Realm, error) {
var realms []*Realm
if err := db.db.Find(&realms).Error; err != nil {
if err := db.db.
Order("name ASC").
Find(&realms).
Error; err != nil {
return nil, err
}
return realms, nil
Expand Down
17 changes: 16 additions & 1 deletion pkg/database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package database
import (
"context"
"fmt"
"sort"
"strings"
"time"

Expand All @@ -40,6 +41,20 @@ type User struct {
LastPasswordChange time.Time
}

// AfterFind runs after the record is found.
func (u *User) AfterFind(tx *gorm.DB) error {
// Sort Realms and Admin realms. Unfortunately gorm provides no way to do this
// via sql hooks or default scopes.
sort.Slice(u.Realms, func(i, j int) bool {
return strings.ToLower(u.Realms[i].Name) < strings.ToLower(u.Realms[j].Name)
})
sort.Slice(u.AdminRealms, func(i, j int) bool {
return strings.ToLower(u.AdminRealms[i].Name) < strings.ToLower(u.AdminRealms[j].Name)
})

return nil
}

// PasswordAgeString displays the age of the password in friendly text.
func (u *User) PasswordAgeString() string {
ago := time.Since(u.LastPasswordChange)
Expand Down Expand Up @@ -196,7 +211,7 @@ func (db *Database) ListSystemAdmins() ([]*User, error) {
if err := db.db.
Model(&User{}).
Where("admin IS TRUE").
Order("name DESC").
Order("LOWER(name) ASC").
Find(&users).
Error; err != nil {
if IsNotFound(err) {
Expand Down

0 comments on commit ce15731

Please sign in to comment.