github.com/cjdelisle/matterfoss@v5.11.1+incompatible/api4/license.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"net/http"
    10  
    11  	"github.com/mattermost/mattermost-server/model"
    12  )
    13  
    14  func (api *API) InitLicense() {
    15  	api.BaseRoutes.ApiRoot.Handle("/license", api.ApiSessionRequired(addLicense)).Methods("POST")
    16  	api.BaseRoutes.ApiRoot.Handle("/license", api.ApiSessionRequired(removeLicense)).Methods("DELETE")
    17  	api.BaseRoutes.ApiRoot.Handle("/license/client", api.ApiHandler(getClientLicense)).Methods("GET")
    18  }
    19  
    20  func getClientLicense(c *Context, w http.ResponseWriter, r *http.Request) {
    21  	format := r.URL.Query().Get("format")
    22  
    23  	if format == "" {
    24  		c.Err = model.NewAppError("getClientLicense", "api.license.client.old_format.app_error", nil, "", http.StatusNotImplemented)
    25  		return
    26  	}
    27  
    28  	if format != "old" {
    29  		c.SetInvalidParam("format")
    30  		return
    31  	}
    32  
    33  	etag := c.App.GetClientLicenseEtag(true)
    34  	if c.HandleEtag(etag, "Get Client License", w, r) {
    35  		return
    36  	}
    37  
    38  	var clientLicense map[string]string
    39  
    40  	if c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
    41  		clientLicense = c.App.ClientLicense()
    42  	} else {
    43  		clientLicense = c.App.GetSanitizedClientLicense()
    44  	}
    45  
    46  	w.Header().Set(model.HEADER_ETAG_SERVER, etag)
    47  	w.Write([]byte(model.MapToJson(clientLicense)))
    48  }
    49  
    50  func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
    51  	c.LogAudit("attempt")
    52  
    53  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
    54  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    55  		return
    56  	}
    57  
    58  	if *c.App.Config().ExperimentalSettings.RestrictSystemAdmin {
    59  		c.Err = model.NewAppError("addLicense", "api.restricted_system_admin", nil, "", http.StatusForbidden)
    60  		return
    61  	}
    62  
    63  	err := r.ParseMultipartForm(*c.App.Config().FileSettings.MaxFileSize)
    64  	if err != nil {
    65  		http.Error(w, err.Error(), http.StatusBadRequest)
    66  		return
    67  	}
    68  
    69  	m := r.MultipartForm
    70  
    71  	fileArray, ok := m.File["license"]
    72  	if !ok {
    73  		c.Err = model.NewAppError("addLicense", "api.license.add_license.no_file.app_error", nil, "", http.StatusBadRequest)
    74  		return
    75  	}
    76  
    77  	if len(fileArray) <= 0 {
    78  		c.Err = model.NewAppError("addLicense", "api.license.add_license.array.app_error", nil, "", http.StatusBadRequest)
    79  		return
    80  	}
    81  
    82  	fileData := fileArray[0]
    83  
    84  	file, err := fileData.Open()
    85  	if err != nil {
    86  		c.Err = model.NewAppError("addLicense", "api.license.add_license.open.app_error", nil, err.Error(), http.StatusBadRequest)
    87  		return
    88  	}
    89  	defer file.Close()
    90  
    91  	buf := bytes.NewBuffer(nil)
    92  	io.Copy(buf, file)
    93  
    94  	license, appErr := c.App.SaveLicense(buf.Bytes())
    95  	if appErr != nil {
    96  		if appErr.Id == model.EXPIRED_LICENSE_ERROR {
    97  			c.LogAudit("failed - expired or non-started license")
    98  		} else if appErr.Id == model.INVALID_LICENSE_ERROR {
    99  			c.LogAudit("failed - invalid license")
   100  		} else {
   101  			c.LogAudit("failed - unable to save license")
   102  		}
   103  		c.Err = appErr
   104  		return
   105  	}
   106  
   107  	c.LogAudit("success")
   108  	w.Write([]byte(license.ToJson()))
   109  }
   110  
   111  func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) {
   112  	c.LogAudit("attempt")
   113  
   114  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
   115  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   116  		return
   117  	}
   118  
   119  	if *c.App.Config().ExperimentalSettings.RestrictSystemAdmin {
   120  		c.Err = model.NewAppError("removeLicense", "api.restricted_system_admin", nil, "", http.StatusForbidden)
   121  		return
   122  	}
   123  
   124  	if err := c.App.RemoveLicense(); err != nil {
   125  		c.Err = err
   126  		return
   127  	}
   128  
   129  	c.LogAudit("success")
   130  	ReturnStatusOK(w)
   131  }