UI Verbesserungen in Web App, Passwort/Lektoren
This commit is contained in:
1 parent
692b9b6ef8
commit
d7113afa5f
23 files changed
+1340
-116
No files matched your search
+68
-25
@@ -49,14 +49,25 @@ func (r UserRole) IsAdmin() bool { return r == RoleAdmin }
|
||||
|
||||
// User is a login account. Memberships are loaded on demand.
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"displayName"`
|
||||
PasswordHash string `json:"-"`
|
||||
Role UserRole `json:"role"`
|
||||
Active bool `json:"active"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
TenantIDs []string `json:"tenantIDs,omitempty"`
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"displayName"`
|
||||
PasswordHash string `json:"-"`
|
||||
Role UserRole `json:"role"`
|
||||
Active bool `json:"active"`
|
||||
MustChangePassword bool `json:"mustChangePassword"`
|
||||
LinkedTenantID string `json:"linkedTenantID,omitempty"`
|
||||
LinkedPersonID string `json:"linkedPersonID,omitempty"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
TenantIDs []string `json:"tenantIDs,omitempty"`
|
||||
}
|
||||
|
||||
// LinkedPerson returns the tenant-scoped person linked to this account, if any.
|
||||
func (u *User) LinkedPerson(tenantID string) (string, bool) {
|
||||
if u == nil || u.LinkedPersonID == "" || u.LinkedTenantID != tenantID {
|
||||
return "", false
|
||||
}
|
||||
return u.LinkedPersonID, true
|
||||
}
|
||||
|
||||
// RoleTitle is a template helper.
|
||||
@@ -65,7 +76,8 @@ func (u *User) RoleTitle() string { return UserRoleTitle(u.Role) }
|
||||
// Users lists all accounts with their memberships.
|
||||
func (s *Store) Users(ctx context.Context) ([]User, error) {
|
||||
rows, err := s.db.QueryContext(ctx,
|
||||
"SELECT id, username, display_name, password_hash, role, active, created_at FROM users ORDER BY username COLLATE NOCASE")
|
||||
"SELECT id, username, display_name, password_hash, role, active, must_change_password, "+
|
||||
"linked_tenant_id, linked_person_id, created_at FROM users ORDER BY username COLLATE NOCASE")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -100,7 +112,8 @@ func (s *Store) Users(ctx context.Context) ([]User, error) {
|
||||
// User loads an account by id.
|
||||
func (s *Store) User(ctx context.Context, id string) (*User, error) {
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
"SELECT id, username, display_name, password_hash, role, active, created_at FROM users WHERE id=?", id)
|
||||
"SELECT id, username, display_name, password_hash, role, active, must_change_password, "+
|
||||
"linked_tenant_id, linked_person_id, created_at FROM users WHERE id=?", id)
|
||||
user, err := scanUser(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
@@ -119,7 +132,8 @@ func (s *Store) User(ctx context.Context, id string) (*User, error) {
|
||||
// UserByUsername loads an account by login name.
|
||||
func (s *Store) UserByUsername(ctx context.Context, username string) (*User, error) {
|
||||
row := s.db.QueryRowContext(ctx,
|
||||
"SELECT id, username, display_name, password_hash, role, active, created_at FROM users WHERE username=? COLLATE NOCASE",
|
||||
"SELECT id, username, display_name, password_hash, role, active, must_change_password, "+
|
||||
"linked_tenant_id, linked_person_id, created_at FROM users WHERE username=? COLLATE NOCASE",
|
||||
strings.TrimSpace(username))
|
||||
user, err := scanUser(row)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
@@ -143,12 +157,14 @@ type rowScanner interface {
|
||||
func scanUser(row rowScanner) (*User, error) {
|
||||
var user User
|
||||
var role, created string
|
||||
var active int
|
||||
if err := row.Scan(&user.ID, &user.Username, &user.DisplayName, &user.PasswordHash, &role, &active, &created); err != nil {
|
||||
var active, mustChange int
|
||||
if err := row.Scan(&user.ID, &user.Username, &user.DisplayName, &user.PasswordHash,
|
||||
&role, &active, &mustChange, &user.LinkedTenantID, &user.LinkedPersonID, &created); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
user.Role = UserRole(role)
|
||||
user.Active = active != 0
|
||||
user.MustChangePassword = mustChange != 0
|
||||
user.CreatedAt = parseTime(created)
|
||||
return &user, nil
|
||||
}
|
||||
@@ -199,14 +215,16 @@ func (s *Store) CreateUser(ctx context.Context, username, displayName, password
|
||||
}
|
||||
user := &User{
|
||||
ID: domain.NewID(), Username: username, DisplayName: displayName,
|
||||
PasswordHash: hash, Role: role, Active: active, CreatedAt: time.Now().UTC(),
|
||||
PasswordHash: hash, Role: role, Active: active, MustChangePassword: true,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
err = s.transaction(ctx, func(tx *sql.Tx) error {
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO users(id, username, display_name, password_hash, role, active, created_at)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?)`,
|
||||
INSERT INTO users(id, username, display_name, password_hash, role, active,
|
||||
must_change_password, linked_tenant_id, linked_person_id, created_at)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
user.ID, user.Username, user.DisplayName, user.PasswordHash, string(user.Role),
|
||||
boolInt(user.Active), user.CreatedAt.Format(time.RFC3339)); err != nil {
|
||||
boolInt(user.Active), 1, "", "", user.CreatedAt.Format(time.RFC3339)); err != nil {
|
||||
return err
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
@@ -233,15 +251,25 @@ func (s *Store) CreateUser(ctx context.Context, username, displayName, password
|
||||
}
|
||||
|
||||
// UpdateUser saves account fields. An empty password keeps the old hash.
|
||||
func (s *Store) UpdateUser(ctx context.Context, id, username, displayName, password string, role UserRole, active bool, tenantIDs []string) error {
|
||||
// Setting a new password by an administrator also forces a change at the next
|
||||
// login. linkedTenantID/linkedPersonID may be empty to remove the link.
|
||||
func (s *Store) UpdateUser(ctx context.Context, id, username, displayName, password string, role UserRole, active bool, tenantIDs []string, forcePasswordChange bool, linkedTenantID, linkedPersonID string) error {
|
||||
username = strings.TrimSpace(username)
|
||||
displayName = strings.TrimSpace(displayName)
|
||||
linkedTenantID = strings.TrimSpace(linkedTenantID)
|
||||
linkedPersonID = strings.TrimSpace(linkedPersonID)
|
||||
if username == "" || displayName == "" {
|
||||
return errors.New("Benutzername und Anzeigename sind erforderlich")
|
||||
}
|
||||
if !role.IsValid() {
|
||||
return errors.New("Unbekannte Rolle")
|
||||
}
|
||||
if role != RoleReader {
|
||||
linkedTenantID, linkedPersonID = "", ""
|
||||
}
|
||||
if linkedPersonID == "" {
|
||||
linkedTenantID = ""
|
||||
}
|
||||
return s.transaction(ctx, func(tx *sql.Tx) error {
|
||||
if password != "" {
|
||||
hash, err := HashPassword(password)
|
||||
@@ -249,14 +277,19 @@ func (s *Store) UpdateUser(ctx context.Context, id, username, displayName, passw
|
||||
return err
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
"UPDATE users SET username=?, display_name=?, role=?, active=?, password_hash=? WHERE id=?",
|
||||
username, displayName, string(role), boolInt(active), hash, id); err != nil {
|
||||
"UPDATE users SET username=?, display_name=?, role=?, active=?, password_hash=?, "+
|
||||
"must_change_password=?, linked_tenant_id=?, linked_person_id=? WHERE id=?",
|
||||
username, displayName, string(role), boolInt(active), hash,
|
||||
boolInt(forcePasswordChange || password != ""), linkedTenantID, linkedPersonID, id); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
"UPDATE users SET username=?, display_name=?, role=?, active=? WHERE id=?",
|
||||
username, displayName, string(role), boolInt(active), id); err != nil {
|
||||
"UPDATE users SET username=?, display_name=?, role=?, active=?, "+
|
||||
"must_change_password=CASE WHEN ? THEN 1 ELSE must_change_password END, "+
|
||||
"linked_tenant_id=?, linked_person_id=? WHERE id=?",
|
||||
username, displayName, string(role), boolInt(active),
|
||||
boolInt(forcePasswordChange), linkedTenantID, linkedPersonID, id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -278,13 +311,23 @@ func (s *Store) UpdateUser(ctx context.Context, id, username, displayName, passw
|
||||
})
|
||||
}
|
||||
|
||||
// SetUserPassword updates only the password hash.
|
||||
func (s *Store) SetUserPassword(ctx context.Context, id, password string) error {
|
||||
// SetUserPassword updates the password hash and the forced-change flag.
|
||||
func (s *Store) SetUserPassword(ctx context.Context, id, password string, mustChange bool) error {
|
||||
hash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.db.ExecContext(ctx, "UPDATE users SET password_hash=? WHERE id=?", hash, id)
|
||||
_, err = s.db.ExecContext(ctx,
|
||||
"UPDATE users SET password_hash=?, must_change_password=? WHERE id=?",
|
||||
hash, boolInt(mustChange), id)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetUserPersonLink ties a reader account to a person inside one tenant.
|
||||
func (s *Store) SetUserPersonLink(ctx context.Context, id, tenantID, personID string) error {
|
||||
_, err := s.db.ExecContext(ctx,
|
||||
"UPDATE users SET linked_tenant_id=?, linked_person_id=? WHERE id=?",
|
||||
strings.TrimSpace(tenantID), strings.TrimSpace(personID), id)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
+47
-1
@@ -97,6 +97,9 @@ CREATE TABLE IF NOT EXISTS users (
|
||||
password_hash TEXT NOT NULL,
|
||||
role TEXT NOT NULL,
|
||||
active INTEGER NOT NULL DEFAULT 1,
|
||||
must_change_password INTEGER NOT NULL DEFAULT 1,
|
||||
linked_tenant_id TEXT NOT NULL DEFAULT '',
|
||||
linked_person_id TEXT NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
@@ -253,14 +256,57 @@ func (s *Store) migrate(ctx context.Context) error {
|
||||
if _, err := s.db.ExecContext(ctx, schema); err != nil {
|
||||
return fmt.Errorf("apply schema: %w", err)
|
||||
}
|
||||
// Existing databases predate the password-state and person-link columns.
|
||||
// CREATE TABLE IF NOT EXISTS does not modify them, so add the columns here.
|
||||
columns := []struct {
|
||||
name string
|
||||
definition string
|
||||
}{
|
||||
{"must_change_password", "INTEGER NOT NULL DEFAULT 1"},
|
||||
{"linked_tenant_id", "TEXT NOT NULL DEFAULT ''"},
|
||||
{"linked_person_id", "TEXT NOT NULL DEFAULT ''"},
|
||||
}
|
||||
for _, column := range columns {
|
||||
exists, err := s.hasColumn(ctx, "users", column.name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("inspect users table: %w", err)
|
||||
}
|
||||
if exists {
|
||||
continue
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx,
|
||||
"ALTER TABLE users ADD COLUMN "+column.name+" "+column.definition); err != nil {
|
||||
return fmt.Errorf("add users.%s: %w", column.name, err)
|
||||
}
|
||||
}
|
||||
if _, err := s.db.ExecContext(ctx,
|
||||
"INSERT INTO schema_meta(key, value) VALUES('schemaVersion', '1') "+
|
||||
"INSERT INTO schema_meta(key, value) VALUES('schemaVersion', '2') "+
|
||||
"ON CONFLICT(key) DO UPDATE SET value=excluded.value"); err != nil {
|
||||
return fmt.Errorf("record schema version: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) hasColumn(ctx context.Context, table, column string) (bool, error) {
|
||||
rows, err := s.db.QueryContext(ctx, "PRAGMA table_info("+table+")")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var cid, notNull, pk int
|
||||
var name, kind string
|
||||
var defaultValue sql.NullString
|
||||
if err := rows.Scan(&cid, &name, &kind, ¬Null, &defaultValue, &pk); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if name == column {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) transaction(ctx context.Context, fn func(*sql.Tx) error) error {
|
||||
tx, err := s.db.BeginTx(ctx, nil)
|
||||
if err != nil {
|
||||
|
||||
@@ -98,6 +98,24 @@ func (s *Store) SpecialServices(ctx context.Context, tenantID string) ([]domain.
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// LatestServiceDate returns the last date with planning data, if any.
|
||||
func (s *Store) LatestServiceDate(ctx context.Context, tenantID string) (*time.Time, error) {
|
||||
var value sql.NullString
|
||||
err := s.db.QueryRowContext(ctx,
|
||||
"SELECT MAX(date) FROM services WHERE tenant_id=?", tenantID).Scan(&value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !value.Valid || value.String == "" {
|
||||
return nil, nil
|
||||
}
|
||||
date, ok := domain.DateFromKey(value.String)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
return &date, nil
|
||||
}
|
||||
|
||||
// ServiceCount returns the number of occurrences in a tenant.
|
||||
func (s *Store) ServiceCount(ctx context.Context, tenantID string) (int, error) {
|
||||
var count int
|
||||
|
||||
Reference in new issue
Block a user