This commit is contained in:
孟帅
2024-04-22 23:08:40 +08:00
parent 82483bd7b9
commit e144b12580
445 changed files with 17457 additions and 6708 deletions

View File

@@ -13,6 +13,7 @@ import (
"hotgo/internal/consts"
"hotgo/internal/library/contexts"
"hotgo/internal/model/entity"
"hotgo/utility/convert"
"hotgo/utility/tree"
)
@@ -22,7 +23,7 @@ func FilterAuth(m *gdb.Model) *gdb.Model {
var (
needAuth bool
filterField string
fields = escapeFieldsToSlice(m.GetFieldsStr())
fields = convert.EscapeFieldsToSlice(m.GetFieldsStr())
)
// 优先级created_by > member_id
@@ -99,11 +100,6 @@ func FilterAuthWithField(filterField string) func(m *gdb.Model) *gdb.Model {
}
}
// escapeFieldsToSlice 将转义过的字段转换为字段集切片
func escapeFieldsToSlice(s string) []string {
return gstr.Explode(",", gstr.Replace(gstr.Replace(s, "`,`", ","), "`", ""))
}
// GetDeptAndSub 获取指定部门的所有下级,含本部门
func GetDeptAndSub(ctx context.Context, deptId int64) (ids []int64) {
array, err := g.Model("admin_dept").

View File

@@ -3,7 +3,6 @@
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package handler
// handler.
@@ -13,8 +12,9 @@ import (
// Option 预处理选项
type Option struct {
FilterAuth bool // 过滤权限
ForceCache bool // 强制缓存
FilterAuth bool // 过滤权限
ForceCache bool // 强制缓存
FilterTenant bool // 过滤多租户权限
}
// DefaultOption 默认预处理选项
@@ -35,5 +35,8 @@ func Model(m *gdb.Model, opt ...*Option) *gdb.Model {
if option.ForceCache {
m = m.Handler(ForceCache)
}
if option.FilterTenant {
m = m.Handler(FilterTenant)
}
return m
}

View File

@@ -0,0 +1,49 @@
// Package handler
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package handler
import (
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/text/gstr"
"hotgo/internal/library/contexts"
"hotgo/utility/convert"
)
// FilterTenant 过滤多租户数据权限
// 根据部门类型识别当前租户、商户、用户身份,过滤只属于自己的数据
func FilterTenant(m *gdb.Model) *gdb.Model {
var (
needAuth bool
filterField string
fields = convert.EscapeFieldsToSlice(m.GetFieldsStr())
ctx = m.GetCtx()
)
// 租户
if contexts.IsTenantDept(ctx) && gstr.InArray(fields, "tenant_id") {
needAuth = true
filterField = "tenant_id"
}
// 商户
if contexts.IsMerchantDept(ctx) && gstr.InArray(fields, "merchant_id") {
needAuth = true
filterField = "merchant_id"
}
// 用户
if contexts.IsUserDept(ctx) && gstr.InArray(fields, "user_id") {
needAuth = true
filterField = "user_id"
}
if !needAuth {
return m
}
m = m.Where(filterField, contexts.GetUserId(ctx))
return m
}