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.

store.go 3.7 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. "encoding/json"
  17. "fmt"
  18. "net"
  19. "strings"
  20. "github.com/casibase/casibase/object"
  21. )
  22. func (c *ApiController) GetGlobalStores() {
  23. stores, err := object.GetGlobalStores()
  24. if err != nil {
  25. c.ResponseError(err.Error())
  26. return
  27. }
  28. c.ResponseOk(stores)
  29. }
  30. func (c *ApiController) GetStores() {
  31. owner := c.Input().Get("owner")
  32. stores, err := object.GetStores(owner)
  33. if err != nil {
  34. c.ResponseError(err.Error())
  35. return
  36. }
  37. c.ResponseOk(stores)
  38. }
  39. func isIpAddress(host string) bool {
  40. // Attempt to split the host and port, ignoring the error
  41. hostWithoutPort, _, err := net.SplitHostPort(host)
  42. if err != nil {
  43. // If an error occurs, it might be because there's no port
  44. // In that case, use the original host string
  45. hostWithoutPort = host
  46. }
  47. // Attempt to parse the host as an IP address (both IPv4 and IPv6)
  48. ip := net.ParseIP(hostWithoutPort)
  49. // if host is not nil is an IP address else is not an IP address
  50. return ip != nil
  51. }
  52. func getOriginFromHost(host string) string {
  53. protocol := "https://"
  54. if !strings.Contains(host, ".") {
  55. // "localhost:14000"
  56. protocol = "http://"
  57. } else if isIpAddress(host) {
  58. // "192.168.0.10"
  59. protocol = "http://"
  60. }
  61. return fmt.Sprintf("%s%s", protocol, host)
  62. }
  63. func (c *ApiController) GetStore() {
  64. id := c.Input().Get("id")
  65. var store *object.Store
  66. var err error
  67. if id == "admin/_casibase_default_store_" {
  68. store, err = object.GetDefaultStore("admin")
  69. } else {
  70. store, err = object.GetStore(id)
  71. }
  72. if err != nil {
  73. c.ResponseError(err.Error())
  74. return
  75. }
  76. if store == nil {
  77. c.ResponseError("store is empty")
  78. return
  79. }
  80. host := c.Ctx.Request.Host
  81. origin := getOriginFromHost(host)
  82. err = store.Populate(origin)
  83. if err != nil {
  84. // gentle error
  85. c.ResponseOk(store, err.Error())
  86. return
  87. }
  88. c.ResponseOk(store)
  89. }
  90. func (c *ApiController) UpdateStore() {
  91. id := c.Input().Get("id")
  92. var store object.Store
  93. err := json.Unmarshal(c.Ctx.Input.RequestBody, &store)
  94. if err != nil {
  95. c.ResponseError(err.Error())
  96. return
  97. }
  98. success, err := object.UpdateStore(id, &store)
  99. if err != nil {
  100. c.ResponseError(err.Error())
  101. return
  102. }
  103. c.ResponseOk(success)
  104. }
  105. func (c *ApiController) AddStore() {
  106. var store object.Store
  107. err := json.Unmarshal(c.Ctx.Input.RequestBody, &store)
  108. if err != nil {
  109. c.ResponseError(err.Error())
  110. return
  111. }
  112. sucess, err := object.AddStore(&store)
  113. if err != nil {
  114. c.ResponseError(err.Error())
  115. return
  116. }
  117. c.ResponseOk(sucess)
  118. }
  119. func (c *ApiController) DeleteStore() {
  120. var store object.Store
  121. err := json.Unmarshal(c.Ctx.Input.RequestBody, &store)
  122. if err != nil {
  123. c.ResponseError(err.Error())
  124. return
  125. }
  126. sucess, err := object.DeleteStore(&store)
  127. if err != nil {
  128. c.ResponseError(err.Error())
  129. return
  130. }
  131. c.ResponseOk(sucess)
  132. }
  133. func (c *ApiController) RefreshStoreVectors() {
  134. var store object.Store
  135. err := json.Unmarshal(c.Ctx.Input.RequestBody, &store)
  136. if err != nil {
  137. c.ResponseError(err.Error())
  138. return
  139. }
  140. ok, err := object.RefreshStoreVectors(&store)
  141. if err != nil {
  142. c.ResponseError(err.Error())
  143. return
  144. }
  145. c.ResponseOk(ok)
  146. }