147 lines
5.1 KiB
Go
147 lines
5.1 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// User 用户模型
|
|
type User struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Username string `gorm:"size:64;uniqueIndex;not null" json:"username"`
|
|
Password string `gorm:"size:255;not null" json:"-"`
|
|
Email string `gorm:"size:128" json:"email"`
|
|
Phone string `gorm:"size:32" json:"phone"`
|
|
RealName string `gorm:"size:64" json:"real_name"`
|
|
Avatar string `gorm:"size:255" json:"avatar"`
|
|
Status int `gorm:"default:1" json:"status"` // 0:禁用 1:启用
|
|
IsAdmin bool `gorm:"default:false" json:"is_admin"`
|
|
AuthSource string `gorm:"size:20;default:'local'" json:"auth_source"` // local / ldap
|
|
LastLogin *time.Time `json:"last_login"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
|
|
// 关联
|
|
Roles []Role `gorm:"many2many:user_roles;" json:"roles,omitempty"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (User) TableName() string {
|
|
return "users"
|
|
}
|
|
|
|
// Role 角色模型
|
|
type Role struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Name string `gorm:"size:64;uniqueIndex;not null" json:"name"`
|
|
Code string `gorm:"size:64;uniqueIndex;not null" json:"code"`
|
|
Description string `gorm:"size:255" json:"description"`
|
|
Status int `gorm:"default:1" json:"status"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
|
|
// 关联
|
|
Permissions []Permission `gorm:"many2many:role_permissions;" json:"permissions,omitempty"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (Role) TableName() string {
|
|
return "roles"
|
|
}
|
|
|
|
// Permission 权限模型
|
|
type Permission struct {
|
|
ID uint `gorm:"primarykey" json:"id"`
|
|
Name string `gorm:"size:64;not null" json:"name"`
|
|
Code string `gorm:"size:128;uniqueIndex;not null" json:"code"`
|
|
Type string `gorm:"size:32" json:"type"` // menu, button, api
|
|
Path string `gorm:"size:255" json:"path"`
|
|
Method string `gorm:"size:32" json:"method"`
|
|
ParentID *uint `json:"parent_id"`
|
|
Sort int `gorm:"default:0" json:"sort"`
|
|
Status int `gorm:"default:1" json:"status"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
}
|
|
|
|
// TableName 表名
|
|
func (Permission) TableName() string {
|
|
return "permissions"
|
|
}
|
|
|
|
// LoginRequest 登录请求
|
|
type LoginRequest struct {
|
|
Username string `json:"username" binding:"required"`
|
|
Password string `json:"password" binding:"required"`
|
|
}
|
|
|
|
// LoginResponse 登录响应
|
|
type LoginResponse struct {
|
|
Token string `json:"token"`
|
|
ExpiresIn int `json:"expires_in"`
|
|
User *User `json:"user"`
|
|
}
|
|
|
|
// CreateUserRequest 创建用户请求
|
|
type CreateUserRequest struct {
|
|
Username string `json:"username" binding:"required,min=3,max=64"`
|
|
Password string `json:"password" binding:"required,min=6,max=32"`
|
|
Email string `json:"email" binding:"required,email"`
|
|
Phone string `json:"phone"`
|
|
RealName string `json:"real_name"`
|
|
RoleIDs []uint `json:"role_ids"`
|
|
}
|
|
|
|
// UpdateUserRequest 更新用户请求
|
|
type UpdateUserRequest struct {
|
|
Email string `json:"email" binding:"omitempty,email"`
|
|
Phone string `json:"phone"`
|
|
RealName string `json:"real_name"`
|
|
Status *int `json:"status"`
|
|
RoleIDs []uint `json:"role_ids"`
|
|
}
|
|
|
|
// CreateRoleRequest 创建角色请求 // Logan.Li
|
|
type CreateRoleRequest struct {
|
|
Name string `json:"name" binding:"required,min=1,max=64"`
|
|
Code string `json:"code" binding:"required,min=1,max=64"`
|
|
Description string `json:"description"`
|
|
PermissionIDs []uint `json:"permission_ids"`
|
|
}
|
|
|
|
// UpdateRoleRequest 更新角色请求 // Logan.Li
|
|
type UpdateRoleRequest struct {
|
|
Name string `json:"name" binding:"omitempty,min=1,max=64"`
|
|
Code string `json:"code" binding:"omitempty,min=1,max=64"`
|
|
Description *string `json:"description"`
|
|
Status *int `json:"status"`
|
|
PermissionIDs []uint `json:"permission_ids"`
|
|
}
|
|
|
|
// ChangePasswordRequest 修改密码请求
|
|
type ChangePasswordRequest struct {
|
|
OldPassword string `json:"old_password" binding:"required"`
|
|
NewPassword string `json:"new_password" binding:"required,min=6,max=32"`
|
|
}
|
|
|
|
// LDAPConfig LDAP配置
|
|
type LDAPConfig struct {
|
|
ID int `gorm:"primarykey" json:"id"`
|
|
LDAPHost string `gorm:"size:200;not null" json:"ldap_host"`
|
|
LDAPPort *int `json:"ldap_port"`
|
|
UseLDAPS *bool `json:"use_ldaps"`
|
|
AdminDN string `gorm:"size:500;not null" json:"admin_dn"`
|
|
AdminPassword string `gorm:"size:500;not null" json:"admin_password"`
|
|
SearchFilter *string `gorm:"size:500" json:"search_filter"`
|
|
BaseDN string `gorm:"size:500;not null" json:"base_dn"`
|
|
UserMapping *string `gorm:"type:text" json:"user_mapping"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (LDAPConfig) TableName() string { return "ldap_configs" }
|