From c3c250cd9105ce010e3f6bb17722ee24f825b4c2 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Thu, 2 Jan 2020 22:15:10 +0100 Subject: [PATCH] [MM-16798] Add a Guest Accounts feature flag (#13231) Co-authored-by: mattermod --- api4/role.go | 4 ++-- api4/team.go | 2 +- api4/user.go | 8 ++++---- model/license.go | 6 ++++++ model/license_test.go | 3 +++ utils/license.go | 1 + 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/api4/role.go b/api4/role.go index 85b4c2fc13..7ef0b5ef20 100644 --- a/api4/role.go +++ b/api4/role.go @@ -96,7 +96,7 @@ func patchRole(c *Context, w http.ResponseWriter, r *http.Request) { return } - if c.App.License() == nil && patch.Permissions != nil { + if (c.App.License() == nil || !*c.App.License().Features.GuestAccounts) && patch.Permissions != nil { if oldRole.Name == "system_guest" || oldRole.Name == "team_guest" || oldRole.Name == "channel_guest" { c.Err = model.NewAppError("Api4.PatchRoles", "api.roles.patch_roles.license.error", nil, "", http.StatusNotImplemented) return @@ -129,7 +129,7 @@ func patchRole(c *Context, w http.ResponseWriter, r *http.Request) { } } - if c.App.License() != nil && (oldRole.Name == "system_guest" || oldRole.Name == "team_guest" || oldRole.Name == "channel_guest") && !*c.App.License().Features.GuestAccountsPermissions { + if c.App.License() != nil && *c.App.License().Features.GuestAccounts && (oldRole.Name == "system_guest" || oldRole.Name == "team_guest" || oldRole.Name == "channel_guest") && !*c.App.License().Features.GuestAccountsPermissions { c.Err = model.NewAppError("Api4.PatchRoles", "api.roles.patch_roles.license.error", nil, "", http.StatusNotImplemented) return } diff --git a/api4/team.go b/api4/team.go index ef25a918b1..e6e014e367 100644 --- a/api4/team.go +++ b/api4/team.go @@ -968,7 +968,7 @@ func inviteUsersToTeam(c *Context, w http.ResponseWriter, r *http.Request) { } func inviteGuestsToChannels(c *Context, w http.ResponseWriter, r *http.Request) { - if c.App.License() == nil { + if c.App.License() == nil || !*c.App.License().Features.GuestAccounts { c.Err = model.NewAppError("Api4.InviteGuestsToChannels", "api.team.invate_guests_to_channels.license.error", nil, "", http.StatusNotImplemented) return } diff --git a/api4/user.go b/api4/user.go index a68a03441a..bfa9c75a03 100644 --- a/api4/user.go +++ b/api4/user.go @@ -104,7 +104,7 @@ func createUser(c *Context, w http.ResponseWriter, r *http.Request) { } if token.Type == app.TOKEN_TYPE_GUEST_INVITATION { - if c.App.License() == nil { + if c.App.License() == nil || !*c.App.License().Features.GuestAccounts { c.Err = model.NewAppError("CreateUserWithToken", "api.user.create_user.guest_accounts.license.app_error", nil, "", http.StatusBadRequest) return } @@ -1387,7 +1387,7 @@ func login(c *Context, w http.ResponseWriter, r *http.Request) { } if user.IsGuest() { - if c.App.License() == nil { + if c.App.License() == nil || !*c.App.License().Features.GuestAccounts { c.Err = model.NewAppError("login", "api.user.login.guest_accounts.license.error", nil, "", http.StatusUnauthorized) return } @@ -1971,7 +1971,7 @@ func promoteGuestToUser(c *Context, w http.ResponseWriter, r *http.Request) { return } - if c.App.License() == nil { + if c.App.License() == nil || !*c.App.License().Features.GuestAccounts { c.Err = model.NewAppError("Api4.promoteGuestToUser", "api.team.promote_guest_to_user.license.error", nil, "", http.StatusNotImplemented) return } @@ -2011,7 +2011,7 @@ func demoteUserToGuest(c *Context, w http.ResponseWriter, r *http.Request) { return } - if c.App.License() == nil { + if c.App.License() == nil || !*c.App.License().Features.GuestAccounts { c.Err = model.NewAppError("Api4.demoteUserToGuest", "api.team.demote_user_to_guest.license.error", nil, "", http.StatusNotImplemented) return } diff --git a/model/license.go b/model/license.go index eaf2439797..dfccd667f2 100644 --- a/model/license.go +++ b/model/license.go @@ -59,6 +59,7 @@ type Features struct { MessageExport *bool `json:"message_export"` CustomPermissionsSchemes *bool `json:"custom_permissions_schemes"` CustomTermsOfService *bool `json:"custom_terms_of_service"` + GuestAccounts *bool `json:"guest_accounts"` GuestAccountsPermissions *bool `json:"guest_accounts_permissions"` IDLoadedPushNotifications *bool `json:"id_loaded"` LockTeammateNameDisplay *bool `json:"lock_teammate_name_display"` @@ -84,6 +85,7 @@ func (f *Features) ToMap() map[string]interface{} { "data_retention": *f.DataRetention, "message_export": *f.MessageExport, "custom_permissions_schemes": *f.CustomPermissionsSchemes, + "guest_accounts": *f.GuestAccounts, "guest_accounts_permissions": *f.GuestAccountsPermissions, "id_loaded": *f.IDLoadedPushNotifications, "lock_teammate_name_display": *f.LockTeammateNameDisplay, @@ -168,6 +170,10 @@ func (f *Features) SetDefaults() { f.CustomPermissionsSchemes = NewBool(*f.FutureFeatures) } + if f.GuestAccounts == nil { + f.GuestAccounts = NewBool(*f.FutureFeatures) + } + if f.GuestAccountsPermissions == nil { f.GuestAccountsPermissions = NewBool(*f.FutureFeatures) } diff --git a/model/license_test.go b/model/license_test.go index d14eb33264..638a5ea347 100644 --- a/model/license_test.go +++ b/model/license_test.go @@ -78,6 +78,7 @@ func TestLicenseFeaturesSetDefaults(t *testing.T) { *f.DataRetention = true *f.MessageExport = true *f.CustomPermissionsSchemes = true + *f.GuestAccounts = true *f.GuestAccountsPermissions = true *f.EmailNotificationContents = true *f.IDLoadedPushNotifications = true @@ -100,6 +101,7 @@ func TestLicenseFeaturesSetDefaults(t *testing.T) { CheckTrue(t, *f.DataRetention) CheckTrue(t, *f.MessageExport) CheckTrue(t, *f.CustomPermissionsSchemes) + CheckTrue(t, *f.GuestAccounts) CheckTrue(t, *f.GuestAccountsPermissions) CheckTrue(t, *f.IDLoadedPushNotifications) CheckFalse(t, *f.FutureFeatures) @@ -176,6 +178,7 @@ func TestLicenseToFromJson(t *testing.T) { CheckBool(t, *f1.DataRetention, *f.DataRetention) CheckBool(t, *f1.MessageExport, *f.MessageExport) CheckBool(t, *f1.CustomPermissionsSchemes, *f.CustomPermissionsSchemes) + CheckBool(t, *f1.GuestAccounts, *f.GuestAccounts) CheckBool(t, *f1.GuestAccountsPermissions, *f.GuestAccountsPermissions) CheckBool(t, *f1.IDLoadedPushNotifications, *f.IDLoadedPushNotifications) CheckBool(t, *f1.FutureFeatures, *f.FutureFeatures) diff --git a/utils/license.go b/utils/license.go index 62a32b1ca7..4ce3f2b790 100644 --- a/utils/license.go +++ b/utils/license.go @@ -155,6 +155,7 @@ func GetClientLicense(l *model.License) map[string]string { props["EmailNotificationContents"] = strconv.FormatBool(*l.Features.EmailNotificationContents) props["MessageExport"] = strconv.FormatBool(*l.Features.MessageExport) props["CustomPermissionsSchemes"] = strconv.FormatBool(*l.Features.CustomPermissionsSchemes) + props["GuestAccounts"] = strconv.FormatBool(*l.Features.GuestAccounts) props["GuestAccountsPermissions"] = strconv.FormatBool(*l.Features.GuestAccountsPermissions) props["CustomTermsOfService"] = strconv.FormatBool(*l.Features.CustomTermsOfService) props["LockTeammateNameDisplay"] = strconv.FormatBool(*l.Features.LockTeammateNameDisplay)