#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
Этот коммит содержится в:
коммит произвёл
Jesse Hallam
родитель
4e59a27293
Коммит
af275fe924
2
Makefile
2
Makefile
@@ -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))
|
||||
|
||||
|
||||
59
api4/service_terms.go
Обычный файл
59
api4/service_terms.go
Обычный файл
@@ -0,0 +1,59 @@
|
||||
// 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()))
|
||||
}
|
||||
}
|
||||
44
api4/service_terms_test.go
Обычный файл
44
api4/service_terms_test.go
Обычный файл
@@ -0,0 +1,44 @@
|
||||
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)
|
||||
}
|
||||
22
api4/user.go
22
api4/user.go
@@ -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:
|
||||
|
||||
45
app/service_terms.go
Обычный файл
45
app/service_terms.go
Обычный файл
@@ -0,0 +1,45 @@
|
||||
// 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
|
||||
}
|
||||
}
|
||||
20
app/user.go
20
app/user.go
@@ -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)
|
||||
}
|
||||
|
||||
48
i18n/en.json
48
i18n/en.json
@@ -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 {
|
||||
|
||||
70
model/service_terms.go
Обычный файл
70
model/service_terms.go
Обычный файл
@@ -0,0 +1,70 @@
|
||||
// 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()
|
||||
}
|
||||
62
model/service_terms_test.go
Обычный файл
62
model/service_terms_test.go
Обычный файл
@@ -0,0 +1,62 @@
|
||||
// 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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceTermsJson(t *testing.T) {
|
||||
o := ServiceTerms{
|
||||
Id: NewId(),
|
||||
Text: NewId(),
|
||||
CreateAt: GetMillis(),
|
||||
UserId: NewId(),
|
||||
}
|
||||
j := o.ToJson()
|
||||
ro := ServiceTermsFromJson(strings.NewReader(j))
|
||||
|
||||
assert.NotNil(t, ro)
|
||||
assert.Equal(t, o, *ro)
|
||||
}
|
||||
@@ -76,5 +76,3 @@ func TestSessionCSRF(t *testing.T) {
|
||||
assert.NotEmpty(t, token2)
|
||||
assert.Equal(t, token, token2)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -48,32 +48,33 @@ const (
|
||||
)
|
||||
|
||||
type User struct {
|
||||
Id string `json:"id"`
|
||||
CreateAt int64 `json:"create_at,omitempty"`
|
||||
UpdateAt int64 `json:"update_at,omitempty"`
|
||||
DeleteAt int64 `json:"delete_at"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password,omitempty"`
|
||||
AuthData *string `json:"auth_data,omitempty"`
|
||||
AuthService string `json:"auth_service"`
|
||||
Email string `json:"email"`
|
||||
EmailVerified bool `json:"email_verified,omitempty"`
|
||||
Nickname string `json:"nickname"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Position string `json:"position"`
|
||||
Roles string `json:"roles"`
|
||||
AllowMarketing bool `json:"allow_marketing,omitempty"`
|
||||
Props StringMap `json:"props,omitempty"`
|
||||
NotifyProps StringMap `json:"notify_props,omitempty"`
|
||||
LastPasswordUpdate int64 `json:"last_password_update,omitempty"`
|
||||
LastPictureUpdate int64 `json:"last_picture_update,omitempty"`
|
||||
FailedAttempts int `json:"failed_attempts,omitempty"`
|
||||
Locale string `json:"locale"`
|
||||
Timezone StringMap `json:"timezone"`
|
||||
MfaActive bool `json:"mfa_active,omitempty"`
|
||||
MfaSecret string `json:"mfa_secret,omitempty"`
|
||||
LastActivityAt int64 `db:"-" json:"last_activity_at,omitempty"`
|
||||
Id string `json:"id"`
|
||||
CreateAt int64 `json:"create_at,omitempty"`
|
||||
UpdateAt int64 `json:"update_at,omitempty"`
|
||||
DeleteAt int64 `json:"delete_at"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password,omitempty"`
|
||||
AuthData *string `json:"auth_data,omitempty"`
|
||||
AuthService string `json:"auth_service"`
|
||||
Email string `json:"email"`
|
||||
EmailVerified bool `json:"email_verified,omitempty"`
|
||||
Nickname string `json:"nickname"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Position string `json:"position"`
|
||||
Roles string `json:"roles"`
|
||||
AllowMarketing bool `json:"allow_marketing,omitempty"`
|
||||
Props StringMap `json:"props,omitempty"`
|
||||
NotifyProps StringMap `json:"notify_props,omitempty"`
|
||||
LastPasswordUpdate int64 `json:"last_password_update,omitempty"`
|
||||
LastPictureUpdate int64 `json:"last_picture_update,omitempty"`
|
||||
FailedAttempts int `json:"failed_attempts,omitempty"`
|
||||
Locale string `json:"locale"`
|
||||
Timezone StringMap `json:"timezone"`
|
||||
MfaActive bool `json:"mfa_active,omitempty"`
|
||||
MfaSecret string `json:"mfa_secret,omitempty"`
|
||||
LastActivityAt int64 `db:"-" json:"last_activity_at,omitempty"`
|
||||
AcceptedServiceTermsId string `json:"accepted_service_terms_id,omitempty"`
|
||||
}
|
||||
|
||||
type UserPatch struct {
|
||||
|
||||
@@ -169,6 +169,10 @@ func (s *LayeredStore) Role() RoleStore {
|
||||
return s.RoleStore
|
||||
}
|
||||
|
||||
func (s *LayeredStore) ServiceTerms() ServiceTermsStore {
|
||||
return s.DatabaseLayer.ServiceTerms()
|
||||
}
|
||||
|
||||
func (s *LayeredStore) Scheme() SchemeStore {
|
||||
return s.SchemeStore
|
||||
}
|
||||
|
||||
143
store/sqlstore/service_terms_store.go
Обычный файл
143
store/sqlstore/service_terms_store.go
Обычный файл
@@ -0,0 +1,143 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/mattermost/mattermost-server/einterfaces"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/store"
|
||||
"github.com/mattermost/mattermost-server/utils"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type SqlServiceTermsStore struct {
|
||||
SqlStore
|
||||
metrics einterfaces.MetricsInterface
|
||||
}
|
||||
|
||||
var serviceTermsCache = utils.NewLru(model.SERVICE_TERMS_CACHE_SIZE)
|
||||
|
||||
const serviceTermsCacheName = "ServiceTerms"
|
||||
|
||||
func NewSqlTermStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) store.ServiceTermsStore {
|
||||
s := SqlServiceTermsStore{sqlStore, metrics}
|
||||
|
||||
for _, db := range sqlStore.GetAllConns() {
|
||||
table := db.AddTableWithName(model.ServiceTerms{}, "ServiceTerms").SetKeys(false, "Id")
|
||||
table.ColMap("Id").SetMaxSize(26)
|
||||
table.ColMap("UserId").SetMaxSize(26)
|
||||
table.ColMap("Text").SetMaxSize(model.POST_MESSAGE_MAX_BYTES_V2)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s SqlServiceTermsStore) CreateIndexesIfNotExists() {
|
||||
}
|
||||
|
||||
func (s SqlServiceTermsStore) Save(serviceTerms *model.ServiceTerms) store.StoreChannel {
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
if len(serviceTerms.Id) > 0 {
|
||||
result.Err = model.NewAppError(
|
||||
"SqlServiceTermsStore.Save",
|
||||
"store.sql_service_terms_store.save.existing.app_error",
|
||||
nil,
|
||||
"id="+serviceTerms.Id, http.StatusBadRequest,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
serviceTerms.PreSave()
|
||||
|
||||
if result.Err = serviceTerms.IsValid(); result.Err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := s.GetMaster().Insert(serviceTerms); err != nil {
|
||||
result.Err = model.NewAppError(
|
||||
"SqlServiceTermsStore.Save",
|
||||
"store.sql_service_terms.save.app_error",
|
||||
nil,
|
||||
"service_term_id="+serviceTerms.Id+",err="+err.Error(),
|
||||
http.StatusInternalServerError,
|
||||
)
|
||||
}
|
||||
|
||||
result.Data = serviceTerms
|
||||
|
||||
serviceTermsCache.AddWithDefaultExpires(serviceTerms.Id, serviceTerms)
|
||||
})
|
||||
}
|
||||
|
||||
func (s SqlServiceTermsStore) GetLatest(allowFromCache bool) store.StoreChannel {
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
if allowFromCache {
|
||||
if serviceTermsCache.Len() == 0 {
|
||||
if s.metrics != nil {
|
||||
s.metrics.IncrementMemCacheMissCounter(serviceTermsCacheName)
|
||||
}
|
||||
} else {
|
||||
if cacheItem, ok := serviceTermsCache.Get(serviceTermsCache.Keys()[0]); ok {
|
||||
if s.metrics != nil {
|
||||
s.metrics.IncrementMemCacheHitCounter(serviceTermsCacheName)
|
||||
}
|
||||
|
||||
result.Data = cacheItem.(*model.ServiceTerms)
|
||||
return
|
||||
} else if s.metrics != nil {
|
||||
s.metrics.IncrementMemCacheMissCounter(serviceTermsCacheName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var serviceTerms *model.ServiceTerms
|
||||
|
||||
err := s.GetReplica().SelectOne(&serviceTerms, "SELECT * FROM ServiceTerms ORDER BY CreateAt DESC LIMIT 1")
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
result.Err = model.NewAppError("SqlServiceTermsStore.GetLatest", "store.sql_service_terms_store.get.no_rows.app_error", nil, "err="+err.Error(), http.StatusNotFound)
|
||||
} else {
|
||||
result.Err = model.NewAppError("SqlServiceTermsStore.GetLatest", "store.sql_service_terms_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
} else {
|
||||
result.Data = serviceTerms
|
||||
|
||||
if allowFromCache {
|
||||
serviceTermsCache.AddWithDefaultExpires(serviceTerms.Id, serviceTerms)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func (s SqlServiceTermsStore) Get(id string, allowFromCache bool) store.StoreChannel {
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
if allowFromCache {
|
||||
if serviceTermsCache.Len() == 0 {
|
||||
if s.metrics != nil {
|
||||
s.metrics.IncrementMemCacheMissCounter(serviceTermsCacheName)
|
||||
}
|
||||
} else {
|
||||
if cacheItem, ok := serviceTermsCache.Get(id); ok {
|
||||
if s.metrics != nil {
|
||||
s.metrics.IncrementMemCacheHitCounter(serviceTermsCacheName)
|
||||
}
|
||||
|
||||
result.Data = cacheItem.(*model.ServiceTerms)
|
||||
return
|
||||
} else if s.metrics != nil {
|
||||
s.metrics.IncrementMemCacheMissCounter(serviceTermsCacheName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if obj, err := s.GetReplica().Get(model.ServiceTerms{}, id); err != nil {
|
||||
result.Err = model.NewAppError("SqlServiceTermsStore.Get", "store.sql_service_terms_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError)
|
||||
} else if obj == nil {
|
||||
result.Err = model.NewAppError("SqlServiceTermsStore.GetLatest", "store.sql_service_terms_store.get.no_rows.app_error", nil, "", http.StatusNotFound)
|
||||
} else {
|
||||
result.Data = obj.(*model.ServiceTerms)
|
||||
}
|
||||
})
|
||||
}
|
||||
10
store/sqlstore/service_terms_store_test.go
Обычный файл
10
store/sqlstore/service_terms_store_test.go
Обычный файл
@@ -0,0 +1,10 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/store/storetest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestServiceTermsStore(t *testing.T) {
|
||||
StoreTest(t, storetest.TestServiceTermsStore)
|
||||
}
|
||||
@@ -93,4 +93,5 @@ type SqlStore interface {
|
||||
UserAccessToken() store.UserAccessTokenStore
|
||||
Role() store.RoleStore
|
||||
Scheme() store.SchemeStore
|
||||
ServiceTerms() store.ServiceTermsStore
|
||||
}
|
||||
|
||||
@@ -92,6 +92,7 @@ type SqlSupplierOldStores struct {
|
||||
channelMemberHistory store.ChannelMemberHistoryStore
|
||||
role store.RoleStore
|
||||
scheme store.SchemeStore
|
||||
serviceTerms store.ServiceTermsStore
|
||||
}
|
||||
|
||||
type SqlSupplier struct {
|
||||
@@ -145,6 +146,7 @@ func NewSqlSupplier(settings model.SqlSettings, metrics einterfaces.MetricsInter
|
||||
supplier.oldStores.userAccessToken = NewSqlUserAccessTokenStore(supplier)
|
||||
supplier.oldStores.channelMemberHistory = NewSqlChannelMemberHistoryStore(supplier)
|
||||
supplier.oldStores.plugin = NewSqlPluginStore(supplier)
|
||||
supplier.oldStores.serviceTerms = NewSqlTermStore(supplier, metrics)
|
||||
|
||||
initSqlSupplierReactions(supplier)
|
||||
initSqlSupplierRoles(supplier)
|
||||
@@ -180,6 +182,7 @@ func NewSqlSupplier(settings model.SqlSettings, metrics einterfaces.MetricsInter
|
||||
supplier.oldStores.job.(*SqlJobStore).CreateIndexesIfNotExists()
|
||||
supplier.oldStores.userAccessToken.(*SqlUserAccessTokenStore).CreateIndexesIfNotExists()
|
||||
supplier.oldStores.plugin.(*SqlPluginStore).CreateIndexesIfNotExists()
|
||||
supplier.oldStores.serviceTerms.(SqlServiceTermsStore).CreateIndexesIfNotExists()
|
||||
|
||||
supplier.oldStores.preference.(*SqlPreferenceStore).DeleteUnusedFeatures()
|
||||
|
||||
@@ -961,6 +964,10 @@ func (ss *SqlSupplier) Role() store.RoleStore {
|
||||
return ss.oldStores.role
|
||||
}
|
||||
|
||||
func (ss *SqlSupplier) ServiceTerms() store.ServiceTermsStore {
|
||||
return ss.oldStores.serviceTerms
|
||||
}
|
||||
|
||||
func (ss *SqlSupplier) Scheme() store.SchemeStore {
|
||||
return ss.oldStores.scheme
|
||||
}
|
||||
|
||||
@@ -496,11 +496,11 @@ func UpgradeDatabaseToVersion54(sqlStore SqlStore) {
|
||||
// if shouldPerformUpgrade(sqlStore, VERSION_5_3_0, VERSION_5_4_0) {
|
||||
sqlStore.AlterColumnTypeIfExists("OutgoingWebhooks", "Description", "varchar(500)", "varchar(500)")
|
||||
sqlStore.AlterColumnTypeIfExists("IncomingWebhooks", "Description", "varchar(500)", "varchar(500)")
|
||||
|
||||
if err := sqlStore.Channel().MigratePublicChannels(); err != nil {
|
||||
mlog.Critical("Failed to migrate PublicChannels table", mlog.Err(err))
|
||||
time.Sleep(time.Second)
|
||||
os.Exit(EXIT_GENERIC_FAILURE)
|
||||
}
|
||||
sqlStore.CreateColumnIfNotExists("Users", "AcceptedServiceTermsId", "varchar(64)", "varchar(64)", "")
|
||||
// saveSchemaVersion(sqlStore, VERSION_5_4_0)
|
||||
}
|
||||
|
||||
@@ -65,6 +65,7 @@ type Store interface {
|
||||
UserAccessToken() UserAccessTokenStore
|
||||
ChannelMemberHistory() ChannelMemberHistoryStore
|
||||
Plugin() PluginStore
|
||||
ServiceTerms() ServiceTermsStore
|
||||
MarkSystemRanUnitTests()
|
||||
Close()
|
||||
LockToMaster()
|
||||
@@ -518,3 +519,9 @@ type SchemeStore interface {
|
||||
Delete(schemeId string) StoreChannel
|
||||
PermanentDeleteAll() StoreChannel
|
||||
}
|
||||
|
||||
type ServiceTermsStore interface {
|
||||
Save(serviceTerms *model.ServiceTerms) StoreChannel
|
||||
GetLatest(allowFromCache bool) StoreChannel
|
||||
Get(id string, allowFromCache bool) StoreChannel
|
||||
}
|
||||
|
||||
@@ -729,6 +729,22 @@ func (_m *LayeredStoreDatabaseLayer) SchemeSave(ctx context.Context, scheme *mod
|
||||
return r0
|
||||
}
|
||||
|
||||
// ServiceTerms provides a mock function with given fields:
|
||||
func (_m *LayeredStoreDatabaseLayer) ServiceTerms() store.ServiceTermsStore {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 store.ServiceTermsStore
|
||||
if rf, ok := ret.Get(0).(func() store.ServiceTermsStore); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.ServiceTermsStore)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// Session provides a mock function with given fields:
|
||||
func (_m *LayeredStoreDatabaseLayer) Session() store.SessionStore {
|
||||
ret := _m.Called()
|
||||
|
||||
62
store/storetest/mocks/ServiceTermsStore.go
Обычный файл
62
store/storetest/mocks/ServiceTermsStore.go
Обычный файл
@@ -0,0 +1,62 @@
|
||||
// Code generated by mockery v1.0.0. DO NOT EDIT.
|
||||
|
||||
// Regenerate this file using `make store-mocks`.
|
||||
|
||||
package mocks
|
||||
|
||||
import mock "github.com/stretchr/testify/mock"
|
||||
import model "github.com/mattermost/mattermost-server/model"
|
||||
import store "github.com/mattermost/mattermost-server/store"
|
||||
|
||||
// ServiceTermsStore is an autogenerated mock type for the ServiceTermsStore type
|
||||
type ServiceTermsStore struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// Get provides a mock function with given fields: id, allowFromCache
|
||||
func (_m *ServiceTermsStore) Get(id string, allowFromCache bool) store.StoreChannel {
|
||||
ret := _m.Called(id, allowFromCache)
|
||||
|
||||
var r0 store.StoreChannel
|
||||
if rf, ok := ret.Get(0).(func(string, bool) store.StoreChannel); ok {
|
||||
r0 = rf(id, allowFromCache)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.StoreChannel)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// GetLatest provides a mock function with given fields: allowFromCache
|
||||
func (_m *ServiceTermsStore) GetLatest(allowFromCache bool) store.StoreChannel {
|
||||
ret := _m.Called(allowFromCache)
|
||||
|
||||
var r0 store.StoreChannel
|
||||
if rf, ok := ret.Get(0).(func(bool) store.StoreChannel); ok {
|
||||
r0 = rf(allowFromCache)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.StoreChannel)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// Save provides a mock function with given fields: serviceTerms
|
||||
func (_m *ServiceTermsStore) Save(serviceTerms *model.ServiceTerms) store.StoreChannel {
|
||||
ret := _m.Called(serviceTerms)
|
||||
|
||||
var r0 store.StoreChannel
|
||||
if rf, ok := ret.Get(0).(func(*model.ServiceTerms) store.StoreChannel); ok {
|
||||
r0 = rf(serviceTerms)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.StoreChannel)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
@@ -603,6 +603,22 @@ func (_m *SqlStore) Scheme() store.SchemeStore {
|
||||
return r0
|
||||
}
|
||||
|
||||
// ServiceTerms provides a mock function with given fields:
|
||||
func (_m *SqlStore) ServiceTerms() store.ServiceTermsStore {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 store.ServiceTermsStore
|
||||
if rf, ok := ret.Get(0).(func() store.ServiceTermsStore); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.ServiceTermsStore)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// Session provides a mock function with given fields:
|
||||
func (_m *SqlStore) Session() store.SessionStore {
|
||||
ret := _m.Called()
|
||||
|
||||
@@ -320,6 +320,22 @@ func (_m *Store) Scheme() store.SchemeStore {
|
||||
return r0
|
||||
}
|
||||
|
||||
// ServiceTerms provides a mock function with given fields:
|
||||
func (_m *Store) ServiceTerms() store.ServiceTermsStore {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 store.ServiceTermsStore
|
||||
if rf, ok := ret.Get(0).(func() store.ServiceTermsStore); ok {
|
||||
r0 = rf()
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(store.ServiceTermsStore)
|
||||
}
|
||||
}
|
||||
|
||||
return r0
|
||||
}
|
||||
|
||||
// Session provides a mock function with given fields:
|
||||
func (_m *Store) Session() store.SessionStore {
|
||||
ret := _m.Called()
|
||||
|
||||
82
store/storetest/service_terms_store.go
Обычный файл
82
store/storetest/service_terms_store.go
Обычный файл
@@ -0,0 +1,82 @@
|
||||
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package storetest
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/store"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestServiceTermsStore(t *testing.T, ss store.Store) {
|
||||
t.Run("TestSaveServiceTerms", func(t *testing.T) { testSaveServiceTerms(t, ss) })
|
||||
t.Run("TestGetLatestServiceTerms", func(t *testing.T) { testGetLatestServiceTerms(t, ss) })
|
||||
t.Run("TestGetServiceTerms", func(t *testing.T) { testGetServiceTerms(t, ss) })
|
||||
}
|
||||
|
||||
func testSaveServiceTerms(t *testing.T, ss store.Store) {
|
||||
u1 := model.User{}
|
||||
u1.Username = model.NewId()
|
||||
u1.Email = MakeEmail()
|
||||
u1.Nickname = model.NewId()
|
||||
store.Must(ss.User().Save(&u1))
|
||||
|
||||
serviceTerms := &model.ServiceTerms{Text: "service terms", UserId: u1.Id}
|
||||
r1 := <-ss.ServiceTerms().Save(serviceTerms)
|
||||
|
||||
if r1.Err != nil {
|
||||
t.Fatal(r1.Err)
|
||||
}
|
||||
|
||||
savedServiceTerms := r1.Data.(*model.ServiceTerms)
|
||||
if len(savedServiceTerms.Id) != 26 {
|
||||
t.Fatal("Id should have been populated")
|
||||
}
|
||||
|
||||
if savedServiceTerms.CreateAt == 0 {
|
||||
t.Fatal("Create at should have been populated")
|
||||
}
|
||||
}
|
||||
|
||||
func testGetLatestServiceTerms(t *testing.T, ss store.Store) {
|
||||
u1 := model.User{}
|
||||
u1.Username = model.NewId()
|
||||
u1.Email = MakeEmail()
|
||||
u1.Nickname = model.NewId()
|
||||
store.Must(ss.User().Save(&u1))
|
||||
|
||||
serviceTerms := &model.ServiceTerms{Text: "service terms", UserId: u1.Id}
|
||||
store.Must(ss.ServiceTerms().Save(serviceTerms))
|
||||
|
||||
r1 := <-ss.ServiceTerms().GetLatest(true)
|
||||
if r1.Err != nil {
|
||||
t.Fatal(r1.Err)
|
||||
}
|
||||
|
||||
fetchedServiceTerms := r1.Data.(*model.ServiceTerms)
|
||||
assert.Equal(t, serviceTerms.Text, fetchedServiceTerms.Text)
|
||||
assert.Equal(t, serviceTerms.UserId, fetchedServiceTerms.UserId)
|
||||
}
|
||||
|
||||
func testGetServiceTerms(t *testing.T, ss store.Store) {
|
||||
u1 := model.User{}
|
||||
u1.Username = model.NewId()
|
||||
u1.Email = MakeEmail()
|
||||
u1.Nickname = model.NewId()
|
||||
store.Must(ss.User().Save(&u1))
|
||||
|
||||
serviceTerms := &model.ServiceTerms{Text: "service terms", UserId: u1.Id}
|
||||
store.Must(ss.ServiceTerms().Save(serviceTerms))
|
||||
|
||||
r1 := <-ss.ServiceTerms().Get("an_invalid_id", true)
|
||||
assert.NotNil(t, r1.Err)
|
||||
assert.Nil(t, r1.Data)
|
||||
|
||||
r1 = <-ss.ServiceTerms().Get(serviceTerms.Id, true)
|
||||
assert.Nil(t, r1.Err)
|
||||
|
||||
receivedServiceTerms := r1.Data.(*model.ServiceTerms)
|
||||
assert.Equal(t, "service terms", receivedServiceTerms.Text)
|
||||
}
|
||||
@@ -45,6 +45,7 @@ type Store struct {
|
||||
ChannelMemberHistoryStore mocks.ChannelMemberHistoryStore
|
||||
RoleStore mocks.RoleStore
|
||||
SchemeStore mocks.SchemeStore
|
||||
ServiceTermsStore mocks.ServiceTermsStore
|
||||
}
|
||||
|
||||
func (s *Store) Team() store.TeamStore { return &s.TeamStore }
|
||||
@@ -72,6 +73,7 @@ func (s *Store) UserAccessToken() store.UserAccessTokenStore { return &s.UserA
|
||||
func (s *Store) Plugin() store.PluginStore { return &s.PluginStore }
|
||||
func (s *Store) Role() store.RoleStore { return &s.RoleStore }
|
||||
func (s *Store) Scheme() store.SchemeStore { return &s.SchemeStore }
|
||||
func (s *Store) ServiceTerms() store.ServiceTermsStore { return &s.ServiceTermsStore }
|
||||
func (s *Store) ChannelMemberHistory() store.ChannelMemberHistoryStore {
|
||||
return &s.ChannelMemberHistoryStore
|
||||
}
|
||||
|
||||
@@ -2181,6 +2181,7 @@ func testUserStoreGetAllAfter(t *testing.T, ss store.Store) {
|
||||
|
||||
found := false
|
||||
for _, u := range d1 {
|
||||
|
||||
if u.Id == u1.Id {
|
||||
found = true
|
||||
assert.Equal(t, u1.Id, u.Id)
|
||||
|
||||
@@ -39,6 +39,14 @@ var (
|
||||
"../..",
|
||||
"../../..",
|
||||
}
|
||||
|
||||
serviceTermsEnabledAndEmpty = model.NewAppError(
|
||||
"Config.IsValid",
|
||||
"model.config.is_valid.support.custom_service_terms_text.app_error",
|
||||
nil,
|
||||
"",
|
||||
http.StatusBadRequest,
|
||||
)
|
||||
)
|
||||
|
||||
func FindPath(path string, baseSearchPaths []string, filter func(os.FileInfo) bool) string {
|
||||
@@ -474,7 +482,10 @@ func LoadConfig(fileName string) (*model.Config, string, map[string]interface{},
|
||||
|
||||
config.SetDefaults()
|
||||
|
||||
if err := config.IsValid(); err != nil {
|
||||
// Don't treat it as an error right now if custom service terms are enabled but text is empty.
|
||||
// This is because service terms text will be fetched from database at a later state, but
|
||||
// the flag indicating it is enabled is fetched from config file right away.
|
||||
if err := config.IsValid(); err != nil && err.Id != serviceTermsEnabledAndEmpty.Id {
|
||||
return nil, "", nil, err
|
||||
}
|
||||
|
||||
@@ -690,6 +701,10 @@ func GenerateClientConfig(c *model.Config, diagnosticId string, license *model.L
|
||||
props["DataRetentionEnableFileDeletion"] = strconv.FormatBool(*c.DataRetentionSettings.EnableFileDeletion)
|
||||
props["DataRetentionFileRetentionDays"] = strconv.FormatInt(int64(*c.DataRetentionSettings.FileRetentionDays), 10)
|
||||
}
|
||||
|
||||
if *license.Features.CustomTermsOfService {
|
||||
props["EnableCustomServiceTerms"] = strconv.FormatBool(*c.SupportSettings.CustomServiceTermsEnabled)
|
||||
}
|
||||
}
|
||||
|
||||
return props
|
||||
|
||||
@@ -37,13 +37,13 @@ func StopTestStore() {
|
||||
}
|
||||
|
||||
type TestHelper struct {
|
||||
App *app.App
|
||||
App *app.App
|
||||
|
||||
BasicUser *model.User
|
||||
BasicChannel *model.Channel
|
||||
BasicTeam *model.Team
|
||||
BasicUser *model.User
|
||||
BasicChannel *model.Channel
|
||||
BasicTeam *model.Team
|
||||
|
||||
SystemAdminUser *model.User
|
||||
SystemAdminUser *model.User
|
||||
}
|
||||
|
||||
func Setup() *TestHelper {
|
||||
|
||||
Ссылка в новой задаче
Block a user