url_instruction.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package api
  2. import (
  3. "net/url"
  4. "reflect"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/logger"
  9. "git.sxidc.com/go-tools/utils/strutils"
  10. "git.sxidc.com/go-tools/utils/template"
  11. "github.com/pkg/errors"
  12. )
  13. type UrlInstruction interface {
  14. FormUrlInstruction() (string, error)
  15. }
  16. func FormUrlInstruction(instructions ...UrlInstruction) string {
  17. urlInstructions := make([]string, 0)
  18. for _, instruction := range instructions {
  19. urlInstruction, err := instruction.FormUrlInstruction()
  20. if err != nil {
  21. panic(err)
  22. }
  23. urlInstructions = append(urlInstructions, urlInstruction)
  24. }
  25. return "${" + strings.Join(urlInstructions, " ") + "}$"
  26. }
  27. const (
  28. permUrlInstructionTpl = `perm:"group:{{ .Group }};name:{{ .Name }};{{- if .Description -}}description:{{ .Description }};{{- end -}}{{- if .NeedCheckExpire -}}needCheckExpire;{{- end -}}{{- if .SensitiveWordScene -}}sensitiveWordScene:{{ .SensitiveWordScene }};{{- end -}}"`
  29. )
  30. type PermUrlInstruction struct {
  31. Group string
  32. Name string
  33. Description string
  34. NeedCheckExpire bool
  35. SensitiveWordScene int
  36. }
  37. func (cmd *PermUrlInstruction) check() error {
  38. if strutils.IsStringEmpty(cmd.Group) {
  39. return errors.New("没有传递权限组")
  40. }
  41. if strutils.IsStringEmpty(cmd.Name) {
  42. return errors.New("没有传递权限名称")
  43. }
  44. return nil
  45. }
  46. func (cmd *PermUrlInstruction) FormUrlInstruction() (string, error) {
  47. err := cmd.check()
  48. if err != nil {
  49. return "", err
  50. }
  51. permUrlInstruction, err := template.ParseTemplateStringToString(permUrlInstructionTpl, cmd)
  52. if err != nil {
  53. return "", err
  54. }
  55. return permUrlInstruction, nil
  56. }
  57. // Perm指令定义
  58. const (
  59. tagPartSeparator = ";"
  60. tagPartKeyValueSeparator = ":"
  61. )
  62. const (
  63. permTagKey = "perm"
  64. permTagPartGroup = "group"
  65. permTagPartName = "name"
  66. permTagPartDescription = "description"
  67. permTagPartNeedCheckExpire = "needCheckExpire"
  68. permTagPartSensitiveWordScene = "sensitiveWordScene"
  69. )
  70. func parseRelativePathPerm(basePath string, relativePathPattern string, method string) (string, *PermissionItem, error) {
  71. re := regexp.MustCompile(`\$\{(.+)\}\$`)
  72. matches := re.FindStringSubmatch(relativePathPattern)
  73. if len(matches) == 0 {
  74. return relativePathPattern, nil, nil
  75. }
  76. relativePath := re.ReplaceAllString(relativePathPattern, "")
  77. permTag, ok := reflect.StructTag(matches[1]).Lookup(permTagKey)
  78. if !ok {
  79. return relativePath, nil, nil
  80. }
  81. tagParts := strings.Split(permTag, tagPartSeparator)
  82. if tagParts == nil || len(tagParts) == 0 {
  83. return relativePath, nil, nil
  84. }
  85. fullPath, err := url.JoinPath(basePath, relativePath)
  86. if err != nil {
  87. return "", nil, err
  88. }
  89. permissionItem := &PermissionItem{
  90. Group: "",
  91. Name: "",
  92. Description: "",
  93. Resource: fullPath,
  94. Action: method,
  95. NeedCheckExpire: false,
  96. SensitiveWordScene: 0,
  97. }
  98. for _, tagPart := range tagParts {
  99. tagPartKeyValue := strings.SplitN(strings.TrimSpace(tagPart), tagPartKeyValueSeparator, 2)
  100. if strutils.IsStringEmpty(tagPartKeyValue[0]) {
  101. continue
  102. }
  103. switch tagPartKeyValue[0] {
  104. case permTagPartGroup:
  105. permissionItem.Group = tagPartKeyValue[1]
  106. case permTagPartName:
  107. permissionItem.Name = tagPartKeyValue[1]
  108. case permTagPartDescription:
  109. permissionItem.Description = tagPartKeyValue[1]
  110. case permTagPartNeedCheckExpire:
  111. permissionItem.NeedCheckExpire = true
  112. case permTagPartSensitiveWordScene:
  113. scene, err := strconv.Atoi(tagPartKeyValue[1])
  114. if err != nil {
  115. return relativePath, nil, err
  116. }
  117. if scene < 1 || scene > 4 {
  118. return relativePath, nil, errors.New("不支持的敏感词场景: " + tagPartKeyValue[1])
  119. }
  120. permissionItem.SensitiveWordScene = scene
  121. default:
  122. err := errors.New(permTagKey + "不支持的tag: " + tagPartKeyValue[0])
  123. logger.GetInstance().Error(err)
  124. continue
  125. }
  126. }
  127. err = permissionItem.check()
  128. if err != nil {
  129. return "", nil, err
  130. }
  131. return relativePath, permissionItem, nil
  132. }