[feat] 临时处理 GetRolesCondition
This commit is contained in:
		
							parent
							
								
									4a1f373efb
								
							
						
					
					
						commit
						385ab967b5
					
				
							
								
								
									
										117
									
								
								base/resource.go
									
									
									
									
									
								
							
							
						
						
									
										117
									
								
								base/resource.go
									
									
									
									
									
								
							@ -10,7 +10,6 @@ import (
 | 
				
			|||||||
	"github.com/samber/lo"
 | 
						"github.com/samber/lo"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.fsdpf.net/go/contracts"
 | 
						"git.fsdpf.net/go/contracts"
 | 
				
			||||||
	"git.fsdpf.net/go/contracts/helper"
 | 
					 | 
				
			||||||
	"git.fsdpf.net/go/contracts/res_type"
 | 
						"git.fsdpf.net/go/contracts/res_type"
 | 
				
			||||||
	"git.fsdpf.net/go/db"
 | 
						"git.fsdpf.net/go/db"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@ -207,77 +206,77 @@ func (this Resource) WithRolesCondition(b *db.Builder, roles ...string) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// 获取鉴权条件
 | 
					// 获取鉴权条件
 | 
				
			||||||
func (this Resource) GetRolesCondition(u contracts.User) *db.Builder {
 | 
					func (this Resource) GetRolesCondition(u contracts.User) *db.Builder {
 | 
				
			||||||
	isFullRight := false
 | 
						// isFullRight := false
 | 
				
			||||||
	isFullNot := false
 | 
						// isFullNot := false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	NewOrmConditionByRes := do.MustInvoke[helper.NewOrmConditionByRes](this.container)
 | 
						// NewOrmJoin := do.MustInvoke[helper.NewOrmJoin](this.container)
 | 
				
			||||||
	NewOrmJoin := do.MustInvoke[helper.NewOrmJoin](this.container)
 | 
						// GetResRelations := do.MustInvoke[GetResRelations](this.container)
 | 
				
			||||||
	GetResRelations := do.MustInvoke[GetResRelations](this.container)
 | 
						// GetResource := do.MustInvoke[contracts.GetResource](this.container)
 | 
				
			||||||
	GetResource := do.MustInvoke[contracts.GetResource](this.container)
 | 
						// GetResConditions := do.MustInvoke[GetResConditions](this.container)
 | 
				
			||||||
	GetResConditions := do.MustInvoke[GetResConditions](this.container)
 | 
						// GetOrmConditionByRes := do.MustInvoke[GetOrmConditionByRes](this.container)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	roles := do.MustInvoke[GetResRoles](this.container)(this.GetUuid())
 | 
						// roles := do.MustInvoke[GetResRoles](this.container)(this.GetUuid())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	subTables := lo.Reduce(roles, func(carry string, item ResRole, _ int) string {
 | 
						// subTables := lo.Reduce(roles, func(carry string, item ResRole, _ int) string {
 | 
				
			||||||
		db := this.GetDB().Table(string(this.GetTable()), this.GetCode()).Select(db.Raw("`" + this.GetCode() + "`.*"))
 | 
						// 	db := this.GetDB().Table(string(this.GetTable()), this.GetCode()).Select(db.Raw("`" + this.GetCode() + "`.*"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		joins := lo.Filter(GetResRelations(item.Uuid), func(item ResRelation, _ int) bool {
 | 
						// 	joins := lo.Filter(GetResRelations(item.Uuid), func(item ResRelation, _ int) bool {
 | 
				
			||||||
			return item.Type == "inner" || item.Type == "left" || item.Type == "right"
 | 
						// 		return item.Type == "inner" || item.Type == "left" || item.Type == "right"
 | 
				
			||||||
		})
 | 
						// 	})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		conditions := NewOrmConditionByRes(GetResConditions(item.Uuid))
 | 
						// 	conditions := GetOrmConditionByRes(GetResConditions(item.Uuid))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		for i := 0; i < len(joins); i++ {
 | 
						// 	for i := 0; i < len(joins); i++ {
 | 
				
			||||||
			oResource, ok := GetResource(joins[i].ResourceCode)
 | 
						// 		oResource, ok := GetResource(joins[i].ResourceCode)
 | 
				
			||||||
			if !ok {
 | 
						// 		if !ok {
 | 
				
			||||||
				continue
 | 
						// 			continue
 | 
				
			||||||
			}
 | 
						// 		}
 | 
				
			||||||
			join := NewOrmJoin(contracts.RelationType(joins[i].Type), oResource, joins[i].Code, joins[i].RelationResource, joins[i].RelationField, joins[i].RelationForeignKey)
 | 
						// 		join := NewOrmJoin(contracts.RelationType(joins[i].Type), oResource, joins[i].Code, joins[i].RelationResource, joins[i].RelationField, joins[i].RelationForeignKey)
 | 
				
			||||||
			// 关联扩展条件
 | 
						// 		// 关联扩展条件
 | 
				
			||||||
			join.SetCondition(NewOrmConditionByRes(GetResConditions(joins[i].Uuid)))
 | 
						// 		join.SetCondition(GetOrmConditionByRes(GetResConditions(joins[i].Uuid)))
 | 
				
			||||||
			join.Inject(db, nil)
 | 
						// 		join.Inject(db, nil)
 | 
				
			||||||
		}
 | 
						// 	}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if len(joins) == 0 && conditions.IsEmpty() {
 | 
						// 	if len(joins) == 0 && conditions.IsEmpty() {
 | 
				
			||||||
			// 无权限, 直接跳过这个 unoin 语句
 | 
						// 		// 无权限, 直接跳过这个 unoin 语句
 | 
				
			||||||
			if carry != "" {
 | 
						// 		if carry != "" {
 | 
				
			||||||
				return carry
 | 
						// 			return carry
 | 
				
			||||||
			}
 | 
						// 		}
 | 
				
			||||||
			// 第一个无权限除外, 避免所有用户所属角色都是无权限
 | 
						// 		// 第一个无权限除外, 避免所有用户所属角色都是无权限
 | 
				
			||||||
			db.WhereRaw("false")
 | 
						// 		db.WhereRaw("false")
 | 
				
			||||||
			isFullNot = true
 | 
						// 		isFullNot = true
 | 
				
			||||||
		} else if len(joins) == 0 && conditions.IsNotEmpty() && conditions.IsRight() /* 1=1 的这种条件*/ {
 | 
						// 	} else if len(joins) == 0 && conditions.IsNotEmpty() && conditions.IsRight() /* 1=1 的这种条件*/ {
 | 
				
			||||||
			// 只要有1个满权限, 直接返回单条语句
 | 
						// 		// 只要有1个满权限, 直接返回单条语句
 | 
				
			||||||
			isFullRight = true
 | 
						// 		isFullRight = true
 | 
				
			||||||
			return db.ToSql()
 | 
						// 		return db.ToSql()
 | 
				
			||||||
		} else if conditions.IsNotEmpty() {
 | 
						// 	} else if conditions.IsNotEmpty() {
 | 
				
			||||||
			db.WhereRaw(string(conditions.ToSql(nil)))
 | 
						// 		db.WhereRaw(string(conditions.ToSql(nil)))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// 如果前面是无权限的sql查看, 这直接返回本次查询
 | 
						// 		// 如果前面是无权限的sql查看, 这直接返回本次查询
 | 
				
			||||||
			if isFullNot {
 | 
						// 		if isFullNot {
 | 
				
			||||||
				isFullNot = false
 | 
						// 			isFullNot = false
 | 
				
			||||||
				return db.ToSql()
 | 
						// 			return db.ToSql()
 | 
				
			||||||
			}
 | 
						// 		}
 | 
				
			||||||
		}
 | 
						// 	}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if carry != "" {
 | 
						// 	if carry != "" {
 | 
				
			||||||
			carry += " union "
 | 
						// 		carry += " union "
 | 
				
			||||||
		}
 | 
						// 	}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		carry += db.ToSql()
 | 
						// 	carry += db.ToSql()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return carry
 | 
						// 	return carry
 | 
				
			||||||
	}, "")
 | 
						// }, "")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// @todo this.GetCode 要换成 alias
 | 
						// // @todo this.GetCode 要换成 alias
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if isFullRight {
 | 
						// if isFullRight {
 | 
				
			||||||
		this.GetDB().Table(string(this.GetTable()), this.GetCode())
 | 
						// 	this.GetDB().Table(string(this.GetTable()), this.GetCode())
 | 
				
			||||||
	} else if isFullNot {
 | 
						// } else if isFullNot {
 | 
				
			||||||
		this.GetDB().Table(string(this.GetTable()), this.GetCode()).WhereRaw("false")
 | 
						// 	this.GetDB().Table(string(this.GetTable()), this.GetCode()).WhereRaw("false")
 | 
				
			||||||
	} else if subTables != "" {
 | 
						// } else if subTables != "" {
 | 
				
			||||||
		this.GetDB().Table(subTables, this.GetCode())
 | 
						// 	this.GetDB().Table(subTables, this.GetCode())
 | 
				
			||||||
	}
 | 
						// }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return this.GetDB().Table(string(this.GetTable()), this.GetCode())
 | 
						return this.GetDB().Table(string(this.GetTable()), this.GetCode())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user