Files
gochat/backend/internal/model/user.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

70 lines
3.0 KiB
Go

package model
import (
"crypto/rand"
"encoding/hex"
"time"
"gorm.io/datatypes"
"gorm.io/gorm"
)
// User represents an agent/admin user in the system.
type User struct {
Base
AccountID uint `gorm:"index;not null" json:"account_id"`
Name string `gorm:"size:255;not null" json:"name"`
Email string `gorm:"size:255;uniqueIndex;not null" json:"email"`
Password string `gorm:"size:255;not null" json:"-"` // hashed password (bcrypt)
PasswordDigest string `gorm:"size:255" json:"-"` // alias used by auth service
Provider string `gorm:"size:50;default:email" json:"provider"` // email, google, saml
UID string `gorm:"size:255" json:"uid,omitempty"` // external ID for OAuth providers
AvatarURL string `gorm:"size:512" json:"avatar_url"`
DisplayName string `gorm:"size:255" json:"display_name"`
MessageSignature string `gorm:"type:text" json:"message_signature"`
PubsubToken string `gorm:"size:255;uniqueIndex" json:"pubsub_token"`
UISettings datatypes.JSON `gorm:"type:jsonb;default:'{}'" json:"ui_settings"`
CustomAttributes datatypes.JSON `gorm:"type:jsonb;default:'{}'" json:"custom_attributes,omitempty"`
Role string `gorm:"size:50;default:agent" json:"role"` // agent, admin
Type string `gorm:"size:50;default:user" json:"type"`
Active bool `gorm:"default:true" json:"active"`
Available bool `gorm:"default:false" json:"available"`
TOTPSecret string `gorm:"size:255" json:"totp_secret,omitempty"`
TOTPEnabled bool `gorm:"default:false" json:"totp_enabled"`
CustomRoleID *uint `gorm:"index" json:"custom_role_id,omitempty"`
SignInCount int `gorm:"default:0" json:"sign_in_count"`
ResetPasswordToken string `gorm:"size:255;index" json:"-"`
ResetPasswordSentAt *time.Time `json:"-"`
ConfirmationToken string `gorm:"size:255;index" json:"-"`
ConfirmationSentAt *time.Time `json:"-"`
UnconfirmedEmail string `gorm:"size:255" json:"unconfirmed_email,omitempty"`
ConfirmedAt *time.Time `json:"confirmed_at,omitempty"`
LastSignInAt *time.Time `json:"last_sign_in_at,omitempty"`
CurrentSignInAt *time.Time `json:"current_sign_in_at,omitempty"`
}
func (User) TableName() string { return "users" }
// BeforeCreate mirrors Chatwoot's Pubsubable concern by assigning every user a cable token.
func (u *User) BeforeCreate(tx *gorm.DB) error {
if u.PubsubToken == "" {
token, err := generateSecureToken(32)
if err != nil {
return err
}
u.PubsubToken = token
}
if u.Type == "" {
u.Type = "User"
}
return nil
}
func generateSecureToken(size int) (string, error) {
b := make([]byte, size)
if _, err := rand.Read(b); err != nil {
return "", err
}
return hex.EncodeToString(b), nil
}