Files
mce/internal/model/user.go

46 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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]
}