From 6d78fe28f5121ebef3c90fab0497d6b075bbac86 Mon Sep 17 00:00:00 2001 From: Fabi <38692350+fgerschwiler@users.noreply.github.com> Date: Tue, 4 Jan 2022 09:35:22 +0100 Subject: [PATCH] fix: Get Label policy in notifications (#2923) --- .../eventsourcing/handler/notification.go | 39 ++------------- .../notification/templates/templateData.go | 50 +++---------------- internal/notification/types/domain_claimed.go | 6 +-- .../types/email_verification_code.go | 6 +-- internal/notification/types/init_code.go | 6 +-- internal/notification/types/password_code.go | 6 +-- .../types/passwordless_registration_link.go | 6 +-- internal/notification/types/templateData.go | 46 +++++++++++++++++ 8 files changed, 72 insertions(+), 93 deletions(-) create mode 100644 internal/notification/types/templateData.go diff --git a/internal/notification/repository/eventsourcing/handler/notification.go b/internal/notification/repository/eventsourcing/handler/notification.go index 28f385a1d1..87f585e1a7 100644 --- a/internal/notification/repository/eventsourcing/handler/notification.go +++ b/internal/notification/repository/eventsourcing/handler/notification.go @@ -18,8 +18,6 @@ import ( queryv1 "github.com/caos/zitadel/internal/eventstore/v1/query" "github.com/caos/zitadel/internal/eventstore/v1/spooler" "github.com/caos/zitadel/internal/i18n" - iam_model "github.com/caos/zitadel/internal/iam/model" - iam_es_model "github.com/caos/zitadel/internal/iam/repository/view/model" "github.com/caos/zitadel/internal/notification/types" "github.com/caos/zitadel/internal/query" user_repo "github.com/caos/zitadel/internal/repository/user" @@ -29,10 +27,8 @@ import ( ) const ( - notificationTable = "notification.notifications" - NotifyUserID = "NOTIFICATION" - labelPolicyTableOrg = "management.label_policies" - labelPolicyTableDef = "adminapi.label_policies" + notificationTable = "notification.notifications" + NotifyUserID = "NOTIFICATION" ) type Notification struct { @@ -404,38 +400,13 @@ func getSetNotifyContextData(orgID string) context.Context { } // Read organization specific colors -func (n *Notification) getLabelPolicy(ctx context.Context) (*iam_model.LabelPolicyView, error) { - // read from Org - policy, err := n.view.StylingByAggregateIDAndState(authz.GetCtxData(ctx).OrgID, labelPolicyTableOrg, int32(domain.LabelPolicyStateActive)) - if errors.IsNotFound(err) { - // read from default - policy, err = n.view.StylingByAggregateIDAndState(n.systemDefaults.IamID, labelPolicyTableDef, int32(domain.LabelPolicyStateActive)) - if err != nil { - return nil, err - } - policy.Default = true - } - if err != nil { - return nil, err - } - return iam_es_model.LabelPolicyViewToModel(policy), err +func (n *Notification) getLabelPolicy(ctx context.Context) (*query.LabelPolicy, error) { + return n.queries.ActiveLabelPolicyByOrg(ctx, authz.GetCtxData(ctx).OrgID) } // Read organization specific template func (n *Notification) getMailTemplate(ctx context.Context) (*query.MailTemplate, error) { - // read from Org - template, err := n.queries.MailTemplateByOrg(ctx, authz.GetCtxData(ctx).OrgID) - if errors.IsNotFound(err) { - // read from default - template, err = n.queries.DefaultMailTemplate(ctx) - if err != nil { - return nil, err - } - } - if err != nil { - return nil, err - } - return template, err + return n.queries.MailTemplateByOrg(ctx, authz.GetCtxData(ctx).OrgID) } func (n *Notification) getTranslatorWithOrgTexts(orgID, textType string) (*i18n.Translator, error) { diff --git a/internal/notification/templates/templateData.go b/internal/notification/templates/templateData.go index 9e2b103ae8..1597c1641d 100644 --- a/internal/notification/templates/templateData.go +++ b/internal/notification/templates/templateData.go @@ -3,20 +3,18 @@ package templates import ( "fmt" "html" - "strings" "github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/i18n" - iam_model "github.com/caos/zitadel/internal/iam/model" ) const ( - defaultFont = "http://fonts.googleapis.com/css?family=Lato:200,300,400,600" - defaultFontFamily = "-apple-system, BlinkMacSystemFont, Segoe UI, Lato, Arial, Helvetica, sans-serif" - defaultLogo = "https://static.zitadel.ch/zitadel-logo-dark@3x.png" - defaultFontColor = "#22292f" - defaultBackgroundColor = "#fafafa" - defaultPrimaryColor = "#5282C1" + DefaultFont = "http://fonts.googleapis.com/css?family=Lato:200,300,400,600" + DefaultFontFamily = "-apple-system, BlinkMacSystemFont, Segoe UI, Lato, Arial, Helvetica, sans-serif" + DefaultLogo = "https://static.zitadel.ch/zitadel-logo-dark@3x.png" + DefaultFontColor = "#22292f" + DefaultBackgroundColor = "#fafafa" + DefaultPrimaryColor = "#5282C1" ) type TemplateData struct { @@ -47,39 +45,3 @@ func (data *TemplateData) Translate(translator *i18n.Translator, msgType string, data.ButtonText = translator.Localize(fmt.Sprintf("%s.%s", msgType, domain.MessageButtonText), args, langs...) data.FooterText = translator.Localize(fmt.Sprintf("%s.%s", msgType, domain.MessageFooterText), args, langs...) } - -func GetTemplateData(translator *i18n.Translator, translateArgs map[string]interface{}, apiDomain, href, msgType, lang string, policy *iam_model.LabelPolicyView) TemplateData { - templateData := TemplateData{ - Href: href, - PrimaryColor: defaultPrimaryColor, - BackgroundColor: defaultBackgroundColor, - FontColor: defaultFontColor, - LogoURL: defaultLogo, - FontURL: defaultFont, - FontFamily: defaultFontFamily, - IncludeFooter: false, - } - templateData.Translate(translator, msgType, translateArgs, lang) - if policy.PrimaryColor != "" { - templateData.PrimaryColor = policy.PrimaryColor - } - if policy.BackgroundColor != "" { - templateData.BackgroundColor = policy.BackgroundColor - } - if policy.FontColor != "" { - templateData.FontColor = policy.FontColor - } - if apiDomain == "" { - return templateData - } - templateData.LogoURL = "" - if policy.LogoURL != "" { - templateData.LogoURL = fmt.Sprintf("%s/assets/v1/%s/%s", apiDomain, policy.AggregateID, policy.LogoURL) - } - if policy.FontURL != "" { - split := strings.Split(policy.FontURL, "/") - templateData.FontFamily = split[len(split)-1] + "," + defaultFontFamily - templateData.FontURL = fmt.Sprintf("%s/assets/v1/%s/%s", apiDomain, policy.AggregateID, policy.FontURL) - } - return templateData -} diff --git a/internal/notification/types/domain_claimed.go b/internal/notification/types/domain_claimed.go index 7e538c1af6..00b083075e 100644 --- a/internal/notification/types/domain_claimed.go +++ b/internal/notification/types/domain_claimed.go @@ -6,8 +6,8 @@ import ( "github.com/caos/zitadel/internal/config/systemdefaults" "github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/i18n" - iam_model "github.com/caos/zitadel/internal/iam/model" "github.com/caos/zitadel/internal/notification/templates" + "github.com/caos/zitadel/internal/query" view_model "github.com/caos/zitadel/internal/user/repository/view/model" ) @@ -16,7 +16,7 @@ type DomainClaimedData struct { URL string } -func SendDomainClaimed(mailhtml string, translator *i18n.Translator, user *view_model.NotifyUser, username string, systemDefaults systemdefaults.SystemDefaults, colors *iam_model.LabelPolicyView, apiDomain string) error { +func SendDomainClaimed(mailhtml string, translator *i18n.Translator, user *view_model.NotifyUser, username string, systemDefaults systemdefaults.SystemDefaults, colors *query.LabelPolicy, apiDomain string) error { url, err := templates.ParseTemplateText(systemDefaults.Notifications.Endpoints.DomainClaimed, &UrlData{UserID: user.ID}) if err != nil { return err @@ -26,7 +26,7 @@ func SendDomainClaimed(mailhtml string, translator *i18n.Translator, user *view_ args["Domain"] = strings.Split(user.LastEmail, "@")[1] domainClaimedData := &DomainClaimedData{ - TemplateData: templates.GetTemplateData(translator, args, apiDomain, url, domain.DomainClaimedMessageType, user.PreferredLanguage, colors), + TemplateData: GetTemplateData(translator, args, apiDomain, url, domain.DomainClaimedMessageType, user.PreferredLanguage, colors), URL: url, } template, err := templates.GetParsedTemplate(mailhtml, domainClaimedData) diff --git a/internal/notification/types/email_verification_code.go b/internal/notification/types/email_verification_code.go index bcc7b7c801..597f24597a 100644 --- a/internal/notification/types/email_verification_code.go +++ b/internal/notification/types/email_verification_code.go @@ -5,8 +5,8 @@ import ( "github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/i18n" - iam_model "github.com/caos/zitadel/internal/iam/model" "github.com/caos/zitadel/internal/notification/templates" + "github.com/caos/zitadel/internal/query" es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model" view_model "github.com/caos/zitadel/internal/user/repository/view/model" ) @@ -16,7 +16,7 @@ type EmailVerificationCodeData struct { URL string } -func SendEmailVerificationCode(mailhtml string, translator *i18n.Translator, user *view_model.NotifyUser, code *es_model.EmailCode, systemDefaults systemdefaults.SystemDefaults, alg crypto.EncryptionAlgorithm, colors *iam_model.LabelPolicyView, apiDomain string) error { +func SendEmailVerificationCode(mailhtml string, translator *i18n.Translator, user *view_model.NotifyUser, code *es_model.EmailCode, systemDefaults systemdefaults.SystemDefaults, alg crypto.EncryptionAlgorithm, colors *query.LabelPolicy, apiDomain string) error { codeString, err := crypto.DecryptString(code.Code, alg) if err != nil { return err @@ -30,7 +30,7 @@ func SendEmailVerificationCode(mailhtml string, translator *i18n.Translator, use args["Code"] = codeString emailCodeData := &EmailVerificationCodeData{ - TemplateData: templates.GetTemplateData(translator, args, apiDomain, url, domain.VerifyEmailMessageType, user.PreferredLanguage, colors), + TemplateData: GetTemplateData(translator, args, apiDomain, url, domain.VerifyEmailMessageType, user.PreferredLanguage, colors), URL: url, } diff --git a/internal/notification/types/init_code.go b/internal/notification/types/init_code.go index 48546b62ee..a91cb956bf 100644 --- a/internal/notification/types/init_code.go +++ b/internal/notification/types/init_code.go @@ -5,8 +5,8 @@ import ( "github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/i18n" - iam_model "github.com/caos/zitadel/internal/iam/model" "github.com/caos/zitadel/internal/notification/templates" + "github.com/caos/zitadel/internal/query" es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model" view_model "github.com/caos/zitadel/internal/user/repository/view/model" ) @@ -22,7 +22,7 @@ type UrlData struct { PasswordSet bool } -func SendUserInitCode(mailhtml string, translator *i18n.Translator, user *view_model.NotifyUser, code *es_model.InitUserCode, systemDefaults systemdefaults.SystemDefaults, alg crypto.EncryptionAlgorithm, colors *iam_model.LabelPolicyView, apiDomain string) error { +func SendUserInitCode(mailhtml string, translator *i18n.Translator, user *view_model.NotifyUser, code *es_model.InitUserCode, systemDefaults systemdefaults.SystemDefaults, alg crypto.EncryptionAlgorithm, colors *query.LabelPolicy, apiDomain string) error { codeString, err := crypto.DecryptString(code.Code, alg) if err != nil { return err @@ -35,7 +35,7 @@ func SendUserInitCode(mailhtml string, translator *i18n.Translator, user *view_m args["Code"] = codeString initCodeData := &InitCodeEmailData{ - TemplateData: templates.GetTemplateData(translator, args, apiDomain, url, domain.InitCodeMessageType, user.PreferredLanguage, colors), + TemplateData: GetTemplateData(translator, args, apiDomain, url, domain.InitCodeMessageType, user.PreferredLanguage, colors), URL: url, } template, err := templates.GetParsedTemplate(mailhtml, initCodeData) diff --git a/internal/notification/types/password_code.go b/internal/notification/types/password_code.go index 04a8828fcd..2a052b9c95 100644 --- a/internal/notification/types/password_code.go +++ b/internal/notification/types/password_code.go @@ -5,8 +5,8 @@ import ( "github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/i18n" - iam_model "github.com/caos/zitadel/internal/iam/model" "github.com/caos/zitadel/internal/notification/templates" + "github.com/caos/zitadel/internal/query" es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model" view_model "github.com/caos/zitadel/internal/user/repository/view/model" ) @@ -18,7 +18,7 @@ type PasswordCodeData struct { URL string } -func SendPasswordCode(mailhtml string, translator *i18n.Translator, user *view_model.NotifyUser, code *es_model.PasswordCode, systemDefaults systemdefaults.SystemDefaults, alg crypto.EncryptionAlgorithm, colors *iam_model.LabelPolicyView, apiDomain string) error { +func SendPasswordCode(mailhtml string, translator *i18n.Translator, user *view_model.NotifyUser, code *es_model.PasswordCode, systemDefaults systemdefaults.SystemDefaults, alg crypto.EncryptionAlgorithm, colors *query.LabelPolicy, apiDomain string) error { codeString, err := crypto.DecryptString(code.Code, alg) if err != nil { return err @@ -31,7 +31,7 @@ func SendPasswordCode(mailhtml string, translator *i18n.Translator, user *view_m args["Code"] = codeString passwordResetData := &PasswordCodeData{ - TemplateData: templates.GetTemplateData(translator, args, apiDomain, url, domain.PasswordResetMessageType, user.PreferredLanguage, colors), + TemplateData: GetTemplateData(translator, args, apiDomain, url, domain.PasswordResetMessageType, user.PreferredLanguage, colors), FirstName: user.FirstName, LastName: user.LastName, URL: url, diff --git a/internal/notification/types/passwordless_registration_link.go b/internal/notification/types/passwordless_registration_link.go index 2485e654bc..b95eea1841 100644 --- a/internal/notification/types/passwordless_registration_link.go +++ b/internal/notification/types/passwordless_registration_link.go @@ -5,8 +5,8 @@ import ( "github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/i18n" - iam_model "github.com/caos/zitadel/internal/iam/model" "github.com/caos/zitadel/internal/notification/templates" + "github.com/caos/zitadel/internal/query" "github.com/caos/zitadel/internal/repository/user" view_model "github.com/caos/zitadel/internal/user/repository/view/model" ) @@ -16,7 +16,7 @@ type PasswordlessRegistrationLinkData struct { URL string } -func SendPasswordlessRegistrationLink(mailhtml string, translator *i18n.Translator, user *view_model.NotifyUser, code *user.HumanPasswordlessInitCodeRequestedEvent, systemDefaults systemdefaults.SystemDefaults, alg crypto.EncryptionAlgorithm, colors *iam_model.LabelPolicyView, apiDomain string) error { +func SendPasswordlessRegistrationLink(mailhtml string, translator *i18n.Translator, user *view_model.NotifyUser, code *user.HumanPasswordlessInitCodeRequestedEvent, systemDefaults systemdefaults.SystemDefaults, alg crypto.EncryptionAlgorithm, colors *query.LabelPolicy, apiDomain string) error { codeString, err := crypto.DecryptString(code.Code, alg) if err != nil { return err @@ -25,7 +25,7 @@ func SendPasswordlessRegistrationLink(mailhtml string, translator *i18n.Translat var args = mapNotifyUserToArgs(user) emailCodeData := &PasswordlessRegistrationLinkData{ - TemplateData: templates.GetTemplateData(translator, args, apiDomain, url, domain.PasswordlessRegistrationMessageType, user.PreferredLanguage, colors), + TemplateData: GetTemplateData(translator, args, apiDomain, url, domain.PasswordlessRegistrationMessageType, user.PreferredLanguage, colors), URL: url, } diff --git a/internal/notification/types/templateData.go b/internal/notification/types/templateData.go new file mode 100644 index 0000000000..a8f0e0a99b --- /dev/null +++ b/internal/notification/types/templateData.go @@ -0,0 +1,46 @@ +package types + +import ( + "fmt" + "strings" + + "github.com/caos/zitadel/internal/i18n" + "github.com/caos/zitadel/internal/notification/templates" + "github.com/caos/zitadel/internal/query" +) + +func GetTemplateData(translator *i18n.Translator, translateArgs map[string]interface{}, apiDomain, href, msgType, lang string, policy *query.LabelPolicy) templates.TemplateData { + templateData := templates.TemplateData{ + Href: href, + PrimaryColor: templates.DefaultPrimaryColor, + BackgroundColor: templates.DefaultBackgroundColor, + FontColor: templates.DefaultFontColor, + LogoURL: templates.DefaultLogo, + FontURL: templates.DefaultFont, + FontFamily: templates.DefaultFontFamily, + IncludeFooter: false, + } + templateData.Translate(translator, msgType, translateArgs, lang) + if policy.Light.PrimaryColor != "" { + templateData.PrimaryColor = policy.Light.PrimaryColor + } + if policy.Light.BackgroundColor != "" { + templateData.BackgroundColor = policy.Light.BackgroundColor + } + if policy.Light.FontColor != "" { + templateData.FontColor = policy.Light.FontColor + } + if apiDomain == "" { + return templateData + } + templateData.LogoURL = "" + if policy.Light.LogoURL != "" { + templateData.LogoURL = fmt.Sprintf("%s/assets/v1/%s/%s", apiDomain, policy.ID, policy.Light.LogoURL) + } + if policy.FontURL != "" { + split := strings.Split(policy.FontURL, "/") + templateData.FontFamily = split[len(split)-1] + "," + templates.DefaultFontFamily + templateData.FontURL = fmt.Sprintf("%s/assets/v1/%s/%s", apiDomain, policy.ID, policy.FontURL) + } + return templateData +}