diff --git a/api/restHandler/NotificationRestHandler.go b/api/restHandler/NotificationRestHandler.go index 067dd7eb43..6ba1b93dcf 100644 --- a/api/restHandler/NotificationRestHandler.go +++ b/api/restHandler/NotificationRestHandler.go @@ -52,6 +52,7 @@ const ( type NotificationRestHandler interface { SaveNotificationSettings(w http.ResponseWriter, r *http.Request) + SaveNotificationSettingsV2(w http.ResponseWriter, r *http.Request) UpdateNotificationSettings(w http.ResponseWriter, r *http.Request) SaveNotificationChannelConfig(w http.ResponseWriter, r *http.Request) FindSESConfig(w http.ResponseWriter, r *http.Request) @@ -117,6 +118,7 @@ func NewNotificationRestHandlerImpl(dockerRegistryConfig pipeline.DockerRegistry } } +// SaveNotificationSettings will be deprecated in future func (impl NotificationRestHandlerImpl) SaveNotificationSettings(w http.ResponseWriter, r *http.Request) { userId, err := impl.userAuthService.GetLoggedInUser(r) if userId == 0 || err != nil { @@ -146,6 +148,65 @@ func (impl NotificationRestHandlerImpl) SaveNotificationSettings(w http.Response } //RBAC + providers := notificationSetting.Providers + + if len(providers) != 0 { + for _, provider := range providers { + if provider.Destination == util.SMTP || provider.Destination == util.SES { + if provider.Recipient == "" { + userEmail, err := impl.userAuthService.GetEmailById(int32(provider.ConfigId)) + if err != nil { + impl.logger.Errorw("service err, SaveNotificationSettings", "err", err, "payload", notificationSetting) + common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) + return + } + provider.Recipient = userEmail + } + // get default configID for SES and SMTP + provider.ConfigId = notificationSetting.SesConfigId + } + } + } + + res, err := impl.notificationService.CreateOrUpdateNotificationSettings(¬ificationSetting, userId) + if err != nil { + impl.logger.Errorw("service err, SaveNotificationSettings", "err", err, "payload", notificationSetting) + common.WriteJsonResp(w, err, nil, http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "application/json") + common.WriteJsonResp(w, nil, res, http.StatusOK) +} + +func (impl NotificationRestHandlerImpl) SaveNotificationSettingsV2(w http.ResponseWriter, r *http.Request) { + userId, err := impl.userAuthService.GetLoggedInUser(r) + if userId == 0 || err != nil { + common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) + return + } + var notificationSetting beans.NotificationRequest + err = json.NewDecoder(r.Body).Decode(¬ificationSetting) + if err != nil { + impl.logger.Errorw("request err, SaveNotificationSettings", "err", err, "payload", notificationSetting) + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) + return + } + impl.logger.Infow("request payload, SaveNotificationSettings", "err", err, "payload", notificationSetting) + err = impl.validator.Struct(notificationSetting) + if err != nil { + impl.logger.Errorw("validation err, SaveNotificationSettings", "err", err, "payload", notificationSetting) + common.WriteJsonResp(w, err, nil, http.StatusBadRequest) + return + } + + //RBAC + token := r.Header.Get("token") + if isSuperAdmin := impl.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); !isSuperAdmin { + common.WriteJsonResp(w, err, nil, http.StatusForbidden) + return + } + //RBAC + res, err := impl.notificationService.CreateOrUpdateNotificationSettings(¬ificationSetting, userId) if err != nil { impl.logger.Errorw("service err, SaveNotificationSettings", "err", err, "payload", notificationSetting) diff --git a/api/router/NotificationRouter.go b/api/router/NotificationRouter.go index 68e0bc5959..f0de1d33f3 100644 --- a/api/router/NotificationRouter.go +++ b/api/router/NotificationRouter.go @@ -32,9 +32,14 @@ func NewNotificationRouterImpl(notificationRestHandler restHandler.NotificationR return &NotificationRouterImpl{notificationRestHandler: notificationRestHandler} } func (impl NotificationRouterImpl) InitNotificationRegRouter(configRouter *mux.Router) { + // to maintain backward compatibility, will be deprecated in future configRouter.Path(""). HandlerFunc(impl.notificationRestHandler.SaveNotificationSettings). Methods("POST") + // new router to save notification settings + configRouter.Path("/v2"). + HandlerFunc(impl.notificationRestHandler.SaveNotificationSettingsV2). + Methods("POST") configRouter.Path(""). HandlerFunc(impl.notificationRestHandler.UpdateNotificationSettings). Methods("PUT") diff --git a/pkg/notifier/beans/beans.go b/pkg/notifier/beans/beans.go index 5fc07dc32b..24f9b75c94 100644 --- a/pkg/notifier/beans/beans.go +++ b/pkg/notifier/beans/beans.go @@ -226,6 +226,8 @@ type NotificationRequest struct { UpdateType util.UpdateType `json:"updateType,omitempty"` Providers []*bean.Provider `json:"providers"` NotificationConfigRequest []*NotificationConfigRequest `json:"notificationConfigRequest" validate:"required"` + // will be deprecated in future + SesConfigId int `json:"sesConfigId"` } type NotificationUpdateRequest struct {