- users/user_groups 模型与 CRUD 接口,组成员关系手动维护(规避 GORM 零值主键问题) - 迁移 v2~v4:用户组、用户表、初始 admin 用户 - 初始密码随机生成,仅终端打印一次并写入 data/admin_password.txt
32 lines
1.1 KiB
Go
32 lines
1.1 KiB
Go
package model
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// 内置用户组 ID:0 为 admin 组,1 为普通用户默认组。
|
|
const (
|
|
GroupIDAdmin = 0
|
|
GroupIDUser = 1
|
|
)
|
|
|
|
// UserGroup 用户组。ID 由服务端分配而非自增,以便跨数据库稳定保留 id 0 作为内置 admin 组。
|
|
type UserGroup struct {
|
|
ID uint `gorm:"primaryKey;autoIncrement:false" json:"id"`
|
|
Name string `gorm:"size:50;uniqueIndex;not null" json:"name"`
|
|
Description string `gorm:"size:255" json:"description"`
|
|
IsSystem bool `gorm:"not null;default:false" json:"is_system"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
}
|
|
|
|
// UserGroupMember 用户与用户组的关联表。组 id 0 是合法主键,GORM 关联读写会把 0 视为零值而丢弃,
|
|
// 因此关联的读写统一由 API 层手动维护。
|
|
type UserGroupMember struct {
|
|
UserID uint `gorm:"primaryKey" json:"user_id"`
|
|
UserGroupID uint `gorm:"primaryKey;index" json:"user_group_id"`
|
|
}
|