option.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package controller
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "one-api/common"
  6. "one-api/model"
  7. "one-api/setting"
  8. "strings"
  9. "github.com/gin-gonic/gin"
  10. )
  11. func GetOptions(c *gin.Context) {
  12. var options []*model.Option
  13. common.OptionMapRWMutex.Lock()
  14. for k, v := range common.OptionMap {
  15. if strings.HasSuffix(k, "Token") || strings.HasSuffix(k, "Secret") || strings.HasSuffix(k, "Key") {
  16. continue
  17. }
  18. options = append(options, &model.Option{
  19. Key: k,
  20. Value: common.Interface2String(v),
  21. })
  22. }
  23. common.OptionMapRWMutex.Unlock()
  24. c.JSON(http.StatusOK, gin.H{
  25. "success": true,
  26. "message": "",
  27. "data": options,
  28. })
  29. return
  30. }
  31. func UpdateOption(c *gin.Context) {
  32. var option model.Option
  33. err := json.NewDecoder(c.Request.Body).Decode(&option)
  34. if err != nil {
  35. c.JSON(http.StatusBadRequest, gin.H{
  36. "success": false,
  37. "message": "无效的参数",
  38. })
  39. return
  40. }
  41. switch option.Key {
  42. case "GitHubOAuthEnabled":
  43. if option.Value == "true" && common.GitHubClientId == "" {
  44. c.JSON(http.StatusOK, gin.H{
  45. "success": false,
  46. "message": "无法启用 GitHub OAuth,请先填入 GitHub Client Id 以及 GitHub Client Secret!",
  47. })
  48. return
  49. }
  50. case "OIDCEnabled":
  51. if option.Value == "true" && common.OIDCClientId == "" {
  52. c.JSON(http.StatusOK, gin.H{
  53. "success": false,
  54. "message": "无法启用 OIDC 登录,请先填入 OIDC Client Id 以及 OIDC Client Secret!",
  55. })
  56. }
  57. case "LinuxDOOAuthEnabled":
  58. if option.Value == "true" && common.LinuxDOClientId == "" {
  59. c.JSON(http.StatusOK, gin.H{
  60. "success": false,
  61. "message": "无法启用 LinuxDO OAuth,请先填入 LinuxDO Client Id 以及 LinuxDO Client Secret!",
  62. })
  63. return
  64. }
  65. case "EmailDomainRestrictionEnabled":
  66. if option.Value == "true" && len(common.EmailDomainWhitelist) == 0 {
  67. c.JSON(http.StatusOK, gin.H{
  68. "success": false,
  69. "message": "无法启用邮箱域名限制,请先填入限制的邮箱域名!",
  70. })
  71. return
  72. }
  73. case "WeChatAuthEnabled":
  74. if option.Value == "true" && common.WeChatServerAddress == "" {
  75. c.JSON(http.StatusOK, gin.H{
  76. "success": false,
  77. "message": "无法启用微信登录,请先填入微信登录相关配置信息!",
  78. })
  79. return
  80. }
  81. case "TurnstileCheckEnabled":
  82. if option.Value == "true" && common.TurnstileSiteKey == "" {
  83. c.JSON(http.StatusOK, gin.H{
  84. "success": false,
  85. "message": "无法启用 Turnstile 校验,请先填入 Turnstile 校验相关配置信息!",
  86. })
  87. return
  88. }
  89. case "GroupRatio":
  90. err = setting.CheckGroupRatio(option.Value)
  91. if err != nil {
  92. c.JSON(http.StatusOK, gin.H{
  93. "success": false,
  94. "message": err.Error(),
  95. })
  96. return
  97. }
  98. }
  99. err = model.UpdateOption(option.Key, option.Value)
  100. if err != nil {
  101. c.JSON(http.StatusOK, gin.H{
  102. "success": false,
  103. "message": err.Error(),
  104. })
  105. return
  106. }
  107. c.JSON(http.StatusOK, gin.H{
  108. "success": true,
  109. "message": "",
  110. })
  111. return
  112. }