github.com/spline-fu/mattermost-server@v4.10.10+incompatible/api/license.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package api 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.License.Handle("/add", api.ApiAdminSystemRequired(addLicense)).Methods("POST") 16 api.BaseRoutes.License.Handle("/remove", api.ApiAdminSystemRequired(removeLicense)).Methods("POST") 17 api.BaseRoutes.License.Handle("/client_config", api.ApiAppHandler(getClientLicenceConfig)).Methods("GET") 18 } 19 20 func addLicense(c *Context, w http.ResponseWriter, r *http.Request) { 21 c.LogAudit("attempt") 22 err := r.ParseMultipartForm(*c.App.Config().FileSettings.MaxFileSize) 23 if err != nil { 24 http.Error(w, err.Error(), http.StatusInternalServerError) 25 return 26 } 27 28 m := r.MultipartForm 29 30 fileArray, ok := m.File["license"] 31 if !ok { 32 c.Err = model.NewAppError("addLicense", "api.license.add_license.no_file.app_error", nil, "", http.StatusBadRequest) 33 return 34 } 35 36 if len(fileArray) <= 0 { 37 c.Err = model.NewAppError("addLicense", "api.license.add_license.array.app_error", nil, "", http.StatusBadRequest) 38 return 39 } 40 41 fileData := fileArray[0] 42 43 file, err := fileData.Open() 44 if err != nil { 45 c.Err = model.NewAppError("addLicense", "api.license.add_license.open.app_error", nil, err.Error(), http.StatusInternalServerError) 46 return 47 } 48 defer file.Close() 49 50 buf := bytes.NewBuffer(nil) 51 io.Copy(buf, file) 52 53 if license, err := c.App.SaveLicense(buf.Bytes()); err != nil { 54 if err.Id == model.EXPIRED_LICENSE_ERROR { 55 c.LogAudit("failed - expired or non-started license") 56 } else if err.Id == model.INVALID_LICENSE_ERROR { 57 c.LogAudit("failed - invalid license") 58 } else { 59 c.LogAudit("failed - unable to save license") 60 } 61 c.Err = err 62 return 63 } else { 64 c.LogAudit("success") 65 w.Write([]byte(license.ToJson())) 66 } 67 } 68 69 func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) { 70 c.LogAudit("") 71 72 if err := c.App.RemoveLicense(); err != nil { 73 c.Err = err 74 return 75 } 76 77 rdata := map[string]string{} 78 rdata["status"] = "ok" 79 w.Write([]byte(model.MapToJson(rdata))) 80 } 81 82 func getClientLicenceConfig(c *Context, w http.ResponseWriter, r *http.Request) { 83 useSanitizedLicense := !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) 84 85 etag := c.App.GetClientLicenseEtag(useSanitizedLicense) 86 if c.HandleEtag(etag, "Get Client License Config", w, r) { 87 return 88 } 89 90 var clientLicense map[string]string 91 92 if useSanitizedLicense { 93 clientLicense = c.App.ClientLicense() 94 } else { 95 clientLicense = c.App.GetSanitizedClientLicense() 96 } 97 98 w.Header().Set(model.HEADER_ETAG_SERVER, etag) 99 w.Write([]byte(model.MapToJson(clientLicense))) 100 }