policy.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2016 beego 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 beego
  15. import (
  16. "strings"
  17. "github.com/astaxie/beego/context"
  18. )
  19. // PolicyFunc defines a policy function which is invoked before the controller handler is executed.
  20. type PolicyFunc func(*context.Context)
  21. // FindRouter Find Router info for URL
  22. func (p *ControllerRegister) FindPolicy(cont *context.Context) []PolicyFunc {
  23. var urlPath = cont.Input.URL()
  24. if !BConfig.RouterCaseSensitive {
  25. urlPath = strings.ToLower(urlPath)
  26. }
  27. httpMethod := cont.Input.Method()
  28. isWildcard := false
  29. // Find policy for current method
  30. t, ok := p.policies[httpMethod]
  31. // If not found - find policy for whole controller
  32. if !ok {
  33. t, ok = p.policies["*"]
  34. isWildcard = true
  35. }
  36. if ok {
  37. runObjects := t.Match(urlPath, cont)
  38. if r, ok := runObjects.([]PolicyFunc); ok {
  39. return r
  40. } else if !isWildcard {
  41. // If no policies found and we checked not for "*" method - try to find it
  42. t, ok = p.policies["*"]
  43. if ok {
  44. runObjects = t.Match(urlPath, cont)
  45. if r, ok = runObjects.([]PolicyFunc); ok {
  46. return r
  47. }
  48. }
  49. }
  50. }
  51. return nil
  52. }
  53. func (p *ControllerRegister) addToPolicy(method, pattern string, r ...PolicyFunc) {
  54. method = strings.ToUpper(method)
  55. p.enablePolicy = true
  56. if !BConfig.RouterCaseSensitive {
  57. pattern = strings.ToLower(pattern)
  58. }
  59. if t, ok := p.policies[method]; ok {
  60. t.AddRouter(pattern, r)
  61. } else {
  62. t := NewTree()
  63. t.AddRouter(pattern, r)
  64. p.policies[method] = t
  65. }
  66. }
  67. // Register new policy in beego
  68. func Policy(pattern, method string, policy ...PolicyFunc) {
  69. BeeApp.Handlers.addToPolicy(method, pattern, policy...)
  70. }
  71. // Find policies and execute if were found
  72. func (p *ControllerRegister) execPolicy(cont *context.Context, urlPath string) (started bool) {
  73. if !p.enablePolicy {
  74. return false
  75. }
  76. // Find Policy for method
  77. policyList := p.FindPolicy(cont)
  78. if len(policyList) > 0 {
  79. // Run policies
  80. for _, runPolicy := range policyList {
  81. runPolicy(cont)
  82. if cont.ResponseWriter.Started {
  83. return true
  84. }
  85. }
  86. return false
  87. }
  88. return false
  89. }