You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

util_record.go 2.1 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2023 The casbin Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package controllers
  15. import (
  16. "fmt"
  17. "strings"
  18. "github.com/astaxie/beego/context"
  19. "github.com/casbin/casibase/util"
  20. "github.com/casdoor/casdoor-go-sdk/casdoorsdk"
  21. )
  22. func NewRecord(ctx *context.Context) *casdoorsdk.Record {
  23. ip := strings.Replace(util.GetIPFromRequest(ctx.Request), ": ", "", -1)
  24. action := strings.Replace(ctx.Request.URL.Path, "/api/", "", -1)
  25. requestUri := util.FilterQuery(ctx.Request.RequestURI, []string{"accessToken"})
  26. if len(requestUri) > 1000 {
  27. requestUri = requestUri[0:1000]
  28. }
  29. record := casdoorsdk.Record{
  30. Name: util.GenerateId(),
  31. CreatedTime: util.GetCurrentTime(),
  32. ClientIp: ip,
  33. User: "",
  34. Method: ctx.Request.Method,
  35. RequestUri: requestUri,
  36. Action: action,
  37. IsTriggered: false,
  38. }
  39. return &record
  40. }
  41. func addRecord(c *ApiController, userName string, requestUri string) {
  42. record := NewRecord(c.Ctx)
  43. record.User = userName
  44. if requestUri != "" {
  45. record.RequestUri = requestUri
  46. }
  47. util.SafeGoroutine(func() {
  48. _, err := casdoorsdk.AddRecord(record)
  49. if err != nil {
  50. panic(err)
  51. }
  52. })
  53. }
  54. func addRecordForFile(c *ApiController, userName string, action string, storeId string, key string, filename string, isLeaf bool) {
  55. typ := "Folder"
  56. if isLeaf {
  57. typ = "File"
  58. }
  59. _, storeName := util.GetOwnerAndNameFromId(storeId)
  60. path := fmt.Sprintf("/%s/%s", key, filename)
  61. if filename == "" {
  62. path = key
  63. }
  64. text := fmt.Sprintf("%s%s, Store: %s, Path: %s", action, typ, storeName, path)
  65. addRecord(c, userName, text)
  66. }