Merge pull request #9980 from rainzm/automated-cherry-pick-of-#9979-upstream-release-3.7

Automated cherry pick of #9979: Send SMS notification for abnormal login
This commit is contained in:
Zexi Li
2021-01-21 21:20:55 +08:00
committed by GitHub
2 changed files with 35 additions and 1 deletions

View File

@@ -222,6 +222,7 @@ func NotifyWithTag(ctx context.Context, params SNotifyParams) {
}
notifyWithChannel(ctx, p,
npk.NotifyByEmail,
npk.NotifyByMobile,
npk.NotifyByDingTalk,
npk.NotifyByFeishu,
npk.NotifyByWorkwx,

View File

@@ -256,7 +256,7 @@ func (tm *STemplateManager) NotifyFilter(contactType, topic, msg, lang string) (
err = errors.Wrap(err, "db.FetchModelObjects")
return
}
for _, template := range templates {
for _, template := range tm.chooseTemplate(contactType, templates) {
var title, content string
switch template.TemplateType {
case api.TEMPLATE_TYPE_TITLE:
@@ -282,6 +282,39 @@ func (tm *STemplateManager) NotifyFilter(contactType, topic, msg, lang string) (
return
}
func (tm *STemplateManager) chooseTemplate(contactType string, tempaltes []STemplate) []*STemplate {
var titleTemplate, contentTemplate *STemplate
// contactType first
for i := range tempaltes {
switch tempaltes[i].TemplateType {
case api.TEMPLATE_TYPE_REMOTE:
if tempaltes[i].ContactType == contactType {
return []*STemplate{&tempaltes[i]}
}
case api.TEMPLATE_TYPE_TITLE:
if tempaltes[i].ContactType == contactType {
titleTemplate = &tempaltes[i]
} else if titleTemplate == nil {
titleTemplate = &tempaltes[i]
}
case api.TEMPLATE_TYPE_CONTENT:
if tempaltes[i].ContactType == contactType {
contentTemplate = &tempaltes[i]
} else if contentTemplate == nil {
contentTemplate = &tempaltes[i]
}
}
}
ret := make([]*STemplate, 0, 2)
if titleTemplate != nil {
ret = append(ret, titleTemplate)
}
if contentTemplate != nil {
ret = append(ret, contentTemplate)
}
return ret
}
func (tm *STemplate) Execute(str string) (string, error) {
tem, err := ptem.New("tmp").Parse(tm.Content)
if err != nil {