diff --git a/model/instance/instance.go b/model/instance/instance.go index 215a7cb4394..632cc470f91 100644 --- a/model/instance/instance.go +++ b/model/instance/instance.go @@ -730,6 +730,15 @@ func (i *Instance) MovedError() *jsonapi.Error { return &jerr } +func (i *Instance) HasPremiumLinksEnabled() bool { + if ctxSettings, ok := i.SettingsContext(); ok { + if enabled, ok := ctxSettings["enable_premium_links"].(bool); ok { + return enabled + } + } + return false +} + // ensure Instance implements couchdb.Doc var ( _ couchdb.Doc = &Instance{} diff --git a/model/notification/center/notification_center.go b/model/notification/center/notification_center.go index eb70e8b2be3..f6f9fdeb153 100644 --- a/model/notification/center/notification_center.go +++ b/model/notification/center/notification_center.go @@ -83,16 +83,16 @@ func init() { _ = PushStack(domain, NotificationDiskQuota, n) }) - oauth.RegisterClientsLimitAlertCallback(func(i *instance.Instance, clientName string, clientsLimit int, enablePremiumLinks bool) { + oauth.RegisterClientsLimitAlertCallback(func(i *instance.Instance, clientName string, clientsLimit int) { devicesLink := i.SubDomain(consts.SettingsSlug) devicesLink.Fragment = "/connectedDevices" var offersLink string - if enablePremiumLinks { + if i.HasPremiumLinksEnabled() { var err error offersLink, err = i.ManagerURL(instance.ManagerPremiumURL) if err != nil { - return + i.Logger().Errorf("Could not get instance Premium Manager URL: %s", err.Error()) } } diff --git a/model/oauth/client.go b/model/oauth/client.go index 5beb557799f..56a803cf150 100644 --- a/model/oauth/client.go +++ b/model/oauth/client.go @@ -524,14 +524,14 @@ func (c *Client) Create(i *instance.Instance, opts ...CreateOptions) *ClientRegi Errorf("Failed to get the OAuth clients limit: %s", err) return nil } + limit := -1 if clientsLimit, ok := flags.M["cozy.oauthclients.max"].(float64); ok && clientsLimit >= 0 { limit = int(clientsLimit) } _, exceeded := CheckOAuthClientsLimitReached(i, limit) if exceeded { - enablePremiumLinks, _ := flags.M["enable_premium_links"].(bool) - PushClientsLimitAlert(i, c.ClientName, limit, enablePremiumLinks) + PushClientsLimitAlert(i, c.ClientName, limit) } return nil } @@ -851,19 +851,19 @@ func CheckOAuthClientsLimitReached(i *instance.Instance, limit int) (reached, ex return } -var cbClientsLimitAlert func(i *instance.Instance, clientName string, clientsLimit int, enablePremiumLinks bool) +var cbClientsLimitAlert func(i *instance.Instance, clientName string, clientsLimit int) // RegisterClientsLimitAlertCallback allows to register a callback function // called when the connected OAuth clients limit (if present) is exceeded. -func RegisterClientsLimitAlertCallback(cb func(i *instance.Instance, clientName string, clientsLimit int, enablePremiumLinks bool)) { +func RegisterClientsLimitAlertCallback(cb func(i *instance.Instance, clientName string, clientsLimit int)) { cbClientsLimitAlert = cb } // PushClientsLimitAlert can be used to notify when the connected OAuth clients // limit (if present) is exceeded. -func PushClientsLimitAlert(i *instance.Instance, clientName string, clientsLimit int, enablePremiumLinks bool) { +func PushClientsLimitAlert(i *instance.Instance, clientName string, clientsLimit int) { if cbClientsLimitAlert != nil { - cbClientsLimitAlert(i, clientName, clientsLimit, enablePremiumLinks) + cbClientsLimitAlert(i, clientName, clientsLimit) } } diff --git a/tests/testutils/test_utils.go b/tests/testutils/test_utils.go index 1641be93820..7496e811084 100644 --- a/tests/testutils/test_utils.go +++ b/tests/testutils/test_utils.go @@ -5,6 +5,7 @@ import ( "context" "errors" "flag" + "fmt" "io" "net/http/httptest" "net/url" @@ -421,14 +422,16 @@ func WithManager(t *testing.T, inst *instance.Instance) (shouldRemoveUUID bool) require.True(t, ok, "Could not enable test instance manager: manager_url config is required") require.NotEmpty(t, managerURL, "Could not enable test instance manager: manager_url config is required") - if inst.FeatureFlags == nil { - inst.FeatureFlags = map[string]interface{}{} - } - inst.FeatureFlags["enable_premium_links"] = true + was := config["enable_premium_links"] + config["enable_premium_links"] = true t.Cleanup(func() { - err := DisableManager(inst, shouldRemoveUUID) - require.NoError(t, err) + config["enable_premium_links"] = was + + if shouldRemoveUUID { + inst.UUID = "" + require.NoError(t, instance.Update(inst)) + } }) err := instance.Update(inst) @@ -438,16 +441,18 @@ func WithManager(t *testing.T, inst *instance.Instance) (shouldRemoveUUID bool) } func DisableManager(inst *instance.Instance, shouldRemoveUUID bool) error { - if inst.FeatureFlags == nil { - inst.FeatureFlags = map[string]interface{}{} + config, ok := inst.SettingsContext() + if !ok { + return fmt.Errorf("Could not disable test instance manager: could not fetch test instance settings context") } - inst.FeatureFlags["enable_premium_links"] = false + + config["enable_premium_links"] = false if shouldRemoveUUID { inst.UUID = "" + return instance.Update(inst) } - - return instance.Update(inst) + return nil } func WithOAuthClientsLimit(t *testing.T, inst *instance.Instance, limit float64) { diff --git a/web/auth/oauth.go b/web/auth/oauth.go index efaa3840bee..f3296896d04 100644 --- a/web/auth/oauth.go +++ b/web/auth/oauth.go @@ -261,9 +261,9 @@ func (a *AuthorizeHTTPHandler) authorizeForm(c echo.Context) error { connectedDevicesURL.Fragment = "/connectedDevices" manageDevicesURL = connectedDevicesURL.String() - if enablePremiumLinks, ok := flags.M["enable_premium_links"].(bool); ok && enablePremiumLinks { + if inst.HasPremiumLinksEnabled() { if premiumURL, err = inst.ManagerURL(instance.ManagerPremiumURL); err != nil { - return fmt.Errorf("Could not get Premium Manager URL for instance %s: %w", inst.DomainName(), err) + inst.Logger().Errorf("Could not get instance Premium Manager URL: %s", err.Error()) } } diff --git a/web/settings/clients.go b/web/settings/clients.go index 30b08f2f238..b1098c41615 100644 --- a/web/settings/clients.go +++ b/web/settings/clients.go @@ -153,13 +153,13 @@ func (h *HTTPHandler) limitExceeded(c echo.Context) error { connectedDevicesURL.Fragment = "/connectedDevices" var premiumURL string - if enablePremiumLinks, ok := flags.M["enable_premium_links"].(bool); ok && enablePremiumLinks { + if inst.HasPremiumLinksEnabled() { isFlagship, _ := strconv.ParseBool(c.QueryParam("isFlagship")) iapEnabled, _ := flags.M["flagship.iap.enabled"].(bool) if !isFlagship || iapEnabled { var err error if premiumURL, err = inst.ManagerURL(instance.ManagerPremiumURL); err != nil { - return fmt.Errorf("Could not get Premium Manager URL for instance %s: %w", inst.DomainName(), err) + inst.Logger().Errorf("Could not get instance Premium Manager URL: %s", err.Error()) } } }