From 81409ee7c401a5f312bd20d5af7845226fe14352 Mon Sep 17 00:00:00 2001 From: Allan Guwatudde Date: Tue, 21 Dec 2021 09:46:02 +0300 Subject: [PATCH] [MM-40407] - Do not show the Renew Now if the license id does not exist in the portal (#19188) * [MM-40407] - Do not show the Renew Now if the license id does not exist in the portal * improvements * impl mock * fix translations * feedback impl * fix typo Co-authored-by: Mattermod --- api4/license.go | 9 ++++++++- app/license.go | 6 +++--- app/server.go | 4 ++-- einterfaces/cloud.go | 3 +++ einterfaces/mocks/CloudInterface.go | 14 ++++++++++++++ i18n/en.json | 4 ++++ 6 files changed, 34 insertions(+), 6 deletions(-) diff --git a/api4/license.go b/api4/license.go index 22a7653c91..5c3a182e93 100644 --- a/api4/license.go +++ b/api4/license.go @@ -266,12 +266,19 @@ func requestRenewalLink(c *Context, w http.ResponseWriter, r *http.Request) { return } - renewalLink, err := c.App.Srv().GenerateLicenseRenewalLink() + renewalLink, token, err := c.App.Srv().GenerateLicenseRenewalLink() if err != nil { c.Err = err return } + // check if it is possible to renew license on the portal with generated token + e := c.App.Cloud().GetLicenseRenewalStatus(c.AppContext.Session().UserId, token) + if e != nil { + c.Err = model.NewAppError("requestRenewalLink", "api.license.request_renewal_link.cannot_renew_on_cws", nil, e.Error(), http.StatusBadRequest) + return + } + auditRec.Success() c.LogAudit("success") diff --git a/app/license.go b/app/license.go index 4eaccb8be4..00f88b404e 100644 --- a/app/license.go +++ b/app/license.go @@ -369,11 +369,11 @@ func (s *Server) renewalTokenValid(tokenString, signingKey string) (bool, error) } // GenerateLicenseRenewalLink returns a link that points to the CWS where clients can renew license -func (s *Server) GenerateLicenseRenewalLink() (string, *model.AppError) { +func (s *Server) GenerateLicenseRenewalLink() (string, string, *model.AppError) { renewalToken, err := s.GenerateRenewalToken(JWTDefaultTokenExpiration) if err != nil { - return "", err + return "", "", err } renewalLink := LicenseRenewalURL + "?token=" + renewalToken - return renewalLink, nil + return renewalLink, renewalToken, nil } diff --git a/app/server.go b/app/server.go index 4b1d20d9d9..65471dd1e7 100644 --- a/app/server.go +++ b/app/server.go @@ -1644,7 +1644,7 @@ func (s *Server) sendLicenseUpForRenewalEmail(users map[string]*model.User, lice daysToExpiration := license.DaysToExpiration() - renewalLink, appErr := s.GenerateLicenseRenewalLink() + renewalLink, _, appErr := s.GenerateLicenseRenewalLink() if appErr != nil { return model.NewAppError("s.sendLicenseUpForRenewalEmail", "api.server.license_up_for_renewal.error_generating_link", nil, appErr.Error(), http.StatusInternalServerError) } @@ -1735,7 +1735,7 @@ func (s *Server) doLicenseExpirationCheck() { // SendRemoveExpiredLicenseEmail formats an email and uses the email service to send the email to user with link pointing to CWS // to renew the user license func (s *Server) SendRemoveExpiredLicenseEmail(email string, locale, siteURL string) *model.AppError { - renewalLink, err := s.GenerateLicenseRenewalLink() + renewalLink, _, err := s.GenerateLicenseRenewalLink() if err != nil { return err } diff --git a/einterfaces/cloud.go b/einterfaces/cloud.go index e1119d1569..cff0ecfe94 100644 --- a/einterfaces/cloud.go +++ b/einterfaces/cloud.go @@ -22,4 +22,7 @@ type CloudInterface interface { GetInvoicePDF(userID, invoiceID string) ([]byte, string, error) ChangeSubscription(userID, subscriptionID string, subscriptionChange *model.SubscriptionChange) (*model.Subscription, error) + + // GetLicenseRenewalStatus checks on the portal whether it is possible to use token to renew a license + GetLicenseRenewalStatus(userID, token string) error } diff --git a/einterfaces/mocks/CloudInterface.go b/einterfaces/mocks/CloudInterface.go index 5327ffb4bd..44780399fb 100644 --- a/einterfaces/mocks/CloudInterface.go +++ b/einterfaces/mocks/CloudInterface.go @@ -173,6 +173,20 @@ func (_m *CloudInterface) GetInvoicesForSubscription(userID string) ([]*model.In return r0, r1 } +// GetLicenseRenewalStatus provides a mock function with given fields: userID, token +func (_m *CloudInterface) GetLicenseRenewalStatus(userID string, token string) error { + ret := _m.Called(userID, token) + + var r0 error + if rf, ok := ret.Get(0).(func(string, string) error); ok { + r0 = rf(userID, token) + } else { + r0 = ret.Error(0) + } + + return r0 +} + // GetSubscription provides a mock function with given fields: userID func (_m *CloudInterface) GetSubscription(userID string) (*model.Subscription, error) { ret := _m.Called(userID) diff --git a/i18n/en.json b/i18n/en.json index e08fe9069c..5a4229f67b 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -1953,6 +1953,10 @@ "id": "api.license.request_renewal_link.app_error", "translation": "Error getting the license renewal link" }, + { + "id": "api.license.request_renewal_link.cannot_renew_on_cws", + "translation": "Renewing this license on the portal is not possible" + }, { "id": "api.license.request_trial_license.app_error", "translation": "Unable to get a trial license, please try again or contact with support@mattermost.com."