初始化项目:基础设施 + 用户认证 + 后台管理系统 + AGPL 3.0 许可

This commit is contained in:
2026-05-26 13:46:33 +08:00
parent 1315df6501
commit 483fdd919f
56 changed files with 5804 additions and 40 deletions

45
internal/model/user.go Normal file
View File

@ -0,0 +1,45 @@
package model
// User 用户模型
type User struct {
BaseModel
Email string `gorm:"type:varchar(255);not null" json:"email"`
PasswordHash string `gorm:"type:varchar(255);not null" json:"-"`
Username string `gorm:"type:varchar(16);uniqueIndex;not null" json:"username"`
Avatar string `gorm:"type:varchar(500);default:''" json:"avatar"`
Bio string `gorm:"type:text" json:"bio"`
Role string `gorm:"type:varchar(20);default:user;index;not null" json:"role"`
Status string `gorm:"type:varchar(20);default:active;index;not null" json:"status"`
TokenVersion int `gorm:"default:0;not null" json:"-"` // 令牌版本,+1 即时吊销所有 JWT
}
// 角色常量
const (
RoleUser = "user"
RoleModerator = "moderator"
RoleAdmin = "admin"
RoleOwner = "owner"
)
// 状态常量
const (
StatusActive = "active"
StatusBanned = "banned"
StatusDeleted = "deleted" // 用户主动注销7 天内登录可恢复)
StatusLocked = "locked" // 永久锁定(管理员删除或注销满 7 天)
)
// roleLevel 角色层级映射(数字越大权限越高)
var roleLevel = map[string]int{
RoleUser: 0,
RoleModerator: 1,
RoleAdmin: 2,
RoleOwner: 3,
}
// HasMinRole 检查 role 是否达到 minRole 的权限级别
func HasMinRole(role, minRole string) bool {
return roleLevel[role] >= roleLevel[minRole]
}