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