github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/service.go (about)

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