fix: Get Label policy in notifications (#2923)

This commit is contained in:
Fabi 2022-01-04 09:35:22 +01:00 committed by GitHub
parent 5640a0ef33
commit 6d78fe28f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 72 additions and 93 deletions

View File

@ -18,8 +18,6 @@ import (
queryv1 "github.com/caos/zitadel/internal/eventstore/v1/query" queryv1 "github.com/caos/zitadel/internal/eventstore/v1/query"
"github.com/caos/zitadel/internal/eventstore/v1/spooler" "github.com/caos/zitadel/internal/eventstore/v1/spooler"
"github.com/caos/zitadel/internal/i18n" "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/notification/types"
"github.com/caos/zitadel/internal/query" "github.com/caos/zitadel/internal/query"
user_repo "github.com/caos/zitadel/internal/repository/user" user_repo "github.com/caos/zitadel/internal/repository/user"
@ -31,8 +29,6 @@ import (
const ( const (
notificationTable = "notification.notifications" notificationTable = "notification.notifications"
NotifyUserID = "NOTIFICATION" NotifyUserID = "NOTIFICATION"
labelPolicyTableOrg = "management.label_policies"
labelPolicyTableDef = "adminapi.label_policies"
) )
type Notification struct { type Notification struct {
@ -404,38 +400,13 @@ func getSetNotifyContextData(orgID string) context.Context {
} }
// Read organization specific colors // Read organization specific colors
func (n *Notification) getLabelPolicy(ctx context.Context) (*iam_model.LabelPolicyView, error) { func (n *Notification) getLabelPolicy(ctx context.Context) (*query.LabelPolicy, error) {
// read from Org return n.queries.ActiveLabelPolicyByOrg(ctx, authz.GetCtxData(ctx).OrgID)
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
} }
// Read organization specific template // Read organization specific template
func (n *Notification) getMailTemplate(ctx context.Context) (*query.MailTemplate, error) { func (n *Notification) getMailTemplate(ctx context.Context) (*query.MailTemplate, error) {
// read from Org return n.queries.MailTemplateByOrg(ctx, authz.GetCtxData(ctx).OrgID)
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
} }
func (n *Notification) getTranslatorWithOrgTexts(orgID, textType string) (*i18n.Translator, error) { func (n *Notification) getTranslatorWithOrgTexts(orgID, textType string) (*i18n.Translator, error) {

View File

@ -3,20 +3,18 @@ package templates
import ( import (
"fmt" "fmt"
"html" "html"
"strings"
"github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/i18n" "github.com/caos/zitadel/internal/i18n"
iam_model "github.com/caos/zitadel/internal/iam/model"
) )
const ( const (
defaultFont = "http://fonts.googleapis.com/css?family=Lato:200,300,400,600" DefaultFont = "http://fonts.googleapis.com/css?family=Lato:200,300,400,600"
defaultFontFamily = "-apple-system, BlinkMacSystemFont, Segoe UI, Lato, Arial, Helvetica, sans-serif" DefaultFontFamily = "-apple-system, BlinkMacSystemFont, Segoe UI, Lato, Arial, Helvetica, sans-serif"
defaultLogo = "https://static.zitadel.ch/zitadel-logo-dark@3x.png" DefaultLogo = "https://static.zitadel.ch/zitadel-logo-dark@3x.png"
defaultFontColor = "#22292f" DefaultFontColor = "#22292f"
defaultBackgroundColor = "#fafafa" DefaultBackgroundColor = "#fafafa"
defaultPrimaryColor = "#5282C1" DefaultPrimaryColor = "#5282C1"
) )
type TemplateData struct { 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.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...) 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
}

View File

@ -6,8 +6,8 @@ import (
"github.com/caos/zitadel/internal/config/systemdefaults" "github.com/caos/zitadel/internal/config/systemdefaults"
"github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/i18n" "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/notification/templates"
"github.com/caos/zitadel/internal/query"
view_model "github.com/caos/zitadel/internal/user/repository/view/model" view_model "github.com/caos/zitadel/internal/user/repository/view/model"
) )
@ -16,7 +16,7 @@ type DomainClaimedData struct {
URL string 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}) url, err := templates.ParseTemplateText(systemDefaults.Notifications.Endpoints.DomainClaimed, &UrlData{UserID: user.ID})
if err != nil { if err != nil {
return err return err
@ -26,7 +26,7 @@ func SendDomainClaimed(mailhtml string, translator *i18n.Translator, user *view_
args["Domain"] = strings.Split(user.LastEmail, "@")[1] args["Domain"] = strings.Split(user.LastEmail, "@")[1]
domainClaimedData := &DomainClaimedData{ 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, URL: url,
} }
template, err := templates.GetParsedTemplate(mailhtml, domainClaimedData) template, err := templates.GetParsedTemplate(mailhtml, domainClaimedData)

View File

@ -5,8 +5,8 @@ import (
"github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/i18n" "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/notification/templates"
"github.com/caos/zitadel/internal/query"
es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model" es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
view_model "github.com/caos/zitadel/internal/user/repository/view/model" view_model "github.com/caos/zitadel/internal/user/repository/view/model"
) )
@ -16,7 +16,7 @@ type EmailVerificationCodeData struct {
URL string 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) codeString, err := crypto.DecryptString(code.Code, alg)
if err != nil { if err != nil {
return err return err
@ -30,7 +30,7 @@ func SendEmailVerificationCode(mailhtml string, translator *i18n.Translator, use
args["Code"] = codeString args["Code"] = codeString
emailCodeData := &EmailVerificationCodeData{ 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, URL: url,
} }

View File

@ -5,8 +5,8 @@ import (
"github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/i18n" "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/notification/templates"
"github.com/caos/zitadel/internal/query"
es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model" es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
view_model "github.com/caos/zitadel/internal/user/repository/view/model" view_model "github.com/caos/zitadel/internal/user/repository/view/model"
) )
@ -22,7 +22,7 @@ type UrlData struct {
PasswordSet bool 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) codeString, err := crypto.DecryptString(code.Code, alg)
if err != nil { if err != nil {
return err return err
@ -35,7 +35,7 @@ func SendUserInitCode(mailhtml string, translator *i18n.Translator, user *view_m
args["Code"] = codeString args["Code"] = codeString
initCodeData := &InitCodeEmailData{ 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, URL: url,
} }
template, err := templates.GetParsedTemplate(mailhtml, initCodeData) template, err := templates.GetParsedTemplate(mailhtml, initCodeData)

View File

@ -5,8 +5,8 @@ import (
"github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/i18n" "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/notification/templates"
"github.com/caos/zitadel/internal/query"
es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model" es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
view_model "github.com/caos/zitadel/internal/user/repository/view/model" view_model "github.com/caos/zitadel/internal/user/repository/view/model"
) )
@ -18,7 +18,7 @@ type PasswordCodeData struct {
URL string 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) codeString, err := crypto.DecryptString(code.Code, alg)
if err != nil { if err != nil {
return err return err
@ -31,7 +31,7 @@ func SendPasswordCode(mailhtml string, translator *i18n.Translator, user *view_m
args["Code"] = codeString args["Code"] = codeString
passwordResetData := &PasswordCodeData{ 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, FirstName: user.FirstName,
LastName: user.LastName, LastName: user.LastName,
URL: url, URL: url,

View File

@ -5,8 +5,8 @@ import (
"github.com/caos/zitadel/internal/crypto" "github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/i18n" "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/notification/templates"
"github.com/caos/zitadel/internal/query"
"github.com/caos/zitadel/internal/repository/user" "github.com/caos/zitadel/internal/repository/user"
view_model "github.com/caos/zitadel/internal/user/repository/view/model" view_model "github.com/caos/zitadel/internal/user/repository/view/model"
) )
@ -16,7 +16,7 @@ type PasswordlessRegistrationLinkData struct {
URL string 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) codeString, err := crypto.DecryptString(code.Code, alg)
if err != nil { if err != nil {
return err return err
@ -25,7 +25,7 @@ func SendPasswordlessRegistrationLink(mailhtml string, translator *i18n.Translat
var args = mapNotifyUserToArgs(user) var args = mapNotifyUserToArgs(user)
emailCodeData := &PasswordlessRegistrationLinkData{ 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, URL: url,
} }

View File

@ -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
}