3
0

Migrate auth&general email templates from settings

This commit is contained in:
Denis Arh
2021-03-12 07:34:05 +01:00
parent 3649da2967
commit 53e0c2838f
6 changed files with 133 additions and 53 deletions
+6 -3
View File
@@ -2,6 +2,7 @@ package provision
import (
"context"
"fmt"
"github.com/cortezaproject/corteza-server/pkg/rbac"
"path/filepath"
"strings"
@@ -19,9 +20,11 @@ import (
//
// paths can be colon delimited list of absolute or relative paths and/or with glob pattern
func importConfig(ctx context.Context, log *zap.Logger, s store.Storer, paths string) error {
if can, err := canImportConfig(ctx, s); !can || err != nil {
log.Info("config import skipped, already provisioned")
return err
if can, err := canImportConfig(ctx, s); !can {
log.Info("already provisioned, skipping full config import")
return nil
} else if err != nil {
return fmt.Errorf("failed to check if config import can be done: %w", err)
}
var (
+7
View File
@@ -0,0 +1,7 @@
package provision
import "time"
func releaseDate(y int, m time.Month) time.Time {
return time.Date(y, m+1, 1, 0, 0, 0, 0, time.UTC)
}
@@ -2,16 +2,13 @@ package provision
import (
"context"
"time"
"github.com/cortezaproject/corteza-server/store"
"github.com/cortezaproject/corteza-server/system/types"
"time"
)
func apps(ctx context.Context, s store.Storer) error {
var (
v202103 = time.Date(2021, 4, 31, 0, 0, 0, 0, time.UTC)
)
rd := releaseDate(2021, time.March)
set, _, err := store.SearchApplications(ctx, s, types.ApplicationFilter{})
if err != nil {
@@ -19,8 +16,8 @@ func apps(ctx context.Context, s store.Storer) error {
}
return set.Walk(func(app *types.Application) (err error) {
if app.Unify.Url == "/messaging" && (app.UpdatedAt == nil || app.UpdatedAt.Before(v202103)) {
// Disable messaging app but only if it was not updated after 21.3 release date
// Disable messaging app but only if it was not updated after 21.3 release date
if app.Unify.Url == "/messaging" && (app.UpdatedAt == nil || app.UpdatedAt.Before(rd)) {
app.Enabled = false
err = store.UpdateApplication(ctx, s, app)
}
@@ -0,0 +1,108 @@
package provision
import (
"context"
"fmt"
"github.com/cortezaproject/corteza-server/pkg/errors"
"github.com/cortezaproject/corteza-server/pkg/id"
"github.com/cortezaproject/corteza-server/store"
"github.com/cortezaproject/corteza-server/system/types"
"go.uber.org/zap"
"time"
)
// Pre 2021.3 versions had email templates stored in settings
// from 2021.3 onwards we have dedicated subsystem for managing templates
//
// This migration moves email templates from settings templates.
func migrateEmailTemplates(ctx context.Context, log *zap.Logger, s store.Storer) error {
var (
// setting name => template handle
m = map[string]*types.Template{
//"general.mail.logo",
"general.mail.header.en": {
Type: "text/html",
Handle: "email_general_header",
Partial: true,
Meta: types.TemplateMeta{
Short: "General template header",
Description: "General template header to use with system email notifications",
},
},
"general.mail.footer.en": {
Type: "text/html",
Handle: "email_general_footer",
Partial: true,
Meta: types.TemplateMeta{
Short: "General template footer",
Description: "General template footer to use with system email notifications",
},
},
"auth.mail.email-confirmation.subject.en": {
Type: "text/plain",
Handle: "auth_email_confirmation_subject",
Meta: types.TemplateMeta{Short: "Password reset subject"},
},
"auth.mail.email-confirmation.body.en": {
Type: "text/html",
Handle: "auth_email_confirmation_body",
Meta: types.TemplateMeta{Short: "Password reset content"},
},
"auth.mail.password-reset.subject.en": {
Type: "text/plain",
Handle: "auth_email_password_reset_subject",
Meta: types.TemplateMeta{Short: "Email confirmation subject"},
},
"auth.mail.password-reset.body.en": {
Type: "text/html",
Handle: "auth_email_password_reset_body",
Meta: types.TemplateMeta{Short: "Email confirmation content"},
},
}
)
return store.Tx(ctx, s, func(ctx context.Context, s store.Storer) error {
for name, tmpl := range m {
sval, err := store.LookupSettingByNameOwnedBy(ctx, s, name, 0)
if errors.IsNotFound(err) {
// setting not found, that's ok, see the next one
continue
} else if err != nil {
return fmt.Errorf("failed to lookup for setting by bame: %w", err)
}
_, err = store.LookupTemplateByHandle(ctx, s, tmpl.Handle)
if err != nil && !errors.IsNotFound(err) {
// any error but not-found is fatal.
return fmt.Errorf("failed to lookup for template by handle: %w", err)
} else if err == nil {
// template exists
continue
}
sval.String()
tmpl.ID = id.Next()
tmpl.CreatedAt = time.Now()
tmpl.Template = sval.String()
err = store.CreateTemplate(ctx, s, tmpl)
if err != nil {
return fmt.Errorf("failed to store migrated template: %w", err)
}
log.Debug("migrated template from settings",
zap.String("setting", name),
zap.String("handle", tmpl.Handle),
)
}
// Go over all settings again and remove them
for name := range m {
if err := store.DeleteSettingByNameOwnedBy(ctx, s, name, 0); err != nil {
return err
}
}
return nil
})
}
+8
View File
@@ -17,9 +17,17 @@ import (
func Run(ctx context.Context, log *zap.Logger, s store.Storer, provisionOpt options.ProvisionOpt, authOpt options.AuthOpt) error {
ffn := []func() error{
// Old relics
func() error { return roles(ctx, s) },
// Migrations:
func() error { return apps(ctx, s) },
func() error { return migrateEmailTemplates(ctx, log, s) },
// Config (full & partial)
func() error { return importConfig(ctx, log, s, provisionOpt.Path) },
// Auto-discoveries and other parts that can not be imported from static files
func() error { return authSettingsAutoDiscovery(ctx, log, service.DefaultSettings) },
func() error { return authAddExternals(ctx, log) },
func() error { return service.DefaultSettings.UpdateCurrent(ctx) },
File diff suppressed because one or more lines are too long