mirror of
https://github.com/zitadel/zitadel.git
synced 2025-01-09 16:23:41 +00:00
fix: get email texts with default language (#1238)
This commit is contained in:
parent
4b9afc43e2
commit
a69f7d69d5
@ -1,5 +1,5 @@
|
|||||||
SystemDefaults:
|
SystemDefaults:
|
||||||
DefaultLanguage: 'de'
|
DefaultLanguage: 'en'
|
||||||
Domain: $ZITADEL_DEFAULT_DOMAIN
|
Domain: $ZITADEL_DEFAULT_DOMAIN
|
||||||
ZitadelDocs:
|
ZitadelDocs:
|
||||||
Issuer: $ZITADEL_ISSUER
|
Issuer: $ZITADEL_ISSUER
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
global_model "github.com/caos/zitadel/internal/model"
|
global_model "github.com/caos/zitadel/internal/model"
|
||||||
"github.com/caos/zitadel/internal/view/repository"
|
"github.com/caos/zitadel/internal/view/repository"
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMailTexts(db *gorm.DB, table string, aggregateID string) ([]*model.MailTextView, error) {
|
func GetMailTexts(db *gorm.DB, table string, aggregateID string) ([]*model.MailTextView, error) {
|
||||||
@ -30,7 +31,7 @@ func GetMailTextByIDs(db *gorm.DB, table, aggregateID string, textType string, l
|
|||||||
mailText := new(model.MailTextView)
|
mailText := new(model.MailTextView)
|
||||||
aggregateIDQuery := &model.MailTextSearchQuery{Key: iam_model.MailTextSearchKeyAggregateID, Value: aggregateID, Method: global_model.SearchMethodEquals}
|
aggregateIDQuery := &model.MailTextSearchQuery{Key: iam_model.MailTextSearchKeyAggregateID, Value: aggregateID, Method: global_model.SearchMethodEquals}
|
||||||
textTypeQuery := &model.MailTextSearchQuery{Key: iam_model.MailTextSearchKeyMailTextType, Value: textType, Method: global_model.SearchMethodEquals}
|
textTypeQuery := &model.MailTextSearchQuery{Key: iam_model.MailTextSearchKeyMailTextType, Value: textType, Method: global_model.SearchMethodEquals}
|
||||||
languageQuery := &model.MailTextSearchQuery{Key: iam_model.MailTextSearchKeyLanguage, Value: language, Method: global_model.SearchMethodEquals}
|
languageQuery := &model.MailTextSearchQuery{Key: iam_model.MailTextSearchKeyLanguage, Value: strings.ToUpper(language), Method: global_model.SearchMethodEquals}
|
||||||
query := repository.PrepareGetByQuery(table, aggregateIDQuery, textTypeQuery, languageQuery)
|
query := repository.PrepareGetByQuery(table, aggregateIDQuery, textTypeQuery, languageQuery)
|
||||||
err := query(db, mailText)
|
err := query(db, mailText)
|
||||||
if caos_errs.IsNotFound(err) {
|
if caos_errs.IsNotFound(err) {
|
||||||
|
@ -3,6 +3,9 @@ package handler
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/caos/zitadel/internal/user/model"
|
||||||
|
model2 "github.com/caos/zitadel/internal/user/repository/view/model"
|
||||||
|
"golang.org/x/text/language"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -150,12 +153,12 @@ func (n *Notification) handleInitUserCode(event *models.Event) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := n.view.NotifyUserByID(event.AggregateID)
|
user, err := n.getUserByID(event.AggregateID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := n.getMailText(context.Background(), mailTextTypeInitCode, user.PreferredLanguage[len(user.PreferredLanguage)-2:])
|
text, err := n.getMailText(context.Background(), mailTextTypeInitCode, user.PreferredLanguage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -189,12 +192,12 @@ func (n *Notification) handlePasswordCode(event *models.Event) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := n.view.NotifyUserByID(event.AggregateID)
|
user, err := n.getUserByID(event.AggregateID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := n.getMailText(context.Background(), mailTextTypePasswordReset, user.PreferredLanguage[len(user.PreferredLanguage)-2:])
|
text, err := n.getMailText(context.Background(), mailTextTypePasswordReset, user.PreferredLanguage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -227,12 +230,12 @@ func (n *Notification) handleEmailVerificationCode(event *models.Event) (err err
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
user, err := n.view.NotifyUserByID(event.AggregateID)
|
user, err := n.getUserByID(event.AggregateID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := n.getMailText(context.Background(), mailTextTypeVerifyEmail, user.PreferredLanguage[len(user.PreferredLanguage)-2:])
|
text, err := n.getMailText(context.Background(), mailTextTypeVerifyEmail, user.PreferredLanguage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -255,7 +258,7 @@ func (n *Notification) handlePhoneVerificationCode(event *models.Event) (err err
|
|||||||
if err != nil || alreadyHandled {
|
if err != nil || alreadyHandled {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
user, err := n.view.NotifyUserByID(event.AggregateID)
|
user, err := n.getUserByID(event.AggregateID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -276,7 +279,7 @@ func (n *Notification) handleDomainClaimed(event *models.Event) (err error) {
|
|||||||
logging.Log("HANDLE-Gghq2").WithError(err).Error("could not unmarshal event data")
|
logging.Log("HANDLE-Gghq2").WithError(err).Error("could not unmarshal event data")
|
||||||
return caos_errs.ThrowInternal(err, "HANDLE-7hgj3", "could not unmarshal event")
|
return caos_errs.ThrowInternal(err, "HANDLE-7hgj3", "could not unmarshal event")
|
||||||
}
|
}
|
||||||
user, err := n.view.NotifyUserByID(event.AggregateID)
|
user, err := n.getUserByID(event.AggregateID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -290,7 +293,7 @@ func (n *Notification) handleDomainClaimed(event *models.Event) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
text, err := n.getMailText(context.Background(), mailTextTypeDomainClaimed, user.PreferredLanguage[len(user.PreferredLanguage)-2:])
|
text, err := n.getMailText(context.Background(), mailTextTypeDomainClaimed, user.PreferredLanguage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -382,12 +385,17 @@ func (n *Notification) getMailTemplate(ctx context.Context) (*iam_model.MailTemp
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read organization specific texts
|
// Read organization specific texts
|
||||||
func (n *Notification) getMailText(ctx context.Context, textType string, language string) (*iam_model.MailTextView, error) {
|
func (n *Notification) getMailText(ctx context.Context, textType string, lang string) (*iam_model.MailTextView, error) {
|
||||||
|
langTag := language.Make(lang)
|
||||||
|
if langTag == language.Und {
|
||||||
|
langTag = n.systemDefaults.DefaultLanguage
|
||||||
|
}
|
||||||
|
base, _ := langTag.Base()
|
||||||
// read from Org
|
// read from Org
|
||||||
mailText, err := n.view.MailTextByIDs(authz.GetCtxData(ctx).OrgID, textType, language, mailTextTableOrg)
|
mailText, err := n.view.MailTextByIDs(authz.GetCtxData(ctx).OrgID, textType, base.String(), mailTextTableOrg)
|
||||||
if errors.IsNotFound(err) {
|
if errors.IsNotFound(err) {
|
||||||
// read from default
|
// read from default
|
||||||
mailText, err = n.view.MailTextByIDs(n.systemDefaults.IamID, textType, language, mailTextTableDef)
|
mailText, err = n.view.MailTextByIDs(n.systemDefaults.IamID, textType, base.String(), mailTextTableDef)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -398,3 +406,27 @@ func (n *Notification) getMailText(ctx context.Context, textType string, languag
|
|||||||
}
|
}
|
||||||
return iam_es_model.MailTextViewToModel(mailText), err
|
return iam_es_model.MailTextViewToModel(mailText), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Notification) getUserByID(userID string) (*model2.NotifyUser, error) {
|
||||||
|
user, usrErr := n.view.NotifyUserByID(userID)
|
||||||
|
if usrErr != nil && !caos_errs.IsNotFound(usrErr) {
|
||||||
|
return nil, usrErr
|
||||||
|
}
|
||||||
|
if user == nil {
|
||||||
|
user = &model2.NotifyUser{}
|
||||||
|
}
|
||||||
|
events, err := n.getUserEvents(userID, user.Sequence)
|
||||||
|
if err != nil {
|
||||||
|
return user, usrErr
|
||||||
|
}
|
||||||
|
userCopy := *user
|
||||||
|
for _, event := range events {
|
||||||
|
if err := userCopy.AppendEvent(event); err != nil {
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if userCopy.State == int32(model.UserStateDeleted) {
|
||||||
|
return nil, caos_errs.ThrowNotFound(nil, "HANDLER-3n8fs", "Errors.User.NotFound")
|
||||||
|
}
|
||||||
|
return &userCopy, nil
|
||||||
|
}
|
||||||
|
@ -39,6 +39,7 @@ type NotifyUser struct {
|
|||||||
VerifiedPhone string `json:"-" gorm:"column:verified_phone"`
|
VerifiedPhone string `json:"-" gorm:"column:verified_phone"`
|
||||||
PasswordSet bool `json:"-" gorm:"column:password_set"`
|
PasswordSet bool `json:"-" gorm:"column:password_set"`
|
||||||
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
Sequence uint64 `json:"-" gorm:"column:sequence"`
|
||||||
|
State int32 `json:"-" gorm:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NotifyUserFromModel(user *model.NotifyUser) *NotifyUser {
|
func NotifyUserFromModel(user *model.NotifyUser) *NotifyUser {
|
||||||
@ -144,6 +145,8 @@ func (u *NotifyUser) AppendEvent(event *models.Event) (err error) {
|
|||||||
case es_model.UserPasswordChanged,
|
case es_model.UserPasswordChanged,
|
||||||
es_model.HumanPasswordChanged:
|
es_model.HumanPasswordChanged:
|
||||||
err = u.setPasswordData(event)
|
err = u.setPasswordData(event)
|
||||||
|
case es_model.UserRemoved:
|
||||||
|
u.State = int32(UserStateDeleted)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user