Skip to content

Commit

Permalink
[add] using a process as an API Guard
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Mar 30, 2022
1 parent 44c041d commit 602f8e5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 31 deletions.
13 changes: 1 addition & 12 deletions table/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func ProcessSearch(process *gou.Process) interface{} {
table := Select(name)

api := table.APIs["search"].ValidateLoop("xiang.table.search")
table.APIGuard(api.Guard, process.Sid, process.Global)

// if process.NumOfArgsIs(5) && api.IsAllow(process.Args[4]) {
// return nil
Expand Down Expand Up @@ -69,7 +68,6 @@ func ProcessFind(process *gou.Process) interface{} {
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["find"].ValidateLoop("xiang.table.find")
table.APIGuard(api.Guard, process.Sid, process.Global)

// Before Hook
process.Args = table.Before(table.Hooks.BeforeFind, process.Args, process.Sid)
Expand Down Expand Up @@ -98,7 +96,6 @@ func ProcessSave(process *gou.Process) interface{} {
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["save"].ValidateLoop("xiang.table.save")
table.APIGuard(api.Guard, process.Sid, process.Global)

// Before Hook
process.Args = table.Before(table.Hooks.BeforeSave, process.Args, process.Sid)
Expand All @@ -123,7 +120,6 @@ func ProcessDelete(process *gou.Process) interface{} {
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["delete"].ValidateLoop("xiang.table.delete")
table.APIGuard(api.Guard, process.Sid, process.Global)

id := process.Args[1]
return gou.NewProcess(api.Process, id).
Expand All @@ -140,7 +136,6 @@ func ProcessDeleteWhere(process *gou.Process) interface{} {
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["delete-where"].ValidateLoop("xiang.table.DeleteWhere")
table.APIGuard(api.Guard, process.Sid, process.Global)

// 批量删除
param := api.MergeDefaultQueryParam(process.ArgsQueryParams(1), 0, process.Sid)
Expand All @@ -162,7 +157,6 @@ func ProcessDeleteIn(process *gou.Process) interface{} {
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["delete-in"].ValidateLoop("xiang.table.DeleteIn")
table.APIGuard(api.Guard, process.Sid, process.Global)

// 批量删除
ids := strings.Split(process.ArgsString(1), ",")
Expand All @@ -187,7 +181,6 @@ func ProcessUpdateWhere(process *gou.Process) interface{} {
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["update-where"].ValidateLoop("xiang.table.UpdateWhere")
table.APIGuard(api.Guard, process.Sid, process.Global)

// 批量更新
param := api.MergeDefaultQueryParam(process.ArgsQueryParams(1), 0, process.Sid)
Expand All @@ -208,7 +201,6 @@ func ProcessUpdateIn(process *gou.Process) interface{} {
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["update-in"].ValidateLoop("xiang.table.UpdateIn")
table.APIGuard(api.Guard, process.Sid, process.Global)

// 批量删除
ids := strings.Split(process.ArgsString(1), ",")
Expand All @@ -231,7 +223,7 @@ func ProcessInsert(process *gou.Process) interface{} {
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["insert"].ValidateLoop("xiang.table.Insert")
table.APIGuard(api.Guard, process.Sid, process.Global)

return gou.NewProcess(api.Process, process.Args[1:]...).
WithGlobal(process.Global).
WithSID(process.Sid).Run()
Expand All @@ -245,7 +237,6 @@ func ProcessSetting(process *gou.Process) interface{} {
field := process.ArgsString(1)
table := Select(name)
api := table.APIs["setting"]
table.APIGuard(api.Guard, process.Sid, process.Global)

fields := strings.Split(field, ",")
if api.ProcessIs("xiang.table.Setting") {
Expand Down Expand Up @@ -292,7 +283,6 @@ func ProcessQuickSave(process *gou.Process) interface{} {
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["quicksave"].ValidateLoop("xiang.table.quicksave")
table.APIGuard(api.Guard, process.Sid, process.Global)

args := []interface{}{}
payload := process.ArgsMap(1)
Expand Down Expand Up @@ -324,7 +314,6 @@ func ProcessSelect(process *gou.Process) interface{} {
name := process.ArgsString(0)
table := Select(name)
api := table.APIs["select"].ValidateLoop("xiang.table.select")
table.APIGuard(api.Guard, process.Sid, process.Global)

// Before Hook
process.Args = table.Before(table.Hooks.BeforeSelect, process.Args, process.Sid)
Expand Down
22 changes: 3 additions & 19 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ func (table *Table) loadAPIs() {
api.Process = table.APIs[name].Process
}

// if table.APIs[name].Guard != "" {
// api.Guard = table.APIs[name].Guard
// }
if table.APIs[name].Guard != "" {
api.Guard = table.APIs[name].Guard
}

if table.APIs[name].Default != nil {
// fmt.Printf("\n%s.APIs[%s].Default: entry\n", table.Table, name)
Expand Down Expand Up @@ -209,22 +209,6 @@ func (table *Table) After(process string, data interface{}, args []interface{},
return response
}

// APIGuard API鉴权
func (table *Table) APIGuard(guard string, sid string, global map[string]interface{}, args ...interface{}) {
if guard == "" {
guard = table.Guard
}
if guard == "-" || guard == "" {
return
}

log.With(log.F{"args": args}).Debug(guard)
gou.NewProcess(guard, args...).
WithSID(sid).
WithGlobal(global).
Run()
}

// loadFilters 加载查询过滤器
func (table *Table) loadFilters() {
if table.Bind.Model == "" {
Expand Down

0 comments on commit 602f8e5

Please sign in to comment.