mattermost/server/channels/api4/license.go

293 lines
9.5 KiB
Go
Raw Permalink Normal View History

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"bytes"
"encoding/json"
"io"
MM-63728: Add license load metric endpoint and UI indicator (#30700) * Add license load metric endpoint and UI indicator Adds an API endpoint to calculate and return license usage as a load metric, and displays this metric in the About dialog. The metric is calculated as (MAU/licensed users)*100. Additionally: - Renamed function to be consistent with API endpoint name - Added proper i18n strings for error messages and UI elements * Fix TypeScript null check in about_build_modal.tsx * MM-63728: Update OpenAPI documentation for license load metric Update the OpenAPI documentation and code comments to correctly describe the license load metric calculation as using a multiplier of 1000 instead of percentage. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * MM-63728: Use float for license load metric calculation Modify the license load metric calculation to use floats throughout the computation process while still returning an integer result. This maintains the existing API but improves the precision of the calculation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * improve tests manually * Update server/channels/api4/license_test.go Co-authored-by: Doug Lauder <wiggin77@warpmail.net> * Update server/channels/api4/license_test.go Co-authored-by: Doug Lauder <wiggin77@warpmail.net> --------- Co-authored-by: Mattermost Build <build@mattermost.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
2025-04-17 16:29:46 -04:00
"math"
"net/http"
"github.com/mattermost/mattermost/server/public/shared/mlog"
"github.com/mattermost/mattermost/server/v8/channels/utils"
MM-34437 Preventing infinite trial requests (#17472) * MM-34434: Added 'is_trial' boolean to all trial license requests and to the License struct. * MM-34434: Generalized the concept of a license request. * MM-34434: Verifies JSON field of license instance is set. * MM-34434: Added missing client param. * MM-34434: Added some tests of the request trial API endpoint. * MM-34434: Removed comment. * fix broken test (#17348) * Add missing wrapped errors (#17339) * Improve document extraction and including a document extraction command (#17183) * Add extract documents content command * Adding the extraction command and making the pure go pdf library as secondary option * Improving the memory usage and docextractor interface * Enable content extraction by default in all the instances * Tiny improvement on archive indexing * Adding App interface generation and the opentracing layer * Fixing linter errors * Addressing PR review comments * Addressing PR review comments * Update en.json (#17356) Automatic Merge * adding new feature flag (#17308) Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Bump no_output_timeout to 2 hours (#17358) * log invalid username (#17345) Automatic Merge * MM-34434: Added missing client param. MM-34434: Added some tests of the request trial API endpoint. MM-34434: Removed comment. * MM-34434: Switched to a hard-coded true value. * MM-34434: Reverts test change. * MM-34434: Removes unnecessary field. * MM-34434: Tests that is_trial is hard-coded by TrialLicenseRequest. * MM-34434: Removed accidental commit. * MM-34434: Removes unnecessary is_trial key from JSON payload. * MM-34434: Reverts to old pointer receiver variable name. * MM-34434: Removes test. * #MM-34437 Initialized license service * ##MM-34437 Verified at all points if server is trial elligible * WIp * #MM-34437 removed unused commented code * MM-34437 make a log less severe * #MM-34437 generated einterface mocks * #MM-34437 added license on new file * #MM-34437 removed unused translation * #MM-34437 some refactoring * Update api4/license.go * Update api4/license.go * #MM-34437 made a variable name consistent * #MM-34437 Added mocks for lince validator * #M--34437 Added license validator test framework * #MM-34437 Renamed isTrial method to isTrialLicense to avoid conflict with newlya dded field * #M--34437 Allowed sales-sanctioned trials * #MM-34437 fixed trial license API tests * Added tests for add license API * #MM-34437 fixed ValidateLicense test * #MM-34437 Added util tests * #MM-34437 using NoError for checking no error * #MM-34437 using NoError for checking no error * Added dummy piblic key for testing * Fixed tests * #MM-34437 udpaetd trial license URL for testing * #MM-34437 adjusted times for licences generated through admin portal * Reverted test-only changes Co-authored-by: Martin Kraft <martin@upspin.org> Co-authored-by: Hossein <hahmadia@users.noreply.github.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> Co-authored-by: Amy Blais <amy_blais@hotmail.com> Co-authored-by: Ben Cooke <benkcooke@gmail.com> Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
2021-06-17 08:07:34 -04:00
"github.com/mattermost/mattermost/server/public/model"
MM-63728: Add license load metric endpoint and UI indicator (#30700) * Add license load metric endpoint and UI indicator Adds an API endpoint to calculate and return license usage as a load metric, and displays this metric in the About dialog. The metric is calculated as (MAU/licensed users)*100. Additionally: - Renamed function to be consistent with API endpoint name - Added proper i18n strings for error messages and UI elements * Fix TypeScript null check in about_build_modal.tsx * MM-63728: Update OpenAPI documentation for license load metric Update the OpenAPI documentation and code comments to correctly describe the license load metric calculation as using a multiplier of 1000 instead of percentage. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * MM-63728: Use float for license load metric calculation Modify the license load metric calculation to use floats throughout the computation process while still returning an integer result. This maintains the existing API but improves the precision of the calculation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * improve tests manually * Update server/channels/api4/license_test.go Co-authored-by: Doug Lauder <wiggin77@warpmail.net> * Update server/channels/api4/license_test.go Co-authored-by: Doug Lauder <wiggin77@warpmail.net> --------- Co-authored-by: Mattermost Build <build@mattermost.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
2025-04-17 16:29:46 -04:00
"github.com/mattermost/mattermost/server/v8/channels/app"
)
func (api *API) InitLicense() {
api.BaseRoutes.APIRoot.Handle("/trial-license", api.APISessionRequired(requestTrialLicense)).Methods(http.MethodPost)
api.BaseRoutes.APIRoot.Handle("/trial-license/prev", api.APISessionRequired(getPrevTrialLicense)).Methods(http.MethodGet)
api.BaseRoutes.APIRoot.Handle("/license", api.APISessionRequired(addLicense, handlerParamFileAPI)).Methods(http.MethodPost)
api.BaseRoutes.APIRoot.Handle("/license", api.APISessionRequired(removeLicense)).Methods(http.MethodDelete)
api.BaseRoutes.APIRoot.Handle("/license/client", api.APIHandler(getClientLicense)).Methods(http.MethodGet)
MM-63728: Add license load metric endpoint and UI indicator (#30700) * Add license load metric endpoint and UI indicator Adds an API endpoint to calculate and return license usage as a load metric, and displays this metric in the About dialog. The metric is calculated as (MAU/licensed users)*100. Additionally: - Renamed function to be consistent with API endpoint name - Added proper i18n strings for error messages and UI elements * Fix TypeScript null check in about_build_modal.tsx * MM-63728: Update OpenAPI documentation for license load metric Update the OpenAPI documentation and code comments to correctly describe the license load metric calculation as using a multiplier of 1000 instead of percentage. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * MM-63728: Use float for license load metric calculation Modify the license load metric calculation to use floats throughout the computation process while still returning an integer result. This maintains the existing API but improves the precision of the calculation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * improve tests manually * Update server/channels/api4/license_test.go Co-authored-by: Doug Lauder <wiggin77@warpmail.net> * Update server/channels/api4/license_test.go Co-authored-by: Doug Lauder <wiggin77@warpmail.net> --------- Co-authored-by: Mattermost Build <build@mattermost.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
2025-04-17 16:29:46 -04:00
api.BaseRoutes.APIRoot.Handle("/license/load_metric", api.APISessionRequired(getLicenseLoadMetric)).Methods(http.MethodGet)
}
func getClientLicense(c *Context, w http.ResponseWriter, r *http.Request) {
format := r.URL.Query().Get("format")
if format == "" {
c.Err = model.NewAppError("getClientLicense", "api.license.client.old_format.app_error", nil, "", http.StatusBadRequest)
return
}
if format != "old" {
c.SetInvalidParam("format")
return
}
var clientLicense map[string]string
2021-07-12 14:05:36 -04:00
if c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionReadLicenseInformation) {
clientLicense = c.App.Srv().ClientLicense()
} else {
clientLicense = c.App.Srv().GetSanitizedClientLicense()
}
if _, err := w.Write([]byte(model.MapToJSON(clientLicense))); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
}
func addLicense(c *Context, w http.ResponseWriter, r *http.Request) {
2025-07-16 00:47:03 -04:00
auditRec := c.MakeAuditRecord(model.AuditEventAddLicense, model.AuditStatusFail)
defer c.LogAuditRec(auditRec)
c.LogAudit("attempt")
if !c.App.SessionHasPermissionToAndNotRestrictedAdmin(*c.AppContext.Session(), model.PermissionManageLicenseInformation) {
2021-07-12 14:05:36 -04:00
c.SetPermissionError(model.PermissionManageLicenseInformation)
return
}
err := r.ParseMultipartForm(*c.App.Config().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]
model.AddEventParameterToAuditRec(auditRec, "filename", fileData.Filename)
file, err := fileData.Open()
if err != nil {
c.Err = model.NewAppError("addLicense", "api.license.add_license.open.app_error", nil, "", http.StatusBadRequest).Wrap(err)
return
}
defer file.Close()
buf := bytes.NewBuffer(nil)
if _, err := io.Copy(buf, file); err != nil {
c.Err = model.NewAppError("addLicense", "api.license.add_license.copy.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
return
}
MM-34437 Preventing infinite trial requests (#17472) * MM-34434: Added 'is_trial' boolean to all trial license requests and to the License struct. * MM-34434: Generalized the concept of a license request. * MM-34434: Verifies JSON field of license instance is set. * MM-34434: Added missing client param. * MM-34434: Added some tests of the request trial API endpoint. * MM-34434: Removed comment. * fix broken test (#17348) * Add missing wrapped errors (#17339) * Improve document extraction and including a document extraction command (#17183) * Add extract documents content command * Adding the extraction command and making the pure go pdf library as secondary option * Improving the memory usage and docextractor interface * Enable content extraction by default in all the instances * Tiny improvement on archive indexing * Adding App interface generation and the opentracing layer * Fixing linter errors * Addressing PR review comments * Addressing PR review comments * Update en.json (#17356) Automatic Merge * adding new feature flag (#17308) Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Bump no_output_timeout to 2 hours (#17358) * log invalid username (#17345) Automatic Merge * MM-34434: Added missing client param. MM-34434: Added some tests of the request trial API endpoint. MM-34434: Removed comment. * MM-34434: Switched to a hard-coded true value. * MM-34434: Reverts test change. * MM-34434: Removes unnecessary field. * MM-34434: Tests that is_trial is hard-coded by TrialLicenseRequest. * MM-34434: Removed accidental commit. * MM-34434: Removes unnecessary is_trial key from JSON payload. * MM-34434: Reverts to old pointer receiver variable name. * MM-34434: Removes test. * #MM-34437 Initialized license service * ##MM-34437 Verified at all points if server is trial elligible * WIp * #MM-34437 removed unused commented code * MM-34437 make a log less severe * #MM-34437 generated einterface mocks * #MM-34437 added license on new file * #MM-34437 removed unused translation * #MM-34437 some refactoring * Update api4/license.go * Update api4/license.go * #MM-34437 made a variable name consistent * #MM-34437 Added mocks for lince validator * #M--34437 Added license validator test framework * #MM-34437 Renamed isTrial method to isTrialLicense to avoid conflict with newlya dded field * #M--34437 Allowed sales-sanctioned trials * #MM-34437 fixed trial license API tests * Added tests for add license API * #MM-34437 fixed ValidateLicense test * #MM-34437 Added util tests * #MM-34437 using NoError for checking no error * #MM-34437 using NoError for checking no error * Added dummy piblic key for testing * Fixed tests * #MM-34437 udpaetd trial license URL for testing * #MM-34437 adjusted times for licences generated through admin portal * Reverted test-only changes Co-authored-by: Martin Kraft <martin@upspin.org> Co-authored-by: Hossein <hahmadia@users.noreply.github.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> Co-authored-by: Amy Blais <amy_blais@hotmail.com> Co-authored-by: Ben Cooke <benkcooke@gmail.com> Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
2021-06-17 08:07:34 -04:00
licenseBytes := buf.Bytes()
license, appErr := utils.LicenseValidator.LicenseFromBytes(licenseBytes)
if appErr != nil {
c.Err = appErr
return
}
// skip the restrictions if license is a sanctioned trial
if !license.IsSanctionedTrial() && license.IsTrialLicense() {
lm := c.App.Srv().Platform().LicenseManager()
if lm == nil {
c.Err = model.NewAppError("addLicense", "api.license.upgrade_needed.app_error", nil, "", http.StatusInternalServerError)
return
}
canStartTrialLicense, err := lm.CanStartTrial()
MM-34437 Preventing infinite trial requests (#17472) * MM-34434: Added 'is_trial' boolean to all trial license requests and to the License struct. * MM-34434: Generalized the concept of a license request. * MM-34434: Verifies JSON field of license instance is set. * MM-34434: Added missing client param. * MM-34434: Added some tests of the request trial API endpoint. * MM-34434: Removed comment. * fix broken test (#17348) * Add missing wrapped errors (#17339) * Improve document extraction and including a document extraction command (#17183) * Add extract documents content command * Adding the extraction command and making the pure go pdf library as secondary option * Improving the memory usage and docextractor interface * Enable content extraction by default in all the instances * Tiny improvement on archive indexing * Adding App interface generation and the opentracing layer * Fixing linter errors * Addressing PR review comments * Addressing PR review comments * Update en.json (#17356) Automatic Merge * adding new feature flag (#17308) Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Bump no_output_timeout to 2 hours (#17358) * log invalid username (#17345) Automatic Merge * MM-34434: Added missing client param. MM-34434: Added some tests of the request trial API endpoint. MM-34434: Removed comment. * MM-34434: Switched to a hard-coded true value. * MM-34434: Reverts test change. * MM-34434: Removes unnecessary field. * MM-34434: Tests that is_trial is hard-coded by TrialLicenseRequest. * MM-34434: Removed accidental commit. * MM-34434: Removes unnecessary is_trial key from JSON payload. * MM-34434: Reverts to old pointer receiver variable name. * MM-34434: Removes test. * #MM-34437 Initialized license service * ##MM-34437 Verified at all points if server is trial elligible * WIp * #MM-34437 removed unused commented code * MM-34437 make a log less severe * #MM-34437 generated einterface mocks * #MM-34437 added license on new file * #MM-34437 removed unused translation * #MM-34437 some refactoring * Update api4/license.go * Update api4/license.go * #MM-34437 made a variable name consistent * #MM-34437 Added mocks for lince validator * #M--34437 Added license validator test framework * #MM-34437 Renamed isTrial method to isTrialLicense to avoid conflict with newlya dded field * #M--34437 Allowed sales-sanctioned trials * #MM-34437 fixed trial license API tests * Added tests for add license API * #MM-34437 fixed ValidateLicense test * #MM-34437 Added util tests * #MM-34437 using NoError for checking no error * #MM-34437 using NoError for checking no error * Added dummy piblic key for testing * Fixed tests * #MM-34437 udpaetd trial license URL for testing * #MM-34437 adjusted times for licences generated through admin portal * Reverted test-only changes Co-authored-by: Martin Kraft <martin@upspin.org> Co-authored-by: Hossein <hahmadia@users.noreply.github.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> Co-authored-by: Amy Blais <amy_blais@hotmail.com> Co-authored-by: Ben Cooke <benkcooke@gmail.com> Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
2021-06-17 08:07:34 -04:00
if err != nil {
c.Err = model.NewAppError("addLicense", "api.license.add_license.open.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
MM-34437 Preventing infinite trial requests (#17472) * MM-34434: Added 'is_trial' boolean to all trial license requests and to the License struct. * MM-34434: Generalized the concept of a license request. * MM-34434: Verifies JSON field of license instance is set. * MM-34434: Added missing client param. * MM-34434: Added some tests of the request trial API endpoint. * MM-34434: Removed comment. * fix broken test (#17348) * Add missing wrapped errors (#17339) * Improve document extraction and including a document extraction command (#17183) * Add extract documents content command * Adding the extraction command and making the pure go pdf library as secondary option * Improving the memory usage and docextractor interface * Enable content extraction by default in all the instances * Tiny improvement on archive indexing * Adding App interface generation and the opentracing layer * Fixing linter errors * Addressing PR review comments * Addressing PR review comments * Update en.json (#17356) Automatic Merge * adding new feature flag (#17308) Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Bump no_output_timeout to 2 hours (#17358) * log invalid username (#17345) Automatic Merge * MM-34434: Added missing client param. MM-34434: Added some tests of the request trial API endpoint. MM-34434: Removed comment. * MM-34434: Switched to a hard-coded true value. * MM-34434: Reverts test change. * MM-34434: Removes unnecessary field. * MM-34434: Tests that is_trial is hard-coded by TrialLicenseRequest. * MM-34434: Removed accidental commit. * MM-34434: Removes unnecessary is_trial key from JSON payload. * MM-34434: Reverts to old pointer receiver variable name. * MM-34434: Removes test. * #MM-34437 Initialized license service * ##MM-34437 Verified at all points if server is trial elligible * WIp * #MM-34437 removed unused commented code * MM-34437 make a log less severe * #MM-34437 generated einterface mocks * #MM-34437 added license on new file * #MM-34437 removed unused translation * #MM-34437 some refactoring * Update api4/license.go * Update api4/license.go * #MM-34437 made a variable name consistent * #MM-34437 Added mocks for lince validator * #M--34437 Added license validator test framework * #MM-34437 Renamed isTrial method to isTrialLicense to avoid conflict with newlya dded field * #M--34437 Allowed sales-sanctioned trials * #MM-34437 fixed trial license API tests * Added tests for add license API * #MM-34437 fixed ValidateLicense test * #MM-34437 Added util tests * #MM-34437 using NoError for checking no error * #MM-34437 using NoError for checking no error * Added dummy piblic key for testing * Fixed tests * #MM-34437 udpaetd trial license URL for testing * #MM-34437 adjusted times for licences generated through admin portal * Reverted test-only changes Co-authored-by: Martin Kraft <martin@upspin.org> Co-authored-by: Hossein <hahmadia@users.noreply.github.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> Co-authored-by: Amy Blais <amy_blais@hotmail.com> Co-authored-by: Ben Cooke <benkcooke@gmail.com> Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
2021-06-17 08:07:34 -04:00
return
}
if !canStartTrialLicense {
c.Err = model.NewAppError("addLicense", "api.license.request-trial.can-start-trial.not-allowed", nil, "", http.StatusBadRequest)
return
}
}
license, appErr = c.App.Srv().SaveLicense(licenseBytes)
if appErr != nil {
2021-07-12 14:05:36 -04:00
if appErr.Id == model.ExpiredLicenseError {
c.LogAudit("failed - expired or non-started license")
2021-07-12 14:05:36 -04:00
} else if appErr.Id == model.InvalidLicenseError {
c.LogAudit("failed - invalid license")
} else {
c.LogAudit("failed - unable to save license")
}
c.Err = appErr
return
}
if c.App.Channels().License().IsCloud() {
// If cloud, invalidate the caches when a new license is loaded
defer func() {
if err := c.App.Srv().Cloud.HandleLicenseChange(); err != nil {
c.Logger.Warn("Error while handling license change", mlog.Err(err))
}
}()
}
auditRec.Success()
c.LogAudit("success")
if err := json.NewEncoder(w).Encode(license); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
}
func removeLicense(c *Context, w http.ResponseWriter, r *http.Request) {
2025-07-16 00:47:03 -04:00
auditRec := c.MakeAuditRecord(model.AuditEventRemoveLicense, model.AuditStatusFail)
defer c.LogAuditRec(auditRec)
c.LogAudit("attempt")
if !c.App.SessionHasPermissionToAndNotRestrictedAdmin(*c.AppContext.Session(), model.PermissionManageLicenseInformation) {
2021-07-12 14:05:36 -04:00
c.SetPermissionError(model.PermissionManageLicenseInformation)
return
}
if err := c.App.Srv().RemoveLicense(); err != nil {
c.Err = err
return
}
auditRec.Success()
c.LogAudit("success")
ReturnStatusOK(w)
}
func requestTrialLicense(c *Context, w http.ResponseWriter, r *http.Request) {
2025-07-16 00:47:03 -04:00
auditRec := c.MakeAuditRecord(model.AuditEventRequestTrialLicense, model.AuditStatusFail)
defer c.LogAuditRec(auditRec)
c.LogAudit("attempt")
if !c.App.SessionHasPermissionToAndNotRestrictedAdmin(*c.AppContext.Session(), model.PermissionManageLicenseInformation) {
2021-07-12 14:05:36 -04:00
c.SetPermissionError(model.PermissionManageLicenseInformation)
return
}
if c.App.Srv().Platform().LicenseManager() == nil {
c.Err = model.NewAppError("requestTrialLicense", "api.license.upgrade_needed.app_error", nil, "", http.StatusForbidden)
return
}
canStartTrialLicense, err := c.App.Srv().Platform().LicenseManager().CanStartTrial()
MM-34437 Preventing infinite trial requests (#17472) * MM-34434: Added 'is_trial' boolean to all trial license requests and to the License struct. * MM-34434: Generalized the concept of a license request. * MM-34434: Verifies JSON field of license instance is set. * MM-34434: Added missing client param. * MM-34434: Added some tests of the request trial API endpoint. * MM-34434: Removed comment. * fix broken test (#17348) * Add missing wrapped errors (#17339) * Improve document extraction and including a document extraction command (#17183) * Add extract documents content command * Adding the extraction command and making the pure go pdf library as secondary option * Improving the memory usage and docextractor interface * Enable content extraction by default in all the instances * Tiny improvement on archive indexing * Adding App interface generation and the opentracing layer * Fixing linter errors * Addressing PR review comments * Addressing PR review comments * Update en.json (#17356) Automatic Merge * adding new feature flag (#17308) Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Bump no_output_timeout to 2 hours (#17358) * log invalid username (#17345) Automatic Merge * MM-34434: Added missing client param. MM-34434: Added some tests of the request trial API endpoint. MM-34434: Removed comment. * MM-34434: Switched to a hard-coded true value. * MM-34434: Reverts test change. * MM-34434: Removes unnecessary field. * MM-34434: Tests that is_trial is hard-coded by TrialLicenseRequest. * MM-34434: Removed accidental commit. * MM-34434: Removes unnecessary is_trial key from JSON payload. * MM-34434: Reverts to old pointer receiver variable name. * MM-34434: Removes test. * #MM-34437 Initialized license service * ##MM-34437 Verified at all points if server is trial elligible * WIp * #MM-34437 removed unused commented code * MM-34437 make a log less severe * #MM-34437 generated einterface mocks * #MM-34437 added license on new file * #MM-34437 removed unused translation * #MM-34437 some refactoring * Update api4/license.go * Update api4/license.go * #MM-34437 made a variable name consistent * #MM-34437 Added mocks for lince validator * #M--34437 Added license validator test framework * #MM-34437 Renamed isTrial method to isTrialLicense to avoid conflict with newlya dded field * #M--34437 Allowed sales-sanctioned trials * #MM-34437 fixed trial license API tests * Added tests for add license API * #MM-34437 fixed ValidateLicense test * #MM-34437 Added util tests * #MM-34437 using NoError for checking no error * #MM-34437 using NoError for checking no error * Added dummy piblic key for testing * Fixed tests * #MM-34437 udpaetd trial license URL for testing * #MM-34437 adjusted times for licences generated through admin portal * Reverted test-only changes Co-authored-by: Martin Kraft <martin@upspin.org> Co-authored-by: Hossein <hahmadia@users.noreply.github.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> Co-authored-by: Amy Blais <amy_blais@hotmail.com> Co-authored-by: Ben Cooke <benkcooke@gmail.com> Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
2021-06-17 08:07:34 -04:00
if err != nil {
c.Err = model.NewAppError("requestTrialLicense", "api.license.request-trial.can-start-trial.error", nil, "", http.StatusInternalServerError).Wrap(err)
MM-34437 Preventing infinite trial requests (#17472) * MM-34434: Added 'is_trial' boolean to all trial license requests and to the License struct. * MM-34434: Generalized the concept of a license request. * MM-34434: Verifies JSON field of license instance is set. * MM-34434: Added missing client param. * MM-34434: Added some tests of the request trial API endpoint. * MM-34434: Removed comment. * fix broken test (#17348) * Add missing wrapped errors (#17339) * Improve document extraction and including a document extraction command (#17183) * Add extract documents content command * Adding the extraction command and making the pure go pdf library as secondary option * Improving the memory usage and docextractor interface * Enable content extraction by default in all the instances * Tiny improvement on archive indexing * Adding App interface generation and the opentracing layer * Fixing linter errors * Addressing PR review comments * Addressing PR review comments * Update en.json (#17356) Automatic Merge * adding new feature flag (#17308) Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Bump no_output_timeout to 2 hours (#17358) * log invalid username (#17345) Automatic Merge * MM-34434: Added missing client param. MM-34434: Added some tests of the request trial API endpoint. MM-34434: Removed comment. * MM-34434: Switched to a hard-coded true value. * MM-34434: Reverts test change. * MM-34434: Removes unnecessary field. * MM-34434: Tests that is_trial is hard-coded by TrialLicenseRequest. * MM-34434: Removed accidental commit. * MM-34434: Removes unnecessary is_trial key from JSON payload. * MM-34434: Reverts to old pointer receiver variable name. * MM-34434: Removes test. * #MM-34437 Initialized license service * ##MM-34437 Verified at all points if server is trial elligible * WIp * #MM-34437 removed unused commented code * MM-34437 make a log less severe * #MM-34437 generated einterface mocks * #MM-34437 added license on new file * #MM-34437 removed unused translation * #MM-34437 some refactoring * Update api4/license.go * Update api4/license.go * #MM-34437 made a variable name consistent * #MM-34437 Added mocks for lince validator * #M--34437 Added license validator test framework * #MM-34437 Renamed isTrial method to isTrialLicense to avoid conflict with newlya dded field * #M--34437 Allowed sales-sanctioned trials * #MM-34437 fixed trial license API tests * Added tests for add license API * #MM-34437 fixed ValidateLicense test * #MM-34437 Added util tests * #MM-34437 using NoError for checking no error * #MM-34437 using NoError for checking no error * Added dummy piblic key for testing * Fixed tests * #MM-34437 udpaetd trial license URL for testing * #MM-34437 adjusted times for licences generated through admin portal * Reverted test-only changes Co-authored-by: Martin Kraft <martin@upspin.org> Co-authored-by: Hossein <hahmadia@users.noreply.github.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> Co-authored-by: Amy Blais <amy_blais@hotmail.com> Co-authored-by: Ben Cooke <benkcooke@gmail.com> Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
2021-06-17 08:07:34 -04:00
return
}
if !canStartTrialLicense {
c.Err = model.NewAppError("requestTrialLicense", "api.license.request-trial.can-start-trial.not-allowed", nil, "", http.StatusBadRequest)
return
}
var trialRequest *model.TrialLicenseRequest
b, readErr := io.ReadAll(r.Body)
if readErr != nil {
c.Err = model.NewAppError("requestTrialLicense", "api.license.request-trial.bad-request", nil, "", http.StatusBadRequest)
return
}
err = json.Unmarshal(b, &trialRequest)
if err != nil {
c.Err = model.NewAppError("requestTrialLicense", "api.license.request-trial.bad-request", nil, "", http.StatusBadRequest).Wrap(err)
return
}
var appErr *model.AppError
// If any of the newly supported trial request fields are set (ie, not a legacy request), process this as a new trial request (requiring the new fields) otherwise fall back on the old method.
if !trialRequest.IsLegacy() {
appErr = c.App.Channels().RequestTrialLicenseWithExtraFields(c.AppContext.Session().UserId, trialRequest)
} else {
appErr = c.App.Channels().RequestTrialLicense(c.AppContext.Session().UserId, trialRequest.Users, trialRequest.TermsAccepted, trialRequest.ReceiveEmailsAccepted)
}
if appErr != nil {
c.Err = appErr
return
}
auditRec.Success()
c.LogAudit("success")
ReturnStatusOK(w)
}
MM-34437 Preventing infinite trial requests (#17472) * MM-34434: Added 'is_trial' boolean to all trial license requests and to the License struct. * MM-34434: Generalized the concept of a license request. * MM-34434: Verifies JSON field of license instance is set. * MM-34434: Added missing client param. * MM-34434: Added some tests of the request trial API endpoint. * MM-34434: Removed comment. * fix broken test (#17348) * Add missing wrapped errors (#17339) * Improve document extraction and including a document extraction command (#17183) * Add extract documents content command * Adding the extraction command and making the pure go pdf library as secondary option * Improving the memory usage and docextractor interface * Enable content extraction by default in all the instances * Tiny improvement on archive indexing * Adding App interface generation and the opentracing layer * Fixing linter errors * Addressing PR review comments * Addressing PR review comments * Update en.json (#17356) Automatic Merge * adding new feature flag (#17308) Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Bump no_output_timeout to 2 hours (#17358) * log invalid username (#17345) Automatic Merge * MM-34434: Added missing client param. MM-34434: Added some tests of the request trial API endpoint. MM-34434: Removed comment. * MM-34434: Switched to a hard-coded true value. * MM-34434: Reverts test change. * MM-34434: Removes unnecessary field. * MM-34434: Tests that is_trial is hard-coded by TrialLicenseRequest. * MM-34434: Removed accidental commit. * MM-34434: Removes unnecessary is_trial key from JSON payload. * MM-34434: Reverts to old pointer receiver variable name. * MM-34434: Removes test. * #MM-34437 Initialized license service * ##MM-34437 Verified at all points if server is trial elligible * WIp * #MM-34437 removed unused commented code * MM-34437 make a log less severe * #MM-34437 generated einterface mocks * #MM-34437 added license on new file * #MM-34437 removed unused translation * #MM-34437 some refactoring * Update api4/license.go * Update api4/license.go * #MM-34437 made a variable name consistent * #MM-34437 Added mocks for lince validator * #M--34437 Added license validator test framework * #MM-34437 Renamed isTrial method to isTrialLicense to avoid conflict with newlya dded field * #M--34437 Allowed sales-sanctioned trials * #MM-34437 fixed trial license API tests * Added tests for add license API * #MM-34437 fixed ValidateLicense test * #MM-34437 Added util tests * #MM-34437 using NoError for checking no error * #MM-34437 using NoError for checking no error * Added dummy piblic key for testing * Fixed tests * #MM-34437 udpaetd trial license URL for testing * #MM-34437 adjusted times for licences generated through admin portal * Reverted test-only changes Co-authored-by: Martin Kraft <martin@upspin.org> Co-authored-by: Hossein <hahmadia@users.noreply.github.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> Co-authored-by: Amy Blais <amy_blais@hotmail.com> Co-authored-by: Ben Cooke <benkcooke@gmail.com> Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
2021-06-17 08:07:34 -04:00
func getPrevTrialLicense(c *Context, w http.ResponseWriter, r *http.Request) {
if c.App.Srv().Platform().LicenseManager() == nil {
c.Err = model.NewAppError("getPrevTrialLicense", "api.license.upgrade_needed.app_error", nil, "", http.StatusForbidden)
return
}
license, err := c.App.Srv().Platform().LicenseManager().GetPrevTrial()
MM-34437 Preventing infinite trial requests (#17472) * MM-34434: Added 'is_trial' boolean to all trial license requests and to the License struct. * MM-34434: Generalized the concept of a license request. * MM-34434: Verifies JSON field of license instance is set. * MM-34434: Added missing client param. * MM-34434: Added some tests of the request trial API endpoint. * MM-34434: Removed comment. * fix broken test (#17348) * Add missing wrapped errors (#17339) * Improve document extraction and including a document extraction command (#17183) * Add extract documents content command * Adding the extraction command and making the pure go pdf library as secondary option * Improving the memory usage and docextractor interface * Enable content extraction by default in all the instances * Tiny improvement on archive indexing * Adding App interface generation and the opentracing layer * Fixing linter errors * Addressing PR review comments * Addressing PR review comments * Update en.json (#17356) Automatic Merge * adding new feature flag (#17308) Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Bump no_output_timeout to 2 hours (#17358) * log invalid username (#17345) Automatic Merge * MM-34434: Added missing client param. MM-34434: Added some tests of the request trial API endpoint. MM-34434: Removed comment. * MM-34434: Switched to a hard-coded true value. * MM-34434: Reverts test change. * MM-34434: Removes unnecessary field. * MM-34434: Tests that is_trial is hard-coded by TrialLicenseRequest. * MM-34434: Removed accidental commit. * MM-34434: Removes unnecessary is_trial key from JSON payload. * MM-34434: Reverts to old pointer receiver variable name. * MM-34434: Removes test. * #MM-34437 Initialized license service * ##MM-34437 Verified at all points if server is trial elligible * WIp * #MM-34437 removed unused commented code * MM-34437 make a log less severe * #MM-34437 generated einterface mocks * #MM-34437 added license on new file * #MM-34437 removed unused translation * #MM-34437 some refactoring * Update api4/license.go * Update api4/license.go * #MM-34437 made a variable name consistent * #MM-34437 Added mocks for lince validator * #M--34437 Added license validator test framework * #MM-34437 Renamed isTrial method to isTrialLicense to avoid conflict with newlya dded field * #M--34437 Allowed sales-sanctioned trials * #MM-34437 fixed trial license API tests * Added tests for add license API * #MM-34437 fixed ValidateLicense test * #MM-34437 Added util tests * #MM-34437 using NoError for checking no error * #MM-34437 using NoError for checking no error * Added dummy piblic key for testing * Fixed tests * #MM-34437 udpaetd trial license URL for testing * #MM-34437 adjusted times for licences generated through admin portal * Reverted test-only changes Co-authored-by: Martin Kraft <martin@upspin.org> Co-authored-by: Hossein <hahmadia@users.noreply.github.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> Co-authored-by: Amy Blais <amy_blais@hotmail.com> Co-authored-by: Ben Cooke <benkcooke@gmail.com> Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
2021-06-17 08:07:34 -04:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var clientLicense map[string]string
2021-07-12 14:05:36 -04:00
if c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionReadLicenseInformation) {
MM-34437 Preventing infinite trial requests (#17472) * MM-34434: Added 'is_trial' boolean to all trial license requests and to the License struct. * MM-34434: Generalized the concept of a license request. * MM-34434: Verifies JSON field of license instance is set. * MM-34434: Added missing client param. * MM-34434: Added some tests of the request trial API endpoint. * MM-34434: Removed comment. * fix broken test (#17348) * Add missing wrapped errors (#17339) * Improve document extraction and including a document extraction command (#17183) * Add extract documents content command * Adding the extraction command and making the pure go pdf library as secondary option * Improving the memory usage and docextractor interface * Enable content extraction by default in all the instances * Tiny improvement on archive indexing * Adding App interface generation and the opentracing layer * Fixing linter errors * Addressing PR review comments * Addressing PR review comments * Update en.json (#17356) Automatic Merge * adding new feature flag (#17308) Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Bump no_output_timeout to 2 hours (#17358) * log invalid username (#17345) Automatic Merge * MM-34434: Added missing client param. MM-34434: Added some tests of the request trial API endpoint. MM-34434: Removed comment. * MM-34434: Switched to a hard-coded true value. * MM-34434: Reverts test change. * MM-34434: Removes unnecessary field. * MM-34434: Tests that is_trial is hard-coded by TrialLicenseRequest. * MM-34434: Removed accidental commit. * MM-34434: Removes unnecessary is_trial key from JSON payload. * MM-34434: Reverts to old pointer receiver variable name. * MM-34434: Removes test. * #MM-34437 Initialized license service * ##MM-34437 Verified at all points if server is trial elligible * WIp * #MM-34437 removed unused commented code * MM-34437 make a log less severe * #MM-34437 generated einterface mocks * #MM-34437 added license on new file * #MM-34437 removed unused translation * #MM-34437 some refactoring * Update api4/license.go * Update api4/license.go * #MM-34437 made a variable name consistent * #MM-34437 Added mocks for lince validator * #M--34437 Added license validator test framework * #MM-34437 Renamed isTrial method to isTrialLicense to avoid conflict with newlya dded field * #M--34437 Allowed sales-sanctioned trials * #MM-34437 fixed trial license API tests * Added tests for add license API * #MM-34437 fixed ValidateLicense test * #MM-34437 Added util tests * #MM-34437 using NoError for checking no error * #MM-34437 using NoError for checking no error * Added dummy piblic key for testing * Fixed tests * #MM-34437 udpaetd trial license URL for testing * #MM-34437 adjusted times for licences generated through admin portal * Reverted test-only changes Co-authored-by: Martin Kraft <martin@upspin.org> Co-authored-by: Hossein <hahmadia@users.noreply.github.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> Co-authored-by: Amy Blais <amy_blais@hotmail.com> Co-authored-by: Ben Cooke <benkcooke@gmail.com> Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
2021-06-17 08:07:34 -04:00
clientLicense = utils.GetClientLicense(license)
} else {
clientLicense = utils.GetSanitizedClientLicense(utils.GetClientLicense(license))
}
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write([]byte(model.MapToJSON(clientLicense))); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
MM-34437 Preventing infinite trial requests (#17472) * MM-34434: Added 'is_trial' boolean to all trial license requests and to the License struct. * MM-34434: Generalized the concept of a license request. * MM-34434: Verifies JSON field of license instance is set. * MM-34434: Added missing client param. * MM-34434: Added some tests of the request trial API endpoint. * MM-34434: Removed comment. * fix broken test (#17348) * Add missing wrapped errors (#17339) * Improve document extraction and including a document extraction command (#17183) * Add extract documents content command * Adding the extraction command and making the pure go pdf library as secondary option * Improving the memory usage and docextractor interface * Enable content extraction by default in all the instances * Tiny improvement on archive indexing * Adding App interface generation and the opentracing layer * Fixing linter errors * Addressing PR review comments * Addressing PR review comments * Update en.json (#17356) Automatic Merge * adding new feature flag (#17308) Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> * Bump no_output_timeout to 2 hours (#17358) * log invalid username (#17345) Automatic Merge * MM-34434: Added missing client param. MM-34434: Added some tests of the request trial API endpoint. MM-34434: Removed comment. * MM-34434: Switched to a hard-coded true value. * MM-34434: Reverts test change. * MM-34434: Removes unnecessary field. * MM-34434: Tests that is_trial is hard-coded by TrialLicenseRequest. * MM-34434: Removed accidental commit. * MM-34434: Removes unnecessary is_trial key from JSON payload. * MM-34434: Reverts to old pointer receiver variable name. * MM-34434: Removes test. * #MM-34437 Initialized license service * ##MM-34437 Verified at all points if server is trial elligible * WIp * #MM-34437 removed unused commented code * MM-34437 make a log less severe * #MM-34437 generated einterface mocks * #MM-34437 added license on new file * #MM-34437 removed unused translation * #MM-34437 some refactoring * Update api4/license.go * Update api4/license.go * #MM-34437 made a variable name consistent * #MM-34437 Added mocks for lince validator * #M--34437 Added license validator test framework * #MM-34437 Renamed isTrial method to isTrialLicense to avoid conflict with newlya dded field * #M--34437 Allowed sales-sanctioned trials * #MM-34437 fixed trial license API tests * Added tests for add license API * #MM-34437 fixed ValidateLicense test * #MM-34437 Added util tests * #MM-34437 using NoError for checking no error * #MM-34437 using NoError for checking no error * Added dummy piblic key for testing * Fixed tests * #MM-34437 udpaetd trial license URL for testing * #MM-34437 adjusted times for licences generated through admin portal * Reverted test-only changes Co-authored-by: Martin Kraft <martin@upspin.org> Co-authored-by: Hossein <hahmadia@users.noreply.github.com> Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com> Co-authored-by: Jesús Espino <jespinog@gmail.com> Co-authored-by: Amy Blais <amy_blais@hotmail.com> Co-authored-by: Ben Cooke <benkcooke@gmail.com> Co-authored-by: Benjamin Cooke <benjamincooke@Benjamins-MacBook-Pro.local> Co-authored-by: Mattermod <mattermod@users.noreply.github.com> Co-authored-by: Agniva De Sarker <agnivade@yahoo.co.in> Co-authored-by: Max Erenberg <max.erenberg@mattermost.com>
2021-06-17 08:07:34 -04:00
}
MM-63728: Add license load metric endpoint and UI indicator (#30700) * Add license load metric endpoint and UI indicator Adds an API endpoint to calculate and return license usage as a load metric, and displays this metric in the About dialog. The metric is calculated as (MAU/licensed users)*100. Additionally: - Renamed function to be consistent with API endpoint name - Added proper i18n strings for error messages and UI elements * Fix TypeScript null check in about_build_modal.tsx * MM-63728: Update OpenAPI documentation for license load metric Update the OpenAPI documentation and code comments to correctly describe the license load metric calculation as using a multiplier of 1000 instead of percentage. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * MM-63728: Use float for license load metric calculation Modify the license load metric calculation to use floats throughout the computation process while still returning an integer result. This maintains the existing API but improves the precision of the calculation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> * improve tests manually * Update server/channels/api4/license_test.go Co-authored-by: Doug Lauder <wiggin77@warpmail.net> * Update server/channels/api4/license_test.go Co-authored-by: Doug Lauder <wiggin77@warpmail.net> --------- Co-authored-by: Mattermost Build <build@mattermost.com> Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
2025-04-17 16:29:46 -04:00
// getLicenseLoadMetric returns a load metric computed as (mau / licensed) * 1000.
func getLicenseLoadMetric(c *Context, w http.ResponseWriter, r *http.Request) {
var loadMetric int
var licenseUsers int
license := c.App.Srv().License()
if license != nil && license.Features != nil {
licenseUsers = *license.Features.Users
}
if licenseUsers > 0 {
monthlyActiveUsers, err := c.App.Srv().Store().User().AnalyticsActiveCount(app.MonthMilliseconds, model.UserCountOptions{IncludeBotAccounts: false, IncludeDeleted: false})
if err != nil {
c.Err = model.NewAppError("getLicenseLoad", "api.license.load_metric.app_error", nil, "", http.StatusInternalServerError).Wrap(err)
return
}
loadMetric = int(math.Round((float64(monthlyActiveUsers) / float64(licenseUsers) * float64(1000))))
}
// Create response object
data := map[string]int{
"load": loadMetric,
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(data); err != nil {
c.Logger.Warn("Error while writing response", mlog.Err(err))
}
}