github.com/RajatVaryani/mattermost-server@v5.11.1+incompatible/api4/terms_of_service.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/mattermost/mattermost-server/app"
    10  	"github.com/mattermost/mattermost-server/model"
    11  )
    12  
    13  func (api *API) InitTermsOfService() {
    14  	api.BaseRoutes.TermsOfService.Handle("", api.ApiSessionRequired(getLatestTermsOfService)).Methods("GET")
    15  	api.BaseRoutes.TermsOfService.Handle("", api.ApiSessionRequired(createTermsOfService)).Methods("POST")
    16  }
    17  
    18  func getLatestTermsOfService(c *Context, w http.ResponseWriter, r *http.Request) {
    19  	termsOfService, err := c.App.GetLatestTermsOfService()
    20  	if err != nil {
    21  		c.Err = err
    22  		return
    23  	}
    24  
    25  	w.Write([]byte(termsOfService.ToJson()))
    26  }
    27  
    28  func createTermsOfService(c *Context, w http.ResponseWriter, r *http.Request) {
    29  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
    30  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    31  		return
    32  	}
    33  
    34  	if license := c.App.License(); license == nil || !*license.Features.CustomTermsOfService {
    35  		c.Err = model.NewAppError("createTermsOfService", "api.create_terms_of_service.custom_terms_of_service_disabled.app_error", nil, "", http.StatusBadRequest)
    36  		return
    37  	}
    38  
    39  	props := model.MapFromJson(r.Body)
    40  	text := props["text"]
    41  	userId := c.App.Session.UserId
    42  
    43  	if text == "" {
    44  		c.Err = model.NewAppError("Config.IsValid", "api.create_terms_of_service.empty_text.app_error", nil, "", http.StatusBadRequest)
    45  		return
    46  	}
    47  
    48  	oldTermsOfService, err := c.App.GetLatestTermsOfService()
    49  	if err != nil && err.Id != app.ERROR_TERMS_OF_SERVICE_NO_ROWS_FOUND {
    50  		c.Err = err
    51  		return
    52  	}
    53  
    54  	if oldTermsOfService == nil || oldTermsOfService.Text != text {
    55  		termsOfService, err := c.App.CreateTermsOfService(text, userId)
    56  		if err != nil {
    57  			c.Err = err
    58  			return
    59  		}
    60  
    61  		w.Write([]byte(termsOfService.ToJson()))
    62  	} else {
    63  		w.Write([]byte(oldTermsOfService.ToJson()))
    64  	}
    65  }