Commit af275fe9 authored by Harshil Sharma's avatar Harshil Sharma Committed by Jesse Hallam

#MM-12130 changes for custom service terms (#9450)

* #MM-12130 changes for custom service terms

* Fixed styling

* Added getServiceTerms API

* removed unnecessary panic

* removed custom service terms text from flat config

* reverted user sql store as those changes are no longer needed

* added tests

* Updated a config key to be more standard

* Added copyright info

* Loading service terms only if the feature is enabled

* Loading service terms only if the feature is enabled

* removed unused index

* added createservice termns API

* made a param to bool instead of string

* added createservice termns API

* review fixes

* fixed styling

* Minor refactoring

* removed saveConfig and loadConfig magic

* added empty service terms text check to createServiceTerms API

* refactoed some urls to be terms_of_service instead of service_terms

* removed check for support settings

* changed URLs in tests

* removed unused code

* fixed a bug

* added service termd id in conif

* fixed a test

* review fixes

* minor fixes

* Fixed TestCreateServiceTerms
parent 4e59a272
......@@ -308,7 +308,7 @@ check-licenses: ## Checks license status.
check-prereqs: ## Checks prerequisite software status.
./scripts/prereq-check.sh
check-style: govet gofmt check-licenses ## Runs govet and gofmt against all packages.
test-te-race: ## Checks for race conditions in the team edition.
......
......@@ -107,6 +107,8 @@ type Routes struct {
ReactionByNameForPostForUser *mux.Router // 'api/v4/users/{user_id:[A-Za-z0-9]+}/posts/{post_id:[A-Za-z0-9]+}/reactions/{emoji_name:[A-Za-z0-9_-+]+}'
Webrtc *mux.Router // 'api/v4/webrtc'
ServiceTerms *mux.Router // 'api/v4/service_terms
}
type API struct {
......@@ -203,6 +205,8 @@ func Init(a *app.App, root *mux.Router) *API {
api.BaseRoutes.Image = api.BaseRoutes.ApiRoot.PathPrefix("/image").Subrouter()
api.BaseRoutes.ServiceTerms = api.BaseRoutes.ApiRoot.PathPrefix("/terms_of_service").Subrouter()
api.InitUser()
api.InitTeam()
api.InitChannel()
......@@ -231,6 +235,7 @@ func Init(a *app.App, root *mux.Router) *API {
api.InitRole()
api.InitScheme()
api.InitImage()
api.InitServiceTerms()
root.Handle("/api/v4/{anything:.*}", http.HandlerFunc(api.Handle404))
......
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package api4
import (
"github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/model"
"net/http"
)
func (api *API) InitServiceTerms() {
api.BaseRoutes.ServiceTerms.Handle("", api.ApiSessionRequired(getServiceTerms)).Methods("GET")
api.BaseRoutes.ServiceTerms.Handle("", api.ApiSessionRequired(createServiceTerms)).Methods("POST")
}
func getServiceTerms(c *Context, w http.ResponseWriter, r *http.Request) {
serviceTerms, err := c.App.GetLatestServiceTerms()
if err != nil {
c.Err = err
return
}
w.Write([]byte(serviceTerms.ToJson()))
}
func createServiceTerms(c *Context, w http.ResponseWriter, r *http.Request) {
if license := c.App.License(); license == nil || !*license.Features.CustomTermsOfService {
c.Err = model.NewAppError("createServiceTerms", "api.create_service_terms.custom_service_terms_disabled.app_error", nil, "", http.StatusBadRequest)
return
}
props := model.MapFromJson(r.Body)
text := props["text"]
userId := c.Session.UserId
if text == "" {
c.Err = model.NewAppError("Config.IsValid", "api.create_service_terms.empty_text.app_error", nil, "", http.StatusBadRequest)
return
}
oldServiceTerms, err := c.App.GetLatestServiceTerms()
if err != nil && err.Id != app.ERROR_SERVICE_TERMS_NO_ROWS_FOUND {
c.Err = err
return
}
if oldServiceTerms == nil || oldServiceTerms.Text != text {
serviceTerms, err := c.App.CreateServiceTerms(text, userId)
if err != nil {
c.Err = err
return
}
w.Write([]byte(serviceTerms.ToJson()))
} else {
w.Write([]byte(oldServiceTerms.ToJson()))
}
}
package api4
import (
"github.com/mattermost/mattermost-server/model"
"github.com/stretchr/testify/assert"
"testing"
)
func TestGetServiceTerms(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
Client := th.Client
_, err := th.App.CreateServiceTerms("abc", th.BasicUser.Id)
if err != nil {
t.Fatal(err)
}
serviceTerms, resp := Client.GetServiceTerms("")
CheckNoError(t, resp)
assert.NotNil(t, serviceTerms)
assert.Equal(t, "abc", serviceTerms.Text)
assert.NotEmpty(t, serviceTerms.Id)
assert.NotEmpty(t, serviceTerms.CreateAt)
}
func TestCreateServiceTerms(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
Client := th.Client
serviceTerms, resp := Client.CreateServiceTerms("service terms new", th.BasicUser.Id)
CheckErrorMessage(t, resp, "api.create_service_terms.custom_service_terms_disabled.app_error")
th.App.SetLicense(model.NewTestLicense("EnableCustomServiceTerms"))
serviceTerms, resp = Client.CreateServiceTerms("service terms new", th.BasicUser.Id)
CheckNoError(t, resp)
assert.NotEmpty(t, serviceTerms.Id)
assert.NotEmpty(t, serviceTerms.CreateAt)
assert.Equal(t, "service terms new", serviceTerms.Text)
assert.Equal(t, th.BasicUser.Id, serviceTerms.UserId)
}
......@@ -39,6 +39,7 @@ func (api *API) InitUser() {
api.BaseRoutes.Users.Handle("/password/reset/send", api.ApiHandler(sendPasswordReset)).Methods("POST")
api.BaseRoutes.Users.Handle("/email/verify", api.ApiHandler(verifyUserEmail)).Methods("POST")
api.BaseRoutes.Users.Handle("/email/verify/send", api.ApiHandler(sendVerificationEmail)).Methods("POST")
api.BaseRoutes.User.Handle("/terms_of_service", api.ApiSessionRequired(registerServiceTermsAction)).Methods("POST")
api.BaseRoutes.User.Handle("/auth", api.ApiSessionRequiredTrustRequester(updateUserAuth)).Methods("PUT")
......@@ -1544,3 +1545,24 @@ func enableUserAccessToken(c *Context, w http.ResponseWriter, r *http.Request) {
c.LogAudit("success - token_id=" + accessToken.Id)
ReturnStatusOK(w)
}
func registerServiceTermsAction(c *Context, w http.ResponseWriter, r *http.Request) {
props := model.StringInterfaceFromJson(r.Body)
userId := c.Session.UserId
serviceTermsId := props["serviceTermsId"].(string)
accepted := props["accepted"].(bool)
if _, err := c.App.GetServiceTerms(serviceTermsId); err != nil {
c.Err = err
return
}
if err := c.App.RecordUserServiceTermsAction(userId, serviceTermsId, accepted); err != nil {
c.Err = err
return
}
c.LogAudit("ServiceTermsId=" + serviceTermsId + ", accepted=" + strconv.FormatBool(accepted))
ReturnStatusOK(w)
}
......@@ -3019,3 +3019,28 @@ func TestGetUsersByStatus(t *testing.T) {
}
})
}
func TestRegisterServiceTermsAction(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
Client := th.Client
success, resp := Client.RegisterServiceTermsAction(th.BasicUser.Id, "st_1", true)
CheckErrorMessage(t, resp, "store.sql_service_terms_store.get.no_rows.app_error")
serviceTerms, err := th.App.CreateServiceTerms("service terms", th.BasicUser.Id)
if err != nil {
t.Fatal(err)
}
success, resp = Client.RegisterServiceTermsAction(th.BasicUser.Id, serviceTerms.Id, true)
CheckNoError(t, resp)
assert.True(t, *success)
user, err := th.App.GetUser(th.BasicUser.Id)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, user.AcceptedServiceTermsId, serviceTerms.Id)
}
......@@ -179,6 +179,7 @@ func (a *App) SaveConfig(cfg *model.Config, sendConfigChangeClusterMessage bool)
}
a.DisableConfigWatch()
a.UpdateConfig(func(update *model.Config) {
*update = *cfg
})
......
......@@ -218,6 +218,7 @@ func New(options ...Option) (outApp *App, outErr error) {
}
app.Srv.Store = app.newStore()
app.AddConfigListener(func(_, current *model.Config) {
if current.SqlSettings.EnablePublicChannelsMaterialization != nil && !*current.SqlSettings.EnablePublicChannelsMaterialization {
app.Srv.Store.Channel().DisableExperimentalPublicChannelsMaterialization()
......
......@@ -23,6 +23,10 @@ import (
"github.com/mattermost/mattermost-server/utils"
)
const (
ERROR_SERVICE_TERMS_NO_ROWS_FOUND = "store.sql_service_terms_store.get.no_rows.app_error"
)
func (a *App) Config() *model.Config {
if cfg := a.config.Load(); cfg != nil {
return cfg.(*model.Config)
......@@ -242,6 +246,16 @@ func (a *App) AsymmetricSigningKey() *ecdsa.PrivateKey {
func (a *App) regenerateClientConfig() {
a.clientConfig = utils.GenerateClientConfig(a.Config(), a.DiagnosticId(), a.License())
if a.clientConfig["EnableCustomServiceTerms"] == "true" {
serviceTerms, err := a.GetLatestServiceTerms()
if err != nil {
mlog.Err(err)
} else {
a.clientConfig["CustomServiceTermsId"] = serviceTerms.Id
}
}
a.limitedClientConfig = utils.GenerateLimitedClientConfig(a.Config(), a.DiagnosticId(), a.License())
if key := a.AsymmetricSigningKey(); key != nil {
......
......@@ -263,6 +263,7 @@ func (a *App) trackConfig() {
"experimental_limit_client_config": *cfg.ServiceSettings.ExperimentalLimitClientConfig,
"enable_email_invitations": *cfg.ServiceSettings.EnableEmailInvitations,
"experimental_channel_organization": *cfg.ServiceSettings.ExperimentalChannelOrganization,
"custom_service_terms_enabled": *cfg.SupportSettings.CustomServiceTermsEnabled,
})
a.SendDiagnostic(TRACK_CONFIG_TEAM, map[string]interface{}{
......
......@@ -103,7 +103,7 @@ func TestDiagnostics(t *testing.T) {
info := ""
// Collect the info sent.
Loop:
Loop:
for {
select {
case result := <-data:
......
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package app
import (
"github.com/mattermost/mattermost-server/model"
)
func (a *App) CreateServiceTerms(text, userId string) (*model.ServiceTerms, *model.AppError) {
serviceTerms := &model.ServiceTerms{
Text: text,
UserId: userId,
}
if _, err := a.GetUser(userId); err != nil {
return nil, err
}
result := <-a.Srv.Store.ServiceTerms().Save(serviceTerms)
if result.Err != nil {
return nil, result.Err
}
serviceTerms = result.Data.(*model.ServiceTerms)
return serviceTerms, nil
}
func (a *App) GetLatestServiceTerms() (*model.ServiceTerms, *model.AppError) {
if result := <-a.Srv.Store.ServiceTerms().GetLatest(true); result.Err != nil {
return nil, result.Err
} else {
serviceTerms := result.Data.(*model.ServiceTerms)
return serviceTerms, nil
}
}
func (a *App) GetServiceTerms(id string) (*model.ServiceTerms, *model.AppError) {
if result := <-a.Srv.Store.ServiceTerms().Get(id, true); result.Err != nil {
return nil, result.Err
} else {
serviceTerms := result.Data.(*model.ServiceTerms)
return serviceTerms, nil
}
}
......@@ -245,7 +245,6 @@ func (a *App) createUser(user *model.User) (*model.User, *model.AppError) {
}
ruser.Sanitize(map[string]bool{})
return ruser, nil
}
}
......@@ -1616,3 +1615,22 @@ func (a *App) UpdateOAuthUserAttrs(userData io.Reader, user *model.User, provide
return nil
}
func (a *App) RecordUserServiceTermsAction(userId, serviceTermsId string, accepted bool) *model.AppError {
user, err := a.GetUser(userId)
if err != nil {
return err
}
if accepted {
user.AcceptedServiceTermsId = serviceTermsId
} else {
user.AcceptedServiceTermsId = ""
}
_, err = a.UpdateUser(user, false)
if err != nil {
return err
}
return nil
}
......@@ -524,3 +524,43 @@ func TestPermanentDeleteUser(t *testing.T) {
t.Fatal("GetFileInfo after DeleteUser is nil")
}
}
func TestRecordUserServiceTermsAction(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
user := &model.User{
Email: strings.ToLower(model.NewId()) + "success+test@example.com",
Nickname: "Luke Skywalker", // trying to bring balance to the "Force", one test user at a time
Username: "luke" + model.NewId(),
Password: "passwd1",
AuthService: "",
}
user, err := th.App.CreateUser(user)
if err != nil {
t.Fatalf("failed to create user: %v", err)
}
defer th.App.PermanentDeleteUser(user)
serviceTerms, err := th.App.CreateServiceTerms("text", user.Id)
if err != nil {
t.Fatalf("failed to create service terms: %v", err)
}
err = th.App.RecordUserServiceTermsAction(user.Id, serviceTerms.Id, true)
if err != nil {
t.Fatalf("failed to record user action: %v", err)
}
nuser, err := th.App.GetUser(user.Id)
assert.Equal(t, serviceTerms.Id, nuser.AcceptedServiceTermsId)
err = th.App.RecordUserServiceTermsAction(user.Id, serviceTerms.Id, false)
if err != nil {
t.Fatalf("failed to record user action: %v", err)
}
nuser, err = th.App.GetUser(user.Id)
assert.Empty(t, nuser.AcceptedServiceTermsId)
}
......@@ -2354,6 +2354,10 @@
"id": "api.user.verify_email.broken_token.app_error",
"translation": "Bad verify email token type."
},
{
"id": "api.user.register_service_terms_action.bad_value.app_error",
"translation": "Bad accepted value"
},
{
"id": "api.web_socket.connect.upgrade.app_error",
"translation": "Failed to upgrade websocket connection"
......@@ -2414,6 +2418,14 @@
"id": "api.websocket_handler.invalid_param.app_error",
"translation": "Invalid {{.Name}} parameter"
},
{
"id": "api.create_service_terms.empty_text.app_error",
"translation": "Please enter text for your Custom Terms of Service."
},
{
"id": "api.create_service_terms.custom_service_terms_disabled.app_error",
"translation": "Custom terms of service feature is disabled"
},
{
"id": "app.admin.test_email.failure",
"translation": "Connection unsuccessful: {{.Error}}"
......@@ -3202,6 +3214,10 @@
"id": "ent.cluster.save_config.error",
"translation": "System Console is set to read-only when High Availability is enabled unless ReadOnlyConfig is disabled in the configuration file."
},
{
"id": "ent.cluster.save_config.update_custom_service_terms_no_user.error",
"translation": "Custom service terms can only be changed if provided with exactly one user id"
},
{
"id": "ent.compliance.bad_export_type.appError",
"translation": "Unknown output format {{.ExportType}}"
......@@ -4722,6 +4738,22 @@
"id": "model.websocket_client.connect_fail.app_error",
"translation": "Unable to connect to the WebSocket server."
},
{
"id": "model.service_terms.is_valid.id.app_error",
"translation": "Invalid term of service id."
},
{
"id": "model.service_terms.is_valid.create_at.app_error",
"translation": "Missing required term of service property: create_at."
},
{
"id": "model.service_terms.is_valid.user_id.app_error",
"translation": "Missing required terms of service property: user_id."
},
{
"id": "model.service_terms.is_valid.text.app_error",
"translation": "Invalid terms of service text."
},
{
"id": "oauth.gitlab.tos.error",
"translation": "GitLab's Terms of Service have updated. Please go to gitlab.com to accept them and then try logging into Mattermost again."
......@@ -6350,6 +6382,22 @@
"id": "store.sql_webhooks.update_outgoing.app_error",
"translation": "Unable to update the webhook"
},
{
"id": "store.sql_service_terms_store.save.existing.app_error",
"translation": "Must not call save for existing service terms"
},
{
"id": "store.sql_service_terms.save.app_error",
"translation": "Unable to save service terms"
},
{
"id": "store.sql_service_terms_store.get.app_error",
"translation": "Unable to fetch service terms"
},
{
"id": "store.sql_service_terms_store.get.no_rows.app_error",
"translation": "No service terms found"
},
{
"id": "system.message.name",
"translation": "System"
......
......@@ -401,6 +401,14 @@ func (c *Client4) GetRedirectLocationRoute() string {
return fmt.Sprintf("/redirect_location")
}
func (c *Client4) GetRegisterServiceTermsRoute(userId string) string {
return c.GetUserRoute(userId) + "/terms_of_service"
}
func (c *Client4) GetServiceTermsRoute() string {
return "/terms_of_service"
}
func (c *Client4) DoApiGet(url string, etag string) (*http.Response, *AppError) {
return c.DoApiRequest(http.MethodGet, c.ApiUrl+url, "", etag)
}
......@@ -3794,3 +3802,38 @@ func (c *Client4) GetRedirectLocation(urlParam, etag string) (string, *Response)
return MapFromJson(r.Body)["location"], BuildResponse(r)
}
}
func (c *Client4) RegisterServiceTermsAction(userId, serviceTermsId string, accepted bool) (*bool, *Response) {
url := c.GetRegisterServiceTermsRoute(userId)
data := map[string]interface{}{"serviceTermsId": serviceTermsId, "accepted": accepted}
if r, err := c.DoApiPost(url, StringInterfaceToJson(data)); err != nil {
return nil, BuildErrorResponse(r, err)
} else {
defer closeBody(r)
return NewBool(CheckStatusOK(r)), BuildResponse(r)
}
}
func (c *Client4) GetServiceTerms(etag string) (*ServiceTerms, *Response) {
url := c.GetServiceTermsRoute()
if r, err := c.DoApiGet(url, etag); err != nil {
return nil, BuildErrorResponse(r, err)
} else {
defer closeBody(r)
return ServiceTermsFromJson(r.Body), BuildResponse(r)
}
}
func (c *Client4) CreateServiceTerms(text, userId string) (*ServiceTerms, *Response) {
url := c.GetServiceTermsRoute()
data := map[string]string{"text": text}
if r, err := c.DoApiPost(url, MapToJson(data)); err != nil {
return nil, BuildErrorResponse(r, err)
} else {
defer closeBody(r)
return ServiceTermsFromJson(r.Body), BuildResponse(r)
}
}
......@@ -996,12 +996,13 @@ type PrivacySettings struct {
}
type SupportSettings struct {
TermsOfServiceLink *string
PrivacyPolicyLink *string
AboutLink *string
HelpLink *string
ReportAProblemLink *string
SupportEmail *string
TermsOfServiceLink *string
PrivacyPolicyLink *string
AboutLink *string
HelpLink *string
ReportAProblemLink *string
SupportEmail *string
CustomServiceTermsEnabled *bool
}
func (s *SupportSettings) SetDefaults() {
......@@ -1048,6 +1049,10 @@ func (s *SupportSettings) SetDefaults() {
if s.SupportEmail == nil {
s.SupportEmail = NewString(SUPPORT_SETTINGS_DEFAULT_SUPPORT_EMAIL)
}
if s.CustomServiceTermsEnabled == nil {
s.CustomServiceTermsEnabled = NewBool(false)
}
}
type AnnouncementSettings struct {
......
......@@ -55,6 +55,7 @@ type Features struct {
DataRetention *bool `json:"data_retention"`
MessageExport *bool `json:"message_export"`
CustomPermissionsSchemes *bool `json:"custom_permissions_schemes"`
CustomTermsOfService *bool `json:"custom_terms_of_service"`
// after we enabled more features for webrtc we'll need to control them with this
FutureFeatures *bool `json:"future_features"`
......@@ -152,6 +153,10 @@ func (f *Features) SetDefaults() {
if f.CustomPermissionsSchemes == nil {
f.CustomPermissionsSchemes = NewBool(*f.FutureFeatures)
}
if f.CustomTermsOfService == nil {
f.CustomTermsOfService = NewBool(*f.FutureFeatures)
}
}
func (l *License) IsExpired() bool {
......
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package model
import (
"encoding/json"
"fmt"
"io"
"net/http"
"unicode/utf8"
)
// we only ever need the latest version of service terms
const SERVICE_TERMS_CACHE_SIZE = 1
type ServiceTerms struct {
Id string `json:"id"`
CreateAt int64 `json:"create_at"`
UserId string `json:"user_id"`
Text string `json:"text"`
}
func (t *ServiceTerms) IsValid() *AppError {
if len(t.Id) != 26 {
return InvalidServiceTermsError("id", "")
}
if t.CreateAt == 0 {
return InvalidServiceTermsError("create_at", t.Id)
}
if len(t.UserId) != 26 {
return InvalidServiceTermsError("user_id", t.Id)
}
if utf8.RuneCountInString(t.Text) > POST_MESSAGE_MAX_RUNES_V2 {
return InvalidServiceTermsError("text", t.Id)
}
return nil
}
func (t *ServiceTerms) ToJson() string {
b, _ := json.Marshal(t)
return string(b)
}
func ServiceTermsFromJson(data io.Reader) *ServiceTerms {
var serviceTerms *ServiceTerms
json.NewDecoder(data).Decode(&serviceTerms)
return serviceTerms
}
func InvalidServiceTermsError(fieldName string, serviceTermsId string) *AppError {
id := fmt.Sprintf("model.term.is_valid.%s.app_error", fieldName)
details := ""
if serviceTermsId != "" {
details = "service_terms_id=" + serviceTermsId
}
return NewAppError("ServiceTerms.IsValid", id, nil, details, http.StatusBadRequest)
}
func (t *ServiceTerms) PreSave() {
if t.Id == "" {
t.Id = NewId()
}
t.CreateAt = GetMillis()
}
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package model
import (
"github.com/stretchr/testify/assert"
"strings"
"testing"
)
func TestServiceTermsIsValid(t *testing.T) {
s := ServiceTerms{}
if err := s.IsValid(); err == nil {
t.Fatal("should be invalid")
}
s.Id = NewId()
if err := s.IsValid(); err == nil {
t.Fatal("should be invalid")
}
s.CreateAt = GetMillis()
if err := s.IsValid(); err == nil {
t.Fatal("should be invalid")
}
s.UserId = NewId()
if err := s.IsValid(); err != nil {
t.Fatal("should be invalid")
}
s.Text = strings.Repeat("0", POST_MESSAGE_MAX_RUNES_V2+1)
if err := s.IsValid(); err == nil {
t.Fatal("should be invalid")
}
s.Text = strings.Repeat("0", POST_MESSAGE_MAX_RUNES_V2)
if err := s.IsValid(); err != nil {
t.Fatal(err)
}
s.Text = "test"
if err := s.IsValid(); err != nil {
t.Fatal(err)
}
}