Skip to content

Commit 8554d4c

Browse files
committed
backward compatibility for notification
1 parent ea98cd9 commit 8554d4c

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

api/restHandler/NotificationRestHandler.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const (
5252

5353
type NotificationRestHandler interface {
5454
SaveNotificationSettings(w http.ResponseWriter, r *http.Request)
55+
SaveNotificationSettingsV2(w http.ResponseWriter, r *http.Request)
5556
UpdateNotificationSettings(w http.ResponseWriter, r *http.Request)
5657
SaveNotificationChannelConfig(w http.ResponseWriter, r *http.Request)
5758
FindSESConfig(w http.ResponseWriter, r *http.Request)
@@ -146,6 +147,65 @@ func (impl NotificationRestHandlerImpl) SaveNotificationSettings(w http.Response
146147
}
147148
//RBAC
148149

150+
providers := notificationSetting.Providers
151+
152+
if len(providers) != 0 {
153+
for _, provider := range providers {
154+
if provider.Destination == "smtp" || provider.Destination == "ses" {
155+
if provider.Recipient == "" {
156+
userEmail, err := impl.userAuthService.GetEmailById(int32(provider.ConfigId))
157+
if err != nil {
158+
impl.logger.Errorw("service err, SaveNotificationSettings", "err", err, "payload", notificationSetting)
159+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
160+
return
161+
}
162+
provider.Recipient = userEmail
163+
}
164+
// get default configID for SES and SMTP
165+
provider.ConfigId = notificationSetting.SesConfigId
166+
}
167+
}
168+
}
169+
170+
res, err := impl.notificationService.CreateOrUpdateNotificationSettings(&notificationSetting, userId)
171+
if err != nil {
172+
impl.logger.Errorw("service err, SaveNotificationSettings", "err", err, "payload", notificationSetting)
173+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
174+
return
175+
}
176+
w.Header().Set("Content-Type", "application/json")
177+
common.WriteJsonResp(w, nil, res, http.StatusOK)
178+
}
179+
180+
func (impl NotificationRestHandlerImpl) SaveNotificationSettingsV2(w http.ResponseWriter, r *http.Request) {
181+
userId, err := impl.userAuthService.GetLoggedInUser(r)
182+
if userId == 0 || err != nil {
183+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
184+
return
185+
}
186+
var notificationSetting beans.NotificationRequest
187+
err = json.NewDecoder(r.Body).Decode(&notificationSetting)
188+
if err != nil {
189+
impl.logger.Errorw("request err, SaveNotificationSettings", "err", err, "payload", notificationSetting)
190+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
191+
return
192+
}
193+
impl.logger.Infow("request payload, SaveNotificationSettings", "err", err, "payload", notificationSetting)
194+
err = impl.validator.Struct(notificationSetting)
195+
if err != nil {
196+
impl.logger.Errorw("validation err, SaveNotificationSettings", "err", err, "payload", notificationSetting)
197+
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
198+
return
199+
}
200+
201+
//RBAC
202+
token := r.Header.Get("token")
203+
if isSuperAdmin := impl.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); !isSuperAdmin {
204+
common.WriteJsonResp(w, err, nil, http.StatusForbidden)
205+
return
206+
}
207+
//RBAC
208+
149209
res, err := impl.notificationService.CreateOrUpdateNotificationSettings(&notificationSetting, userId)
150210
if err != nil {
151211
impl.logger.Errorw("service err, SaveNotificationSettings", "err", err, "payload", notificationSetting)

api/router/NotificationRouter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ func NewNotificationRouterImpl(notificationRestHandler restHandler.NotificationR
3232
return &NotificationRouterImpl{notificationRestHandler: notificationRestHandler}
3333
}
3434
func (impl NotificationRouterImpl) InitNotificationRegRouter(configRouter *mux.Router) {
35+
// to maintain backward compatibility, will be deprecated in future
3536
configRouter.Path("").
3637
HandlerFunc(impl.notificationRestHandler.SaveNotificationSettings).
3738
Methods("POST")
39+
// new router to save notification settings
40+
configRouter.Path("/v2").
41+
HandlerFunc(impl.notificationRestHandler.SaveNotificationSettingsV2).
42+
Methods("POST")
3843
configRouter.Path("").
3944
HandlerFunc(impl.notificationRestHandler.UpdateNotificationSettings).
4045
Methods("PUT")

pkg/notifier/beans/beans.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ type NotificationRequest struct {
226226
UpdateType util.UpdateType `json:"updateType,omitempty"`
227227
Providers []*bean.Provider `json:"providers"`
228228
NotificationConfigRequest []*NotificationConfigRequest `json:"notificationConfigRequest" validate:"required"`
229+
SesConfigId int `json:"sesConfigId"`
229230
}
230231

231232
type NotificationUpdateRequest struct {

0 commit comments

Comments
 (0)