list users

This commit is contained in:
lowcarbdev
2026-01-17 21:14:15 -07:00
parent 0936472932
commit 8c054905c3
3 changed files with 89 additions and 0 deletions
+26
View File
@@ -82,6 +82,32 @@ MIT
## FAQ
Q: How do I list all users?
Docker:
```bash
docker exec -it <container_name> /app/sbv -list-users
```
Binary:
```bash
./sbv -list-users
```
Q: How do I reset a user's password?
Docker:
```bash
docker exec -it <container_name> /app/sbv -reset-password <username>
```
Binary:
```bash
./sbv -reset-password <username>
```
You will be prompted to enter and confirm the new password.
Q: What backups does this program support?
XML backups from the [SMS Backup & Restore app](https://play.google.com/store/apps/details?id=com.riteshsahu.SMSBackupRestore&hl=en_US). Android devices are supported. iPhone (iOS) devices are not supported by SMS Backup & Restore.
+26
View File
@@ -223,3 +223,29 @@ func UpdatePassword(userID string, newPassword string) error {
return nil
}
// ListUsers returns all users in the database
func ListUsers() ([]User, error) {
rows, err := authDB.Query("SELECT id, username, password_hash, created_at FROM users ORDER BY username")
if err != nil {
return nil, fmt.Errorf("failed to query users: %w", err)
}
defer rows.Close()
var users []User
for rows.Next() {
var user User
var createdAt int64
if err := rows.Scan(&user.ID, &user.Username, &user.PasswordHash, &createdAt); err != nil {
return nil, fmt.Errorf("failed to scan user: %w", err)
}
user.CreatedAt = time.Unix(createdAt, 0)
users = append(users, user)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("error iterating users: %w", err)
}
return users, nil
}
+37
View File
@@ -7,6 +7,8 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"text/tabwriter"
"time"
"github.com/labstack/echo/v4"
@@ -20,6 +22,7 @@ var logger *slog.Logger
func main() {
// Parse CLI flags
resetPassword := flag.String("reset-password", "", "Reset password for the specified username")
listUsers := flag.Bool("list-users", false, "List all users")
flag.Parse()
// Initialize slog logger
@@ -51,6 +54,15 @@ func main() {
os.Exit(0)
}
// Handle list users if requested
if *listUsers {
if err := handleListUsers(dbPathPrefix); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
// Create Echo instance
e := echo.New()
@@ -209,3 +221,28 @@ func handleResetPassword(username string) error {
fmt.Printf("Password reset successfully for user '%s'\n", username)
return nil
}
// handleListUsers lists all users with their usernames, UUIDs, and ingest directories
func handleListUsers(dbPathPrefix string) error {
users, err := internal.ListUsers()
if err != nil {
return fmt.Errorf("failed to list users: %w", err)
}
if len(users) == 0 {
fmt.Println("No users found.")
return nil
}
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "USERNAME\tUUID\tINGEST DIRECTORY")
fmt.Fprintln(w, "--------\t----\t----------------")
for _, user := range users {
ingestDir := filepath.Join(dbPathPrefix, "data", user.ID, "ingest")
fmt.Fprintf(w, "%s\t%s\t%s\n", user.Username, user.ID, ingestDir)
}
w.Flush()
return nil
}