github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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/vnforks/kid/v5/app"
    10  	"github.com/vnforks/kid/v5/audit"
    11  	"github.com/vnforks/kid/v5/model"
    12  )
    13  
    14  func (api *API) InitTermsOfService() {
    15  	api.BaseRoutes.TermsOfService.Handle("", api.ApiSessionRequired(getLatestTermsOfService)).Methods("GET")
    16  	api.BaseRoutes.TermsOfService.Handle("", api.ApiSessionRequired(createTermsOfService)).Methods("POST")
    17  }
    18  
    19  func getLatestTermsOfService(c *Context, w http.ResponseWriter, r *http.Request) {
    20  	termsOfService, err := c.App.GetLatestTermsOfService()
    21  	if err != nil {
    22  		c.Err = err
    23  		return
    24  	}
    25  
    26  	w.Write([]byte(termsOfService.ToJson()))
    27  }
    28  
    29  func createTermsOfService(c *Context, w http.ResponseWriter, r *http.Request) {
    30  	if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
    31  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    32  		return
    33  	}
    34  
    35  	if license := c.App.License(); license == nil || !*license.Features.CustomTermsOfService {
    36  		c.Err = model.NewAppError("createTermsOfService", "api.create_terms_of_service.custom_terms_of_service_disabled.app_error", nil, "", http.StatusBadRequest)
    37  		return
    38  	}
    39  
    40  	auditRec := c.MakeAuditRecord("createTermsOfService", audit.Fail)
    41  	defer c.LogAuditRec(auditRec)
    42  
    43  	props := model.MapFromJson(r.Body)
    44  	text := props["text"]
    45  	userId := c.App.Session().UserId
    46  
    47  	if text == "" {
    48  		c.Err = model.NewAppError("Config.IsValid", "api.create_terms_of_service.empty_text.app_error", nil, "", http.StatusBadRequest)
    49  		return
    50  	}
    51  
    52  	oldTermsOfService, err := c.App.GetLatestTermsOfService()
    53  	if err != nil && err.Id != app.ERROR_TERMS_OF_SERVICE_NO_ROWS_FOUND {
    54  		c.Err = err
    55  		return
    56  	}
    57  
    58  	if oldTermsOfService == nil || oldTermsOfService.Text != text {
    59  		termsOfService, err := c.App.CreateTermsOfService(text, userId)
    60  		if err != nil {
    61  			c.Err = err
    62  			return
    63  		}
    64  
    65  		w.Write([]byte(termsOfService.ToJson()))
    66  	} else {
    67  		w.Write([]byte(oldTermsOfService.ToJson()))
    68  	}
    69  	auditRec.Success()
    70  }