Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions api/restHandler/NotificationRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(&notificationSetting, 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(&notificationSetting)
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(&notificationSetting, userId)
if err != nil {
impl.logger.Errorw("service err, SaveNotificationSettings", "err", err, "payload", notificationSetting)
Expand Down
5 changes: 5 additions & 0 deletions api/router/NotificationRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions pkg/notifier/beans/beans.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading