@@ -52,6 +52,7 @@ const (
52
52
53
53
type NotificationRestHandler interface {
54
54
SaveNotificationSettings (w http.ResponseWriter , r * http.Request )
55
+ SaveNotificationSettingsV2 (w http.ResponseWriter , r * http.Request )
55
56
UpdateNotificationSettings (w http.ResponseWriter , r * http.Request )
56
57
SaveNotificationChannelConfig (w http.ResponseWriter , r * http.Request )
57
58
FindSESConfig (w http.ResponseWriter , r * http.Request )
@@ -117,6 +118,7 @@ func NewNotificationRestHandlerImpl(dockerRegistryConfig pipeline.DockerRegistry
117
118
}
118
119
}
119
120
121
+ // SaveNotificationSettings will be deprecated in future
120
122
func (impl NotificationRestHandlerImpl ) SaveNotificationSettings (w http.ResponseWriter , r * http.Request ) {
121
123
userId , err := impl .userAuthService .GetLoggedInUser (r )
122
124
if userId == 0 || err != nil {
@@ -146,6 +148,65 @@ func (impl NotificationRestHandlerImpl) SaveNotificationSettings(w http.Response
146
148
}
147
149
//RBAC
148
150
151
+ providers := notificationSetting .Providers
152
+
153
+ if len (providers ) != 0 {
154
+ for _ , provider := range providers {
155
+ if provider .Destination == util .SMTP || provider .Destination == util .SES {
156
+ if provider .Recipient == "" {
157
+ userEmail , err := impl .userAuthService .GetEmailById (int32 (provider .ConfigId ))
158
+ if err != nil {
159
+ impl .logger .Errorw ("service err, SaveNotificationSettings" , "err" , err , "payload" , notificationSetting )
160
+ common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
161
+ return
162
+ }
163
+ provider .Recipient = userEmail
164
+ }
165
+ // get default configID for SES and SMTP
166
+ provider .ConfigId = notificationSetting .SesConfigId
167
+ }
168
+ }
169
+ }
170
+
171
+ res , err := impl .notificationService .CreateOrUpdateNotificationSettings (& notificationSetting , userId )
172
+ if err != nil {
173
+ impl .logger .Errorw ("service err, SaveNotificationSettings" , "err" , err , "payload" , notificationSetting )
174
+ common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
175
+ return
176
+ }
177
+ w .Header ().Set ("Content-Type" , "application/json" )
178
+ common .WriteJsonResp (w , nil , res , http .StatusOK )
179
+ }
180
+
181
+ func (impl NotificationRestHandlerImpl ) SaveNotificationSettingsV2 (w http.ResponseWriter , r * http.Request ) {
182
+ userId , err := impl .userAuthService .GetLoggedInUser (r )
183
+ if userId == 0 || err != nil {
184
+ common .WriteJsonResp (w , err , "Unauthorized User" , http .StatusUnauthorized )
185
+ return
186
+ }
187
+ var notificationSetting beans.NotificationRequest
188
+ err = json .NewDecoder (r .Body ).Decode (& notificationSetting )
189
+ if err != nil {
190
+ impl .logger .Errorw ("request err, SaveNotificationSettings" , "err" , err , "payload" , notificationSetting )
191
+ common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
192
+ return
193
+ }
194
+ impl .logger .Infow ("request payload, SaveNotificationSettings" , "err" , err , "payload" , notificationSetting )
195
+ err = impl .validator .Struct (notificationSetting )
196
+ if err != nil {
197
+ impl .logger .Errorw ("validation err, SaveNotificationSettings" , "err" , err , "payload" , notificationSetting )
198
+ common .WriteJsonResp (w , err , nil , http .StatusBadRequest )
199
+ return
200
+ }
201
+
202
+ //RBAC
203
+ token := r .Header .Get ("token" )
204
+ if isSuperAdmin := impl .enforcer .Enforce (token , casbin .ResourceGlobal , casbin .ActionGet , "*" ); ! isSuperAdmin {
205
+ common .WriteJsonResp (w , err , nil , http .StatusForbidden )
206
+ return
207
+ }
208
+ //RBAC
209
+
149
210
res , err := impl .notificationService .CreateOrUpdateNotificationSettings (& notificationSetting , userId )
150
211
if err != nil {
151
212
impl .logger .Errorw ("service err, SaveNotificationSettings" , "err" , err , "payload" , notificationSetting )
0 commit comments