mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-13 20:53:49 +08:00
1、泛型化参数,预留泛型约束,方便以后代码扩展的类型安全校验
2、升级goframe至v2.7.3
This commit is contained in:
@@ -19,7 +19,7 @@ import (
|
||||
|
||||
// FilterAuth 过滤数据权限
|
||||
// 通过上下文中的用户角色权限和表中是否含有需要过滤的字段附加查询条件
|
||||
func FilterAuth(m *gdb.Model) *gdb.Model {
|
||||
func FilterAuth[T any](m *gdb.Model) *gdb.Model {
|
||||
var (
|
||||
needAuth bool
|
||||
filterField string
|
||||
@@ -40,16 +40,16 @@ func FilterAuth(m *gdb.Model) *gdb.Model {
|
||||
if !needAuth {
|
||||
return m
|
||||
}
|
||||
return m.Handler(FilterAuthWithField(filterField))
|
||||
return m.Handler(FilterAuthWithField[T](filterField))
|
||||
}
|
||||
|
||||
// FilterAuthWithField 过滤数据权限,设置指定字段
|
||||
func FilterAuthWithField(filterField string) func(m *gdb.Model) *gdb.Model {
|
||||
func FilterAuthWithField[T any](filterField string) func(m *gdb.Model) *gdb.Model {
|
||||
return func(m *gdb.Model) *gdb.Model {
|
||||
var (
|
||||
role *entity.AdminRole
|
||||
ctx = m.GetCtx()
|
||||
co = contexts.Get(ctx)
|
||||
co = contexts.Get[T](ctx)
|
||||
)
|
||||
|
||||
if co == nil || co.User == nil {
|
||||
|
||||
@@ -30,13 +30,13 @@ func Model(m *gdb.Model, opt ...*Option) *gdb.Model {
|
||||
option = DefaultOption
|
||||
}
|
||||
if option.FilterAuth {
|
||||
m = m.Handler(FilterAuth)
|
||||
m = m.Handler(FilterAuth[any])
|
||||
}
|
||||
if option.ForceCache {
|
||||
m = m.Handler(ForceCache)
|
||||
}
|
||||
if option.FilterTenant {
|
||||
m = m.Handler(FilterTenant)
|
||||
m = m.Handler(FilterTenant[any])
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
|
||||
// FilterTenant 过滤多租户数据权限
|
||||
// 根据部门类型识别当前租户、商户、用户身份,过滤只属于自己的数据
|
||||
func FilterTenant(m *gdb.Model) *gdb.Model {
|
||||
func FilterTenant[T any](m *gdb.Model) *gdb.Model {
|
||||
var (
|
||||
needAuth bool
|
||||
filterField string
|
||||
@@ -23,19 +23,19 @@ func FilterTenant(m *gdb.Model) *gdb.Model {
|
||||
)
|
||||
|
||||
// 租户
|
||||
if contexts.IsTenantDept(ctx) && gstr.InArray(fields, "tenant_id") {
|
||||
if contexts.IsTenantDept[T](ctx) && gstr.InArray(fields, "tenant_id") {
|
||||
needAuth = true
|
||||
filterField = "tenant_id"
|
||||
}
|
||||
|
||||
// 商户
|
||||
if contexts.IsMerchantDept(ctx) && gstr.InArray(fields, "merchant_id") {
|
||||
if contexts.IsMerchantDept[T](ctx) && gstr.InArray(fields, "merchant_id") {
|
||||
needAuth = true
|
||||
filterField = "merchant_id"
|
||||
}
|
||||
|
||||
// 用户
|
||||
if contexts.IsUserDept(ctx) && gstr.InArray(fields, "user_id") {
|
||||
if contexts.IsUserDept[T](ctx) && gstr.InArray(fields, "user_id") {
|
||||
needAuth = true
|
||||
filterField = "user_id"
|
||||
}
|
||||
@@ -44,6 +44,6 @@ func FilterTenant(m *gdb.Model) *gdb.Model {
|
||||
return m
|
||||
}
|
||||
|
||||
m = m.Where(filterField, contexts.GetUserId(ctx))
|
||||
m = m.Where(filterField, contexts.GetUserId[T](ctx))
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ func (h *hookSaveTenant) checkRelationSingle(idx any, relation, limitType string
|
||||
return
|
||||
}
|
||||
|
||||
relationId := contexts.GetUserId(h.ctx)
|
||||
relationId := contexts.GetUserId[any](h.ctx)
|
||||
switch relation {
|
||||
case consts.DeptTypeTenant:
|
||||
if ok = tr.TenantId == relationId; !ok {
|
||||
@@ -210,8 +210,8 @@ func (h *hookSaveTenant) handle() (result sql.Result, err error) {
|
||||
var (
|
||||
update = make(g.Map)
|
||||
fields = h.getFields()
|
||||
memberId = contexts.GetUserId(h.ctx)
|
||||
deptType = contexts.GetDeptType(h.ctx)
|
||||
memberId = contexts.GetUserId[any](h.ctx)
|
||||
deptType = contexts.GetDeptType[any](h.ctx)
|
||||
tr *hgorm.TenantRelation
|
||||
)
|
||||
|
||||
@@ -221,7 +221,7 @@ func (h *hookSaveTenant) handle() (result sql.Result, err error) {
|
||||
}
|
||||
|
||||
// 非公司类型,加载自己的租户关系,用于重写关系
|
||||
if !contexts.IsCompanyDept(h.ctx) {
|
||||
if !contexts.IsCompanyDept[any](h.ctx) {
|
||||
tr, err = h.getRelation(memberId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user