Files

174 lines
4.5 KiB
Go
Raw Permalink Normal View History

2024-12-30 15:46:18 +01:00
package user
import (
2025-01-04 21:29:33 +01:00
"bamort/database"
"fmt"
"strings"
2024-12-30 15:46:18 +01:00
"time"
2025-01-04 21:29:33 +01:00
"gorm.io/gorm"
2024-12-30 15:46:18 +01:00
)
2025-12-30 08:00:04 +01:00
// Role constants
const (
RoleStandardUser = "standard"
RoleMaintainer = "maintainer"
RoleAdmin = "admin"
)
2024-12-30 15:46:18 +01:00
type User struct {
2025-08-13 08:28:47 +02:00
UserID uint `gorm:"primaryKey" json:"id"`
Username string `gorm:"unique" json:"username"`
DisplayName string `gorm:"not null;default:''" json:"display_name"`
2025-08-13 08:28:47 +02:00
PasswordHash string `json:"password"`
Email string `gorm:"unique" json:"email"`
2025-12-30 08:00:04 +01:00
Role string `gorm:"default:standard" json:"role"`
2026-01-14 15:35:51 +01:00
PreferredLanguage string `gorm:"default:de" json:"preferred_language"`
2025-08-13 08:28:47 +02:00
ResetPwHash *string `gorm:"index" json:"-"` // Hash für Password-Reset (wird nicht serialisiert)
ResetPwHashExpires *time.Time `json:"-"` // Ablaufzeit für Password-Reset-Hash
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
2024-12-30 15:46:18 +01:00
}
2025-01-04 21:29:33 +01:00
func (object *User) Create() error {
2025-08-30 08:59:45 +02:00
if database.DB == nil {
return fmt.Errorf("database connection is nil")
}
if strings.TrimSpace(object.DisplayName) == "" {
object.DisplayName = object.Username
}
2025-01-04 21:29:33 +01:00
err := database.DB.Transaction(func(tx *gorm.DB) error {
// Save the User record
if err := tx.Create(&object).Error; err != nil {
return fmt.Errorf("failed to save User: %w", err)
}
return nil
})
return err
}
func (object *User) First(value string) error {
2025-08-30 08:59:45 +02:00
if database.DB == nil {
return fmt.Errorf("database connection is nil")
}
2025-01-04 21:29:33 +01:00
err := database.DB.First(&object, "username = ?", value).Error
if err != nil {
// User found
return err
}
return nil
}
func (object *User) FirstId(value uint) error {
2025-08-30 08:59:45 +02:00
if database.DB == nil {
return fmt.Errorf("database connection is nil")
}
2025-01-04 21:29:33 +01:00
err := database.DB.First(&object, "user_id = ?", value).Error
if err != nil {
// User found
return err
}
return nil
}
func (object *User) Save() error {
2025-08-30 08:59:45 +02:00
if database.DB == nil {
return fmt.Errorf("database connection is nil")
}
if strings.TrimSpace(object.DisplayName) == "" {
object.DisplayName = object.Username
}
2025-01-04 21:29:33 +01:00
err := database.DB.Save(&object).Error
if err != nil {
// User found
return err
}
return nil
}
2025-08-13 08:28:47 +02:00
// FindByEmail findet einen User anhand der E-Mail-Adresse
func (object *User) FindByEmail(email string) error {
2025-08-30 08:59:45 +02:00
if database.DB == nil {
return fmt.Errorf("database connection is nil")
}
2025-08-30 08:59:45 +02:00
if email == "" {
return fmt.Errorf("email cannot be empty")
}
2025-08-13 08:28:47 +02:00
err := database.DB.First(&object, "email = ?", email).Error
return err
}
// FindByResetHash findet einen User anhand des Reset-Hashes
func (object *User) FindByResetHash(resetHash string) error {
2025-08-30 08:59:45 +02:00
if database.DB == nil {
return fmt.Errorf("database connection is nil")
}
2025-08-13 08:28:47 +02:00
err := database.DB.First(&object, "reset_pw_hash = ? AND reset_pw_hash_expires > ?", resetHash, time.Now()).Error
return err
}
// SetPasswordResetHash setzt den Reset-Hash und die Ablaufzeit (14 Tage)
func (object *User) SetPasswordResetHash(resetHash string) error {
expiryTime := time.Now().Add(14 * 24 * time.Hour) // 14 Tage gültig
object.ResetPwHash = &resetHash
object.ResetPwHashExpires = &expiryTime
return object.Save()
}
// ClearPasswordResetHash entfernt den Reset-Hash
func (object *User) ClearPasswordResetHash() error {
object.ResetPwHash = nil
object.ResetPwHashExpires = nil
return object.Save()
}
// IsResetHashValid prüft ob der Reset-Hash gültig und nicht abgelaufen ist
func (object *User) IsResetHashValid(resetHash string) bool {
if object.ResetPwHash == nil || object.ResetPwHashExpires == nil {
return false
}
return *object.ResetPwHash == resetHash && time.Now().Before(*object.ResetPwHashExpires)
}
2025-12-30 08:00:04 +01:00
// HasRole checks if the user has the specified role
func (u *User) HasRole(role string) bool {
return u.Role == role
}
// IsAdmin checks if the user is an admin
func (u *User) IsAdmin() bool {
return u.Role == RoleAdmin
}
// IsMaintainer checks if the user is a maintainer or higher
func (u *User) IsMaintainer() bool {
return u.Role == RoleMaintainer || u.Role == RoleAdmin
}
// IsStandardUser checks if the user is a standard user or higher
func (u *User) IsStandardUser() bool {
return u.Role == RoleStandardUser || u.IsMaintainer()
}
// ValidateRole checks if the given role is valid
func ValidateRole(role string) bool {
return role == RoleStandardUser || role == RoleMaintainer || role == RoleAdmin
}
func (u *User) DisplayNameOrUsername() string {
if strings.TrimSpace(u.DisplayName) != "" {
return u.DisplayName
}
return u.Username
}