Check license expiration, disable license and notify administrator (#14420)

* Check license expiration and notify administrator
Этот коммит содержится в:
catalintomai
2020-05-06 10:28:49 -07:00
коммит произвёл GitHub
родитель 5ad3eaf7ee
Коммит 58305b080f
9 изменённых файлов: 175 добавлений и 0 удалений

Просмотреть файл

@@ -848,6 +848,7 @@ type AppIface interface {
SendNotifications(post *model.Post, team *model.Team, channel *model.Channel, sender *model.User, parentPostList *model.PostList) ([]string, error)
SendPasswordReset(email string, siteURL string) (bool, *model.AppError)
SendPasswordResetEmail(email string, token *model.Token, locale, siteURL string) (bool, *model.AppError)
SendRemoveExpiredLicenseEmail(email string, locale, siteURL string, licenseId string) *model.AppError
SendSignInChangeEmail(email, method, locale, siteURL string) *model.AppError
ServeInterPluginRequest(w http.ResponseWriter, r *http.Request, sourcePluginId, destinationPluginId string)
ServePluginRequest(w http.ResponseWriter, r *http.Request)

Просмотреть файл

@@ -513,6 +513,24 @@ func (a *App) SendDeactivateAccountEmail(email string, locale, siteURL string) *
return nil
}
func (a *App) SendRemoveExpiredLicenseEmail(email string, locale, siteURL string, licenseId string) *model.AppError {
T := utils.GetUserTranslations(locale)
subject := T("api.templates.remove_expired_license.subject",
map[string]interface{}{"SiteName": a.ClientConfig()["SiteName"]})
bodyPage := a.newEmailTemplate("remove_expired_license", locale)
bodyPage.Props["SiteURL"] = siteURL
bodyPage.Props["Title"] = T("api.templates.remove_expired_license.body.title")
bodyPage.Props["Link"] = fmt.Sprintf("%s?id=%s", model.LICENSE_RENEWAL_LINK, licenseId)
bodyPage.Props["LinkButton"] = T("api.templates.remove_expired_license.body.renew_button")
if err := a.sendMail(email, subject, bodyPage.Render()); err != nil {
return model.NewAppError("SendRemoveExpiredLicenseEmail", "api.license.remove_expired_license.failed.error", nil, err.Error(), http.StatusInternalServerError)
}
return nil
}
func (a *App) sendNotificationMail(to, subject, htmlBody string) *model.AppError {
if !*a.Config().EmailSettings.SendEmailNotifications {
return nil

Просмотреть файл

@@ -158,6 +158,8 @@ func (a *App) RemoveLicense() *model.AppError {
return nil
}
mlog.Info("Remove license.", mlog.String("id", model.SYSTEM_ACTIVE_LICENSE_ID))
sysVar := &model.System{}
sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID
sysVar.Value = ""

Просмотреть файл

@@ -12402,6 +12402,28 @@ func (a *OpenTracingAppLayer) SendPasswordResetEmail(email string, token *model.
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) SendRemoveExpiredLicenseEmail(email string, locale string, siteURL string, licenseId string) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SendRemoveExpiredLicenseEmail")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0 := a.app.SendRemoveExpiredLicenseEmail(email, locale, siteURL, licenseId)
if resultVar0 != nil {
span.LogFields(spanlog.Error(resultVar0))
ext.Error.Set(span, true)
}
return resultVar0
}
func (a *OpenTracingAppLayer) SendSignInChangeEmail(email string, method string, locale string, siteURL string) *model.AppError {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.SendSignInChangeEmail")

Просмотреть файл

@@ -348,6 +348,9 @@ func NewServer(options ...Option) (*Server, error) {
s.Go(func() {
runCommandWebhookCleanupJob(s)
})
s.Go(func() {
runLicenseExpirationCheckJob(s)
})
if complianceI := s.Compliance; complianceI != nil {
complianceI.StartComplianceDailyJob()
@@ -777,6 +780,13 @@ func runSessionCleanupJob(s *Server) {
}, time.Hour*24)
}
func runLicenseExpirationCheckJob(s *Server) {
doLicenseExpirationCheck(s)
model.CreateRecurringTask("License Expiration Check", func() {
doLicenseExpirationCheck(s)
}, time.Hour*24)
}
func doSecurity(s *Server) {
s.DoSecurityUpdateCheck()
}
@@ -804,6 +814,46 @@ func doSessionCleanup(s *Server) {
s.Store.Session().Cleanup(model.GetMillis(), SESSIONS_CLEANUP_BATCH_SIZE)
}
func doLicenseExpirationCheck(s *Server) {
s.FakeApp().LoadLicense()
license := s.License()
if license == nil {
mlog.Debug("License cannot be found.")
return
}
if !license.IsPastGracePeriod() {
mlog.Debug("License is not past the grace period.")
return
}
users, err := s.Store.User().GetSystemAdminProfiles()
if err != nil {
mlog.Error("Failed to get system admins for license expired message from Mattermost.")
return
}
//send email to admin(s)
for _, user := range users {
user := user
if user.Email == "" {
mlog.Error("Invalid system admin email.", mlog.String("user_email", user.Email))
continue
}
mlog.Debug("Sending license expired email.", mlog.String("user_email", user.Email))
s.Go(func() {
if err := s.FakeApp().SendRemoveExpiredLicenseEmail(user.Email, user.Locale, *s.Config().ServiceSettings.SiteURL, license.Id); err != nil {
mlog.Error("Error while sending the license expired email.", mlog.String("user_email", user.Email), mlog.Err(err))
}
})
}
//remove the license
s.FakeApp().RemoveLicense()
}
func (s *Server) StartSearchEngine() (string, string) {
if s.SearchEngine.ElasticsearchEngine != nil && s.SearchEngine.ElasticsearchEngine.IsActive() {
s.Go(func() {