You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

service.go 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package setting
  4. import (
  5. "regexp"
  6. "strings"
  7. "time"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/structs"
  10. )
  11. // Service settings
  12. var Service = struct {
  13. DefaultUserVisibility string
  14. DefaultUserVisibilityMode structs.VisibleType
  15. AllowedUserVisibilityModes []string
  16. AllowedUserVisibilityModesSlice AllowedVisibility `ini:"-"`
  17. DefaultOrgVisibility string
  18. DefaultOrgVisibilityMode structs.VisibleType
  19. ActiveCodeLives int
  20. ResetPwdCodeLives int
  21. RegisterEmailConfirm bool
  22. RegisterManualConfirm bool
  23. EmailDomainWhitelist []string
  24. EmailDomainBlocklist []string
  25. DisableRegistration bool
  26. AllowOnlyInternalRegistration bool
  27. AllowOnlyExternalRegistration bool
  28. ShowRegistrationButton bool
  29. ShowMilestonesDashboardPage bool
  30. RequireSignInView bool
  31. EnableNotifyMail bool
  32. EnableBasicAuth bool
  33. EnableReverseProxyAuth bool
  34. EnableReverseProxyAutoRegister bool
  35. EnableReverseProxyEmail bool
  36. EnableReverseProxyFullName bool
  37. EnableCaptcha bool
  38. RequireCaptchaForLogin bool
  39. RequireExternalRegistrationCaptcha bool
  40. RequireExternalRegistrationPassword bool
  41. CaptchaType string
  42. RecaptchaSecret string
  43. RecaptchaSitekey string
  44. RecaptchaURL string
  45. HcaptchaSecret string
  46. HcaptchaSitekey string
  47. McaptchaSecret string
  48. McaptchaSitekey string
  49. McaptchaURL string
  50. DefaultKeepEmailPrivate bool
  51. DefaultAllowCreateOrganization bool
  52. DefaultUserIsRestricted bool
  53. EnableTimetracking bool
  54. DefaultEnableTimetracking bool
  55. DefaultEnableDependencies bool
  56. AllowCrossRepositoryDependencies bool
  57. DefaultAllowOnlyContributorsToTrackTime bool
  58. NoReplyAddress string
  59. EnableUserHeatmap bool
  60. AutoWatchNewRepos bool
  61. AutoWatchOnChanges bool
  62. DefaultOrgMemberVisible bool
  63. UserDeleteWithCommentsMaxTime time.Duration
  64. ValidSiteURLSchemes []string
  65. // OpenID settings
  66. EnableOpenIDSignIn bool
  67. EnableOpenIDSignUp bool
  68. OpenIDWhitelist []*regexp.Regexp
  69. OpenIDBlacklist []*regexp.Regexp
  70. // Explore page settings
  71. Explore struct {
  72. RequireSigninView bool `ini:"REQUIRE_SIGNIN_VIEW"`
  73. DisableUsersPage bool `ini:"DISABLE_USERS_PAGE"`
  74. } `ini:"service.explore"`
  75. }{
  76. AllowedUserVisibilityModesSlice: []bool{true, true, true},
  77. }
  78. // AllowedVisibility store in a 3 item bool array what is allowed
  79. type AllowedVisibility []bool
  80. // IsAllowedVisibility check if a AllowedVisibility allow a specific VisibleType
  81. func (a AllowedVisibility) IsAllowedVisibility(t structs.VisibleType) bool {
  82. if int(t) >= len(a) {
  83. return false
  84. }
  85. return a[t]
  86. }
  87. // ToVisibleTypeSlice convert a AllowedVisibility into a VisibleType slice
  88. func (a AllowedVisibility) ToVisibleTypeSlice() (result []structs.VisibleType) {
  89. for i, v := range a {
  90. if v {
  91. result = append(result, structs.VisibleType(i))
  92. }
  93. }
  94. return result
  95. }
  96. func newService() {
  97. sec := Cfg.Section("service")
  98. Service.ActiveCodeLives = sec.Key("ACTIVE_CODE_LIVE_MINUTES").MustInt(180)
  99. Service.ResetPwdCodeLives = sec.Key("RESET_PASSWD_CODE_LIVE_MINUTES").MustInt(180)
  100. Service.DisableRegistration = sec.Key("DISABLE_REGISTRATION").MustBool()
  101. Service.AllowOnlyInternalRegistration = sec.Key("ALLOW_ONLY_INTERNAL_REGISTRATION").MustBool()
  102. Service.AllowOnlyExternalRegistration = sec.Key("ALLOW_ONLY_EXTERNAL_REGISTRATION").MustBool()
  103. if Service.AllowOnlyExternalRegistration && Service.AllowOnlyInternalRegistration {
  104. log.Warn("ALLOW_ONLY_INTERNAL_REGISTRATION and ALLOW_ONLY_EXTERNAL_REGISTRATION are true - disabling registration")
  105. Service.DisableRegistration = true
  106. }
  107. if !sec.Key("REGISTER_EMAIL_CONFIRM").MustBool() {
  108. Service.RegisterManualConfirm = sec.Key("REGISTER_MANUAL_CONFIRM").MustBool(false)
  109. } else {
  110. Service.RegisterManualConfirm = false
  111. }
  112. Service.EmailDomainWhitelist = sec.Key("EMAIL_DOMAIN_WHITELIST").Strings(",")
  113. Service.EmailDomainBlocklist = sec.Key("EMAIL_DOMAIN_BLOCKLIST").Strings(",")
  114. Service.ShowRegistrationButton = sec.Key("SHOW_REGISTRATION_BUTTON").MustBool(!(Service.DisableRegistration || Service.AllowOnlyExternalRegistration))
  115. Service.ShowMilestonesDashboardPage = sec.Key("SHOW_MILESTONES_DASHBOARD_PAGE").MustBool(true)
  116. Service.RequireSignInView = sec.Key("REQUIRE_SIGNIN_VIEW").MustBool()
  117. Service.EnableBasicAuth = sec.Key("ENABLE_BASIC_AUTHENTICATION").MustBool(true)
  118. Service.EnableReverseProxyAuth = sec.Key("ENABLE_REVERSE_PROXY_AUTHENTICATION").MustBool()
  119. Service.EnableReverseProxyAutoRegister = sec.Key("ENABLE_REVERSE_PROXY_AUTO_REGISTRATION").MustBool()
  120. Service.EnableReverseProxyEmail = sec.Key("ENABLE_REVERSE_PROXY_EMAIL").MustBool()
  121. Service.EnableReverseProxyFullName = sec.Key("ENABLE_REVERSE_PROXY_FULL_NAME").MustBool()
  122. Service.EnableCaptcha = sec.Key("ENABLE_CAPTCHA").MustBool(false)
  123. Service.RequireCaptchaForLogin = sec.Key("REQUIRE_CAPTCHA_FOR_LOGIN").MustBool(false)
  124. Service.RequireExternalRegistrationCaptcha = sec.Key("REQUIRE_EXTERNAL_REGISTRATION_CAPTCHA").MustBool(Service.EnableCaptcha)
  125. Service.RequireExternalRegistrationPassword = sec.Key("REQUIRE_EXTERNAL_REGISTRATION_PASSWORD").MustBool()
  126. Service.CaptchaType = sec.Key("CAPTCHA_TYPE").MustString(ImageCaptcha)
  127. Service.RecaptchaSecret = sec.Key("RECAPTCHA_SECRET").MustString("")
  128. Service.RecaptchaSitekey = sec.Key("RECAPTCHA_SITEKEY").MustString("")
  129. Service.RecaptchaURL = sec.Key("RECAPTCHA_URL").MustString("https://www.google.com/recaptcha/")
  130. Service.HcaptchaSecret = sec.Key("HCAPTCHA_SECRET").MustString("")
  131. Service.HcaptchaSitekey = sec.Key("HCAPTCHA_SITEKEY").MustString("")
  132. Service.McaptchaURL = sec.Key("MCAPTCHA_URL").MustString("https://demo.mcaptcha.org/")
  133. Service.McaptchaSecret = sec.Key("MCAPTCHA_SECRET").MustString("")
  134. Service.McaptchaSitekey = sec.Key("MCAPTCHA_SITEKEY").MustString("")
  135. Service.DefaultKeepEmailPrivate = sec.Key("DEFAULT_KEEP_EMAIL_PRIVATE").MustBool()
  136. Service.DefaultAllowCreateOrganization = sec.Key("DEFAULT_ALLOW_CREATE_ORGANIZATION").MustBool(true)
  137. Service.DefaultUserIsRestricted = sec.Key("DEFAULT_USER_IS_RESTRICTED").MustBool(false)
  138. Service.EnableTimetracking = sec.Key("ENABLE_TIMETRACKING").MustBool(true)
  139. if Service.EnableTimetracking {
  140. Service.DefaultEnableTimetracking = sec.Key("DEFAULT_ENABLE_TIMETRACKING").MustBool(true)
  141. }
  142. Service.DefaultEnableDependencies = sec.Key("DEFAULT_ENABLE_DEPENDENCIES").MustBool(true)
  143. Service.AllowCrossRepositoryDependencies = sec.Key("ALLOW_CROSS_REPOSITORY_DEPENDENCIES").MustBool(true)
  144. Service.DefaultAllowOnlyContributorsToTrackTime = sec.Key("DEFAULT_ALLOW_ONLY_CONTRIBUTORS_TO_TRACK_TIME").MustBool(true)
  145. Service.NoReplyAddress = sec.Key("NO_REPLY_ADDRESS").MustString("noreply." + Domain)
  146. Service.EnableUserHeatmap = sec.Key("ENABLE_USER_HEATMAP").MustBool(true)
  147. Service.AutoWatchNewRepos = sec.Key("AUTO_WATCH_NEW_REPOS").MustBool(true)
  148. Service.AutoWatchOnChanges = sec.Key("AUTO_WATCH_ON_CHANGES").MustBool(false)
  149. Service.DefaultUserVisibility = sec.Key("DEFAULT_USER_VISIBILITY").In("public", structs.ExtractKeysFromMapString(structs.VisibilityModes))
  150. Service.DefaultUserVisibilityMode = structs.VisibilityModes[Service.DefaultUserVisibility]
  151. Service.AllowedUserVisibilityModes = sec.Key("ALLOWED_USER_VISIBILITY_MODES").Strings(",")
  152. if len(Service.AllowedUserVisibilityModes) != 0 {
  153. Service.AllowedUserVisibilityModesSlice = []bool{false, false, false}
  154. for _, sMode := range Service.AllowedUserVisibilityModes {
  155. Service.AllowedUserVisibilityModesSlice[structs.VisibilityModes[sMode]] = true
  156. }
  157. }
  158. Service.DefaultOrgVisibility = sec.Key("DEFAULT_ORG_VISIBILITY").In("public", structs.ExtractKeysFromMapString(structs.VisibilityModes))
  159. Service.DefaultOrgVisibilityMode = structs.VisibilityModes[Service.DefaultOrgVisibility]
  160. Service.DefaultOrgMemberVisible = sec.Key("DEFAULT_ORG_MEMBER_VISIBLE").MustBool()
  161. Service.UserDeleteWithCommentsMaxTime = sec.Key("USER_DELETE_WITH_COMMENTS_MAX_TIME").MustDuration(0)
  162. sec.Key("VALID_SITE_URL_SCHEMES").MustString("http,https")
  163. Service.ValidSiteURLSchemes = sec.Key("VALID_SITE_URL_SCHEMES").Strings(",")
  164. schemes := make([]string, len(Service.ValidSiteURLSchemes))
  165. for _, scheme := range Service.ValidSiteURLSchemes {
  166. scheme = strings.ToLower(strings.TrimSpace(scheme))
  167. if scheme != "" {
  168. schemes = append(schemes, scheme)
  169. }
  170. }
  171. Service.ValidSiteURLSchemes = schemes
  172. if err := Cfg.Section("service.explore").MapTo(&Service.Explore); err != nil {
  173. log.Fatal("Failed to map service.explore settings: %v", err)
  174. }
  175. sec = Cfg.Section("openid")
  176. Service.EnableOpenIDSignIn = sec.Key("ENABLE_OPENID_SIGNIN").MustBool(!InstallLock)
  177. Service.EnableOpenIDSignUp = sec.Key("ENABLE_OPENID_SIGNUP").MustBool(!Service.DisableRegistration && Service.EnableOpenIDSignIn)
  178. pats := sec.Key("WHITELISTED_URIS").Strings(" ")
  179. if len(pats) != 0 {
  180. Service.OpenIDWhitelist = make([]*regexp.Regexp, len(pats))
  181. for i, p := range pats {
  182. Service.OpenIDWhitelist[i] = regexp.MustCompilePOSIX(p)
  183. }
  184. }
  185. pats = sec.Key("BLACKLISTED_URIS").Strings(" ")
  186. if len(pats) != 0 {
  187. Service.OpenIDBlacklist = make([]*regexp.Regexp, len(pats))
  188. for i, p := range pats {
  189. Service.OpenIDBlacklist[i] = regexp.MustCompilePOSIX(p)
  190. }
  191. }
  192. }