From 1d66e64e54c59ea9184bd614790498451d025e25 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Mon, 19 Jun 2017 13:46:51 -0400 Subject: Add POST and DELETE /license endpoints for v4 (#6665) * Add POST and DELETE /license endpoints for v4 * Fix comment text --- api4/system.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'api4/system.go') diff --git a/api4/system.go b/api4/system.go index 97d8bb7dc..92674419f 100644 --- a/api4/system.go +++ b/api4/system.go @@ -4,6 +4,8 @@ package api4 import ( + "bytes" + "io" "net/http" "runtime" "strconv" @@ -24,6 +26,8 @@ func InitSystem() { BaseRoutes.ApiRoot.Handle("/config/reload", ApiSessionRequired(configReload)).Methods("POST") BaseRoutes.ApiRoot.Handle("/config/client", ApiHandler(getClientConfig)).Methods("GET") + BaseRoutes.ApiRoot.Handle("/license", ApiSessionRequired(addLicense)).Methods("POST") + BaseRoutes.ApiRoot.Handle("/license", ApiSessionRequired(removeLicense)).Methods("DELETE") BaseRoutes.ApiRoot.Handle("/license/client", ApiHandler(getClientLicense)).Methods("GET") BaseRoutes.ApiRoot.Handle("/audits", ApiSessionRequired(getAudits)).Methods("GET") @@ -260,3 +264,75 @@ func getClientLicense(c *Context, w http.ResponseWriter, r *http.Request) { w.Header().Set(model.HEADER_ETAG_SERVER, etag) w.Write([]byte(model.MapToJson(clientLicense))) } + +func addLicense(c *Context, w http.ResponseWriter, r *http.Request) { + c.LogAudit("attempt") + + if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + err := r.ParseMultipartForm(*utils.Cfg.FileSettings.MaxFileSize) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + + m := r.MultipartForm + + fileArray, ok := m.File["license"] + if !ok { + c.Err = model.NewAppError("addLicense", "api.license.add_license.no_file.app_error", nil, "", http.StatusBadRequest) + return + } + + if len(fileArray) <= 0 { + c.Err = model.NewAppError("addLicense", "api.license.add_license.array.app_error", nil, "", http.StatusBadRequest) + return + } + + fileData := fileArray[0] + + file, err := fileData.Open() + defer file.Close() + if err != nil { + c.Err = model.NewAppError("addLicense", "api.license.add_license.open.app_error", nil, err.Error(), http.StatusBadRequest) + return + } + + buf := bytes.NewBuffer(nil) + io.Copy(buf, file) + + if license, err := app.SaveLicense(buf.Bytes()); err != nil { + if err.Id == model.EXPIRED_LICENSE_ERROR { + c.LogAudit("failed - expired or non-started license") + } else if err.Id == model.INVALID_LICENSE_ERROR { + c.LogAudit("failed - invalid license") + } else { + c.LogAudit("failed - unable to save license") + } + c.Err = err + return + } else { + c.LogAudit("success") + w.Write([]byte(license.ToJson())) + } +} + +func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) { + c.LogAudit("attempt") + + if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) { + c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM) + return + } + + if err := app.RemoveLicense(); err != nil { + c.Err = err + return + } + + c.LogAudit("success") + ReturnStatusOK(w) +} -- cgit v1.2.3-1-g7c22