Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Add missing sortability to users page and APIs.

Kailash Nadh052e7c6f050a
diff --git a/cmd/users.go b/cmd/users.go
index a3656f5e..553e40a3 100644
--- a/cmd/users.go
+++ b/cmd/users.go
@@ -25,6 +25,8 @@ var (
"status": "",
"user_role_id": "",
"list_role_id": "",
+ "order_by": "created_at",
+ "order": "asc",
}
)
@@ -70,7 +72,7 @@ func (a *App) ViewUsers(c echo.Context) error {
)
// Filtering is done in SQL.
- users, err := a.core.GetUsers(fType, fStatus, fUserRole, fListRole)
+ users, err := a.core.GetUsers(fType, fStatus, fUserRole, fListRole, q.Get("order_by"), q.Get("order"))
if err != nil {
return err
}
@@ -200,7 +202,7 @@ func (a *App) GetUser(c echo.Context) error {
// GetUsers retrieves all users.
func (a *App) GetUsers(c echo.Context) error {
// Get all users from the DB.
- out, err := a.core.GetUsers("", "", 0, 0)
+ out, err := a.core.GetUsers("", "", 0, 0, c.QueryParam("order_by"), c.QueryParam("order"))
if err != nil {
return err
}
@@ -528,7 +530,7 @@ func (a *App) DisableTOTP(c echo.Context) error {
// It also returns a bool indicating whether there are any actual users in the DB at all,
// which if there aren't, the first time user setup needs to be run.
func cacheUsers(co *core.Core, a *auth.Auth) (bool, error) {
- users, err := co.GetUsers("", "", 0, 0)
+ users, err := co.GetUsers("", "", 0, 0, "", "")
if err != nil {
return false, err
}
diff --git a/internal/core/core.go b/internal/core/core.go
index 7830b66f..0bab39c9 100644
--- a/internal/core/core.go
+++ b/internal/core/core.go
@@ -71,6 +71,7 @@ var (
regexpSpaces = regexp.MustCompile(`[\s]+`)
campQuerySortFields = []string{"name", "status", "created_at", "updated_at"}
subQuerySortFields = []string{"email", "status", "name", "created_at", "updated_at"}
+ userQuerySortFields = []string{"username", "email", "created_at", "loggedin_at"}
listQuerySortFields = []string{"name", "status", "created_at", "updated_at", "subscriber_count"}
)
diff --git a/internal/core/users.go b/internal/core/users.go
index 3652169d..4055b668 100644
--- a/internal/core/users.go
+++ b/internal/core/users.go
@@ -14,9 +14,16 @@ import (
)
// GetUsers retrieves users.
-func (c *Core) GetUsers(typ, status string, userRoleID, listRoleID int) ([]auth.User, error) {
- out := []auth.User{}
- if err := c.q.GetUsers.Select(&out, typ, status, userRoleID, listRoleID); err != nil {
+func (c *Core) GetUsers(typ, status string, userRoleID, listRoleID int, orderBy, order string) ([]auth.User, error) {
+ if order == "" {
+ order = SortAsc
+ }
+
+ var (
+ out = []auth.User{}
+ _, stmt = makeSearchQuery("", orderBy, order, c.q.GetUsers, userQuerySortFields)
+ )
+ if err := c.db.Select(&out, stmt, typ, status, userRoleID, listRoleID); err != nil {
return nil, echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.users}", "error", pqErrMsg(err)))
}
diff --git a/models/queries.go b/models/queries.go
index d891b4cb..4697ea20 100644
--- a/models/queries.go
+++ b/models/queries.go
@@ -125,7 +125,7 @@ type Queries struct {
UpdateUserLogin *sqlx.Stmt `query:"update-user-login"`
SetUserTwoFA *sqlx.Stmt `query:"set-user-twofa"`
DeleteUsers *sqlx.Stmt `query:"delete-users"`
- GetUsers *sqlx.Stmt `query:"get-users"`
+ GetUsers string `query:"get-users"`
GetUser *sqlx.Stmt `query:"get-user"`
GetAPITokens *sqlx.Stmt `query:"get-api-tokens"`
LoginUser *sqlx.Stmt `query:"login-user"`
diff --git a/queries/users.sql b/queries/users.sql
index 9749fa61..39ce915b 100644
--- a/queries/users.sql
+++ b/queries/users.sql
@@ -93,7 +93,7 @@ FROM users
AND ($2 = '' OR users.status = $2::user_status)
AND ($3 = 0 OR users.user_role_id = $3)
AND ($4 = 0 OR users.list_role_id = $4)
- ORDER BY users.created_at;
+ ORDER BY %order%;
-- name: get-user
WITH sel AS (
diff --git a/static/admin/views/users.html b/static/admin/views/users.html
index 1eda4fcb..d2ed45c3 100644
--- a/static/admin/views/users.html
+++ b/static/admin/views/users.html
@@ -58,11 +58,11 @@
{{- if $d.Can "users:manage" }}
<th width="5%"><input type="checkbox" aria-label="{{ .L.T "globals.buttons.toggleSelect" }}"></th>
{{- end }}
- <th>{{ .L.T "users.username" }}</th>
+ <th>{{ $d.Page.SortLink $base "username" (.L.T "users.username") }}</th>
<th>{{ .L.Tc "users.role" 1 }}</th>
- <th>{{ .L.T "subscribers.email" }}</th>
- <th>{{ .L.T "globals.fields.createdAt" }}</th>
- <th>{{ .L.T "users.lastLogin" }}</th>
+ <th>{{ $d.Page.SortLink $base "email" (.L.T "subscribers.email") }}</th>
+ <th>{{ $d.Page.SortLink $base "created_at" (.L.T "globals.fields.createdAt") }}</th>
+ <th>{{ $d.Page.SortLink $base "loggedin_at" (.L.T "users.lastLogin") }}</th>
<th class="actions align-right"></th>
</tr>
</thead>