fieldperm.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package permlib
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. )
  8. type filterResponseWriter struct {
  9. http.ResponseWriter
  10. buf bytes.Buffer
  11. req *http.Request
  12. perms []string
  13. engine *Engine
  14. code int
  15. }
  16. func newFilterResponseWriter(w http.ResponseWriter, req *http.Request, perms []string, e *Engine) *filterResponseWriter {
  17. return &filterResponseWriter{
  18. ResponseWriter: w,
  19. req: req,
  20. perms: perms,
  21. engine: e,
  22. code: 200,
  23. }
  24. }
  25. func (fw *filterResponseWriter) WriteHeader(code int) {
  26. fw.code = code
  27. }
  28. func (fw *filterResponseWriter) Write(b []byte) (int, error) {
  29. return fw.buf.Write(b)
  30. }
  31. func (fw *filterResponseWriter) flush() {
  32. body := fw.buf.Bytes()
  33. if len(body) > 0 {
  34. body = fw.engine.filterResponseBody(body, fw.req, fw.perms)
  35. }
  36. fw.ResponseWriter.WriteHeader(fw.code)
  37. fw.ResponseWriter.Write(body)
  38. }
  39. func (e *Engine) filterResponseBody(body []byte, req *http.Request, perms []string) []byte {
  40. key := req.Method + " " + req.URL.Path
  41. fm, ok := e.fieldPerms[key]
  42. if !ok || fm.Response == nil {
  43. return body
  44. }
  45. return filterResponseByMap(body, fm.Response, perms)
  46. }
  47. func filterResponseByMap(body []byte, node *FieldNode, perms []string) []byte {
  48. if node == nil {
  49. return body
  50. }
  51. permSet := toPermSet(perms)
  52. body = bytes.TrimSpace(body)
  53. if len(body) == 0 {
  54. return body
  55. }
  56. if body[0] == '[' {
  57. return filterArray(body, node, permSet)
  58. }
  59. return filterObject(body, node, permSet)
  60. }
  61. func filterObject(raw json.RawMessage, node *FieldNode, permSet map[string]bool) json.RawMessage {
  62. if node == nil {
  63. return raw
  64. }
  65. var obj map[string]json.RawMessage
  66. if err := json.Unmarshal(raw, &obj); err != nil {
  67. return raw
  68. }
  69. for jsonName, permCode := range node.Fields {
  70. if !permSet[permCode] {
  71. delete(obj, jsonName)
  72. }
  73. }
  74. for jsonName, child := range node.Nested {
  75. v, ok := obj[jsonName]
  76. if !ok {
  77. continue
  78. }
  79. v = bytes.TrimSpace(v)
  80. if len(v) == 0 {
  81. continue
  82. }
  83. if v[0] == '[' {
  84. obj[jsonName] = filterArray(v, child, permSet)
  85. } else if v[0] == '{' {
  86. obj[jsonName] = filterObject(v, child, permSet)
  87. }
  88. }
  89. result, _ := json.Marshal(obj)
  90. return result
  91. }
  92. func filterArray(raw json.RawMessage, node *FieldNode, permSet map[string]bool) json.RawMessage {
  93. var arr []json.RawMessage
  94. if err := json.Unmarshal(raw, &arr); err != nil {
  95. return raw
  96. }
  97. for i, item := range arr {
  98. arr[i] = filterObject(item, node, permSet)
  99. }
  100. result, _ := json.Marshal(arr)
  101. return result
  102. }
  103. func toPermSet(perms []string) map[string]bool {
  104. m := make(map[string]bool, len(perms))
  105. for _, p := range perms {
  106. m[p] = true
  107. }
  108. return m
  109. }
  110. func readBody(req *http.Request) ([]byte, error) {
  111. if req.Body == nil {
  112. return nil, nil
  113. }
  114. body, err := io.ReadAll(req.Body)
  115. req.Body.Close()
  116. if err != nil {
  117. return nil, err
  118. }
  119. req.Body = io.NopCloser(bytes.NewReader(body))
  120. return body, nil
  121. }
  122. func restoreBody(req *http.Request, body []byte) {
  123. req.Body = io.NopCloser(bytes.NewReader(body))
  124. }