zitadel/internal/iam/repository/view/model/message_text.go

160 lines
4.8 KiB
Go
Raw Normal View History

package model
import (
"time"
"golang.org/x/text/language"
"github.com/caos/zitadel/internal/domain"
org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
"github.com/caos/zitadel/internal/eventstore/v1/models"
"github.com/caos/zitadel/internal/iam/model"
)
const (
MessageTextKeyAggregateID = "aggregate_id"
MessageTextKeyMessageTextType = "message_text_type"
MessageTextKeyLanguage = "language"
)
type MessageTextView struct {
AggregateID string `json:"-" gorm:"column:aggregate_id;primary_key"`
CreationDate time.Time `json:"-" gorm:"column:creation_date"`
ChangeDate time.Time `json:"-" gorm:"column:change_date"`
State int32 `json:"-" gorm:"column:message_text_state"`
MessageTextType string `json:"-" gorm:"column:message_text_type;primary_key"`
Language string `json:"-" gorm:"column:language;primary_key"`
Title string `json:"-" gorm:"column:title"`
PreHeader string `json:"-" gorm:"column:pre_header"`
Subject string `json:"-" gorm:"column:subject"`
Greeting string `json:"-" gorm:"column:greeting"`
Text string `json:"-" gorm:"column:text"`
ButtonText string `json:"-" gorm:"column:button_text"`
FooterText string `json:"-" gorm:"column:footer_text"`
Default bool `json:"-" gorm:"-"`
Sequence uint64 `json:"-" gorm:"column:sequence"`
}
func MessageTextViewFromModel(template *model.MessageTextView) *MessageTextView {
return &MessageTextView{
AggregateID: template.AggregateID,
Sequence: template.Sequence,
CreationDate: template.CreationDate,
ChangeDate: template.ChangeDate,
MessageTextType: template.MessageTextType,
Language: template.Language.String(),
Title: template.Title,
PreHeader: template.PreHeader,
Subject: template.Subject,
Greeting: template.Greeting,
Text: template.Text,
ButtonText: template.ButtonText,
FooterText: template.FooterText,
Default: template.Default,
}
}
func messageTextsViewToModelArr(texts []*MessageTextView, defaultIn bool) []*model.MessageTextView {
result := make([]*model.MessageTextView, len(texts))
for i, r := range texts {
r.Default = defaultIn
result[i] = MessageTextViewToModel(r)
}
return result
}
func MessageTextViewToModel(template *MessageTextView) *model.MessageTextView {
lang := language.Make(template.Language)
return &model.MessageTextView{
AggregateID: template.AggregateID,
Sequence: template.Sequence,
CreationDate: template.CreationDate,
ChangeDate: template.ChangeDate,
MessageTextType: template.MessageTextType,
Language: lang,
Title: template.Title,
PreHeader: template.PreHeader,
Subject: template.Subject,
Greeting: template.Greeting,
Text: template.Text,
ButtonText: template.ButtonText,
FooterText: template.FooterText,
Default: template.Default,
}
}
func (i *MessageTextView) AppendEvent(event *models.Event) (err error) {
i.Sequence = event.Sequence
switch event.Type {
case es_model.CustomTextSet, org_es_model.CustomTextSet:
i.setRootData(event)
feat: Custom text login (#1925) * feat: default custom message text * feat: org custom message text * feat: org custom message text * feat: custom messages query side * feat: default messages * feat: message text user fields * feat: check for inactive user * feat: fix send password reset * feat: fix custom org text * feat: add variables to docs * feat: custom text tests * feat: fix notifications * feat: add custom text feature * feat: add custom text feature * feat: feature in custom message texts * feat: add custom text feature in frontend * feat: merge main * feat: feature tests * feat: change phone message in setup * fix: remove unused code, add event translation * fix: merge main and fix problems * fix: english translation file * fix: migration versions * fix: setup * fix: custom login text * feat: add all possible custom texts for login * feat: iam login texts * feat: org login texts * feat: protos * fix: custom text in admin api * fix: add success login text * fix: docs * fix: add custom login texts to management api * fix: add sub messages to custom login texts * fix: setup custom texts * feat: get org login texts * feat: get org login texts * feat: handler in adminapi * feat: handlers in auth and admin * feat: render login texts * feat: custom login text * feat: add all login text keys * feat: handle correct login texts * feat: custom login texts in command side * feat: custom login texts in command side * feat: fix yaml file * feat: merge master and add confirmation text * feat: fix html * feat: read default login texts * feat: get default text files * feat: get custom texts org * feat: tests * feat: change translator handling * fix translator from authReq * feat: change h1 on login screens * feat: add custom login text for remove * feat: add custom login text for remove * feat: cache translation files * feat: cache translation files * feat: zitadel user in env var * feat: add registration user description * feat: better func naming * feat: tests * feat: add mutex to read file * feat: add mutex to read file * fix mutex for accessing translation map * fix: translation key Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2021-07-05 13:10:49 +00:00
customText := new(CustomTextView)
err = customText.SetData(event)
if err != nil {
return err
}
if customText.Key == domain.MessageTitle {
i.Title = customText.Text
}
if customText.Key == domain.MessagePreHeader {
i.PreHeader = customText.Text
}
if customText.Key == domain.MessageSubject {
i.Subject = customText.Text
}
if customText.Key == domain.MessageGreeting {
i.Greeting = customText.Text
}
if customText.Key == domain.MessageText {
i.Text = customText.Text
}
if customText.Key == domain.MessageButtonText {
i.ButtonText = customText.Text
}
if customText.Key == domain.MessageFooterText {
i.FooterText = customText.Text
}
i.ChangeDate = event.CreationDate
case es_model.CustomTextRemoved, org_es_model.CustomTextRemoved:
feat: Custom text login (#1925) * feat: default custom message text * feat: org custom message text * feat: org custom message text * feat: custom messages query side * feat: default messages * feat: message text user fields * feat: check for inactive user * feat: fix send password reset * feat: fix custom org text * feat: add variables to docs * feat: custom text tests * feat: fix notifications * feat: add custom text feature * feat: add custom text feature * feat: feature in custom message texts * feat: add custom text feature in frontend * feat: merge main * feat: feature tests * feat: change phone message in setup * fix: remove unused code, add event translation * fix: merge main and fix problems * fix: english translation file * fix: migration versions * fix: setup * fix: custom login text * feat: add all possible custom texts for login * feat: iam login texts * feat: org login texts * feat: protos * fix: custom text in admin api * fix: add success login text * fix: docs * fix: add custom login texts to management api * fix: add sub messages to custom login texts * fix: setup custom texts * feat: get org login texts * feat: get org login texts * feat: handler in adminapi * feat: handlers in auth and admin * feat: render login texts * feat: custom login text * feat: add all login text keys * feat: handle correct login texts * feat: custom login texts in command side * feat: custom login texts in command side * feat: fix yaml file * feat: merge master and add confirmation text * feat: fix html * feat: read default login texts * feat: get default text files * feat: get custom texts org * feat: tests * feat: change translator handling * fix translator from authReq * feat: change h1 on login screens * feat: add custom login text for remove * feat: add custom login text for remove * feat: cache translation files * feat: cache translation files * feat: zitadel user in env var * feat: add registration user description * feat: better func naming * feat: tests * feat: add mutex to read file * feat: add mutex to read file * fix mutex for accessing translation map * fix: translation key Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2021-07-05 13:10:49 +00:00
customText := new(CustomTextView)
err = customText.SetData(event)
if err != nil {
return err
}
if customText.Key == domain.MessageTitle {
i.Title = ""
}
if customText.Key == domain.MessagePreHeader {
i.PreHeader = ""
}
if customText.Key == domain.MessageSubject {
i.Subject = ""
}
if customText.Key == domain.MessageGreeting {
i.Greeting = ""
}
if customText.Key == domain.MessageText {
i.Text = ""
}
if customText.Key == domain.MessageButtonText {
i.ButtonText = ""
}
if customText.Key == domain.MessageFooterText {
i.FooterText = ""
}
i.ChangeDate = event.CreationDate
case org_es_model.CustomTextMessageRemoved:
i.State = int32(model.PolicyStateRemoved)
}
return err
}
func (r *MessageTextView) setRootData(event *models.Event) {
r.AggregateID = event.AggregateID
}