add permission model

This commit is contained in:
Yavolte 2025-06-17 17:17:02 +08:00
parent e85cc0b8ca
commit 2631943904
3 changed files with 33 additions and 12 deletions

View File

@ -28,6 +28,7 @@ import (
type Model struct { type Model struct {
naming types.Naming //命名规则 naming types.Naming //命名规则
value reflect.Value //模块值 value reflect.Value //模块值
modelValue any //模块实例
db *gorm.DB //数据库 db *gorm.DB //数据库
primaryKey string //主键 primaryKey string //主键
urlPrefix string //url前缀 urlPrefix string //url前缀
@ -265,6 +266,9 @@ func (m *Model) Fields() []*schema.Field {
// 权限示例: "module.model.scenario" // 权限示例: "module.model.scenario"
// 比如: organize:user:list, organize:user:create // 比如: organize:user:list, organize:user:create
func (m *Model) Permission(scenario string) string { func (m *Model) Permission(scenario string) string {
if pm, ok := m.modelValue.(PermissionModel); ok {
return pm.GetPermission(scenario)
}
ss := make([]string, 0, 4) ss := make([]string, 0, 4)
switch scenario { switch scenario {
case types.ScenarioList: case types.ScenarioList:
@ -1085,6 +1089,7 @@ func newModel(v any, db *gorm.DB, naming types.Naming) *Model {
value: reflect.Indirect(reflect.ValueOf(v)), value: reflect.Indirect(reflect.ValueOf(v)),
valueLookup: defaultValueLookup, valueLookup: defaultValueLookup,
} }
model.modelValue = reflect.New(model.value.Type()).Interface()
model.statement = &gorm.Statement{ model.statement = &gorm.Statement{
DB: model.getDB(), DB: model.getDB(),
ConnPool: model.getDB().ConnPool, ConnPool: model.getDB().ConnPool,

35
rest.go
View File

@ -674,38 +674,49 @@ func ModelTypes(ctx context.Context, db *gorm.DB, model any, domainName, labelCo
} }
// GetFieldValue 获取模型某个字段的值 // GetFieldValue 获取模型某个字段的值
func GetFieldValue(stmt *gorm.Statement, refValue reflect.Value, column string) interface{} { func GetFieldValue(stmt *gorm.Statement, refValue reflect.Value, column string) any {
var ( var (
idx = -1 rawField *schema.Field
) )
refVal := reflect.Indirect(refValue) refVal := reflect.Indirect(refValue)
for i, field := range stmt.Schema.Fields { for _, field := range stmt.Schema.Fields {
if field.DBName == column || field.Name == column { if field.DBName == column || field.Name == column {
idx = i rawField = field
break break
} }
} }
if idx > -1 { if rawField == nil {
return refVal.Field(idx).Interface()
}
return nil return nil
} }
var targetValue reflect.Value
targetValue = refVal
for _, i := range rawField.StructField.Index {
targetValue = targetValue.Field(i)
}
return targetValue.Interface()
}
// SetFieldValue 设置模型某个字段的值 // SetFieldValue 设置模型某个字段的值
func SetFieldValue(stmt *gorm.Statement, refValue reflect.Value, column string, value any) { func SetFieldValue(stmt *gorm.Statement, refValue reflect.Value, column string, value any) {
var ( var (
idx = -1 rawField *schema.Field
) )
refVal := reflect.Indirect(refValue) refVal := reflect.Indirect(refValue)
for i, field := range stmt.Schema.Fields { for _, field := range stmt.Schema.Fields {
if field.DBName == column || field.Name == column { if field.DBName == column || field.Name == column {
idx = i rawField = field
break break
} }
} }
if idx > -1 { if rawField == nil {
refVal.Field(idx).Set(reflect.ValueOf(value)) return
} }
var targetValue reflect.Value
targetValue = refVal
for _, i := range rawField.StructField.Index {
targetValue = targetValue.Field(i)
}
targetValue.Set(reflect.ValueOf(value))
} }
// SafeSetFileValue 安全设置模型某个字段的值 // SafeSetFileValue 安全设置模型某个字段的值

View File

@ -44,6 +44,11 @@ type (
HttpTableName(req *http.Request) string HttpTableName(req *http.Request) string
} }
// 权限模型定义
PermissionModel interface {
GetPermission(scenario string) string
}
//创建后的回调,这个回调不在事物内 //创建后的回调,这个回调不在事物内
afterCreated interface { afterCreated interface {
AfterCreated(ctx context.Context, tx *gorm.DB) AfterCreated(ctx context.Context, tx *gorm.DB)