diff options
author | Joram Wilander <jwawilander@gmail.com> | 2016-04-21 09:43:10 -0400 |
---|---|---|
committer | Harrison Healey <harrisonmhealey@gmail.com> | 2016-04-21 09:43:10 -0400 |
commit | cf1f3ba322ce64ca383ce1a5f0ca3d9bacb180ea (patch) | |
tree | 2581e852a605d7d8e71ebcd7696cd60c8d767153 /api | |
parent | 94d5a793426efe7474957a20e887af849401caa0 (diff) | |
download | chat-cf1f3ba322ce64ca383ce1a5f0ca3d9bacb180ea.tar.gz chat-cf1f3ba322ce64ca383ce1a5f0ca3d9bacb180ea.tar.bz2 chat-cf1f3ba322ce64ca383ce1a5f0ca3d9bacb180ea.zip |
PLT-2561 Add commandline option to upload license file (#2757)
* Add commandline option to upload license file
* Remove unnecessary comment
Diffstat (limited to 'api')
-rw-r--r-- | api/license.go | 75 |
1 files changed, 48 insertions, 27 deletions
diff --git a/api/license.go b/api/license.go index 526f4a4c1..4bf8cd3b8 100644 --- a/api/license.go +++ b/api/license.go @@ -14,6 +14,11 @@ import ( "strings" ) +const ( + EXPIRED_LICENSE_ERROR = "api.license.add_license.expired.app_error" + INVALID_LICENSE_ERROR = "api.license.add_license.invalid.app_error" +) + func InitLicense(r *mux.Router) { l4g.Debug(utils.T("api.license.init.debug")) @@ -78,33 +83,45 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) { buf := bytes.NewBuffer(nil) io.Copy(buf, file) - data := buf.Bytes() + if license, err := SaveLicense(buf.Bytes()); err != nil { + if err.Id == EXPIRED_LICENSE_ERROR { + c.LogAudit("failed - expired or non-started license") + } else if err.Id == 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 SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) { var license *model.License - if success, licenseStr := utils.ValidateLicense(data); success { + + if success, licenseStr := utils.ValidateLicense(licenseBytes); success { license = model.LicenseFromJson(strings.NewReader(licenseStr)) if result := <-Srv.Store.User().AnalyticsUniqueUserCount(""); result.Err != nil { - c.Err = model.NewLocAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, result.Err.Error()) - return + return nil, model.NewLocAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, result.Err.Error()) } else { uniqueUserCount := result.Data.(int64) if uniqueUserCount > int64(*license.Features.Users) { - c.Err = model.NewLocAppError("addLicense", "api.license.add_license.unique_users.app_error", map[string]interface{}{"Users": *license.Features.Users, "Count": uniqueUserCount}, "") - return + return nil, model.NewLocAppError("addLicense", "api.license.add_license.unique_users.app_error", map[string]interface{}{"Users": *license.Features.Users, "Count": uniqueUserCount}, "") } } if ok := utils.SetLicense(license); !ok { - c.LogAudit("failed - expired or non-started license") - c.Err = model.NewLocAppError("addLicense", "api.license.add_license.expired.app_error", nil, "") - return + return nil, model.NewLocAppError("addLicense", EXPIRED_LICENSE_ERROR, nil, "") } record := &model.LicenseRecord{} record.Id = license.Id - record.Bytes = string(data) + record.Bytes = string(licenseBytes) rchan := Srv.Store.License().Save(record) sysVar := &model.System{} @@ -113,43 +130,47 @@ func addLicense(c *Context, w http.ResponseWriter, r *http.Request) { schan := Srv.Store.System().SaveOrUpdate(sysVar) if result := <-rchan; result.Err != nil { - c.Err = model.NewLocAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+result.Err.Error()) - utils.RemoveLicense() - return + RemoveLicense() + return nil, model.NewLocAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+result.Err.Error()) } if result := <-schan; result.Err != nil { - c.Err = model.NewLocAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "") - utils.RemoveLicense() - return + RemoveLicense() + return nil, model.NewLocAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "") } } else { - c.LogAudit("failed - invalid license") - c.Err = model.NewLocAppError("addLicense", "api.license.add_license.invalid.app_error", nil, "") - return + return nil, model.NewLocAppError("addLicense", INVALID_LICENSE_ERROR, nil, "") } - c.LogAudit("success") - w.Write([]byte(license.ToJson())) + return license, nil } func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) { c.LogAudit("") + if err := RemoveLicense(); err != nil { + c.Err = err + return + } + + rdata := map[string]string{} + rdata["status"] = "ok" + w.Write([]byte(model.MapToJson(rdata))) +} + +func RemoveLicense() *model.AppError { utils.RemoveLicense() sysVar := &model.System{} sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID sysVar.Value = "" - if result := <-Srv.Store.System().Update(sysVar); result.Err != nil { - c.Err = model.NewLocAppError("removeLicense", "api.license.remove_license.update.app_error", nil, "") - return + if result := <-Srv.Store.System().SaveOrUpdate(sysVar); result.Err != nil { + utils.RemoveLicense() + return result.Err } - rdata := map[string]string{} - rdata["status"] = "ok" - w.Write([]byte(model.MapToJson(rdata))) + return nil } func getClientLicenceConfig(c *Context, w http.ResponseWriter, r *http.Request) { |