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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user