feat(webui): add user accounts list, admin middleware and admin restricted menu entries

This commit is contained in:
Julien Dessaux 2025-02-26 00:07:35 +01:00
parent fb98b4fffe
commit e23a146490
Signed by: adyxax
GPG key ID: F92E51B86E07177E
9 changed files with 143 additions and 2 deletions

View file

@ -54,6 +54,42 @@ func (db *DB) InitAdminAccount() error {
})
}
func (db *DB) LoadAccounts() ([]model.Account, error) {
rows, err := db.Query(
`SELECT id, username, salt, password_hash, is_admin, created, last_login, settings FROM accounts;`)
if err != nil {
return nil, fmt.Errorf("failed to load accounts from database: %w", err)
}
defer rows.Close()
accounts := make([]model.Account, 0)
for rows.Next() {
var (
account model.Account
created int64
lastLogin int64
)
err = rows.Scan(
&account.Id,
&account.Username,
&account.Salt,
&account.PasswordHash,
&account.IsAdmin,
&created,
&lastLogin,
&account.Settings)
if err != nil {
return nil, fmt.Errorf("failed to load account from row: %w", err)
}
account.Created = time.Unix(created, 0)
account.LastLogin = time.Unix(lastLogin, 0)
accounts = append(accounts, account)
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("failed to load accounts from rows: %w", err)
}
return accounts, nil
}
func (db *DB) LoadAccountUsernames() (map[string]string, error) {
rows, err := db.Query(
`SELECT id, username FROM accounts;`)

View file

@ -122,8 +122,8 @@ func (db *DB) LoadStates() ([]model.State, error) {
defer rows.Close()
states := make([]model.State, 0)
for rows.Next() {
var state model.State
var (
state model.State
created int64
updated int64
)