Skip to content

Commit 4d78c5e

Browse files
authored
Merge branch 'develop' into misc/prValidator
2 parents 917dbe6 + 8fbdd88 commit 4d78c5e

40 files changed

+411
-222
lines changed

api/restHandler/BulkUpdateRestHandler.go

+41-6
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ package restHandler
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
bean4 "github.com/devtron-labs/devtron/pkg/auth/user/bean"
2223
"github.com/devtron-labs/devtron/pkg/build/git/gitMaterial/repository"
2324
"github.com/devtron-labs/devtron/pkg/build/git/gitProvider"
2425
"github.com/devtron-labs/devtron/pkg/bulkAction/bean"
2526
"github.com/devtron-labs/devtron/pkg/bulkAction/service"
2627
"github.com/devtron-labs/devtron/pkg/cluster/environment"
28+
"github.com/devtron-labs/devtron/util"
2729
"net/http"
2830
"strconv"
2931
"strings"
@@ -216,9 +218,14 @@ func (handler BulkUpdateRestHandlerImpl) CheckAuthForBulkUpdate(AppId int, EnvId
216218

217219
}
218220
func (handler BulkUpdateRestHandlerImpl) BulkUpdate(w http.ResponseWriter, r *http.Request) {
221+
userId, err := handler.userAuthService.GetLoggedInUser(r)
222+
if userId == 0 || err != nil {
223+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
224+
return
225+
}
219226
decoder := json.NewDecoder(r.Body)
220227
var script bean.BulkUpdateScript
221-
err := decoder.Decode(&script)
228+
err = decoder.Decode(&script)
222229
if err != nil {
223230
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
224231
return
@@ -257,8 +264,14 @@ func (handler BulkUpdateRestHandlerImpl) BulkUpdate(w http.ResponseWriter, r *ht
257264
return
258265
}
259266
}
260-
261-
response := handler.bulkUpdateService.BulkUpdate(script.Spec)
267+
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
268+
userEmail := util.GetEmailFromContext(r.Context())
269+
userMetadata := &bean4.UserMetadata{
270+
UserEmailId: userEmail,
271+
IsUserSuperAdmin: isSuperAdmin,
272+
UserId: userId,
273+
}
274+
response := handler.bulkUpdateService.BulkUpdate(script.Spec, userMetadata)
262275
common.WriteJsonResp(w, nil, response, http.StatusOK)
263276
}
264277

@@ -268,7 +281,15 @@ func (handler BulkUpdateRestHandlerImpl) BulkHibernate(w http.ResponseWriter, r
268281
return // response already written by the helper on error.
269282
}
270283
token := r.Header.Get("token")
271-
response, err := handler.bulkUpdateService.BulkHibernate(request, r.Context(), w, token, handler.checkAuthForBulkHibernateAndUnhibernate)
284+
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
285+
userEmail := util.GetEmailFromContext(r.Context())
286+
userMetadata := &bean4.UserMetadata{
287+
UserEmailId: userEmail,
288+
IsUserSuperAdmin: isSuperAdmin,
289+
UserId: request.UserId,
290+
}
291+
292+
response, err := handler.bulkUpdateService.BulkHibernate(r.Context(), request, handler.checkAuthForBulkHibernateAndUnhibernate, userMetadata)
272293
if err != nil {
273294
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
274295
return
@@ -305,7 +326,14 @@ func (handler BulkUpdateRestHandlerImpl) BulkUnHibernate(w http.ResponseWriter,
305326
return // response already written by the helper on error.
306327
}
307328
token := r.Header.Get("token")
308-
response, err := handler.bulkUpdateService.BulkUnHibernate(request, r.Context(), w, token, handler.checkAuthForBulkHibernateAndUnhibernate)
329+
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
330+
userEmail := util.GetEmailFromContext(r.Context())
331+
userMetadata := &bean4.UserMetadata{
332+
UserEmailId: userEmail,
333+
IsUserSuperAdmin: isSuperAdmin,
334+
UserId: request.UserId,
335+
}
336+
response, err := handler.bulkUpdateService.BulkUnHibernate(r.Context(), request, handler.checkAuthForBulkHibernateAndUnhibernate, userMetadata)
309337
if err != nil {
310338
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
311339
return
@@ -333,7 +361,14 @@ func (handler BulkUpdateRestHandlerImpl) BulkDeploy(w http.ResponseWriter, r *ht
333361
common.WriteJsonResp(w, err, nil, http.StatusBadRequest)
334362
return
335363
}
336-
response, err := handler.bulkUpdateService.BulkDeploy(&request, token, handler.checkAuthBatch)
364+
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
365+
userEmail := util.GetEmailFromContext(r.Context())
366+
userMetadata := &bean4.UserMetadata{
367+
UserEmailId: userEmail,
368+
IsUserSuperAdmin: isSuperAdmin,
369+
UserId: userId,
370+
}
371+
response, err := handler.bulkUpdateService.BulkDeploy(&request, token, handler.checkAuthBatch, userMetadata)
337372
if err != nil {
338373
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
339374
return

api/restHandler/ConfigMapRestHandler.go

+17-48
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package restHandler
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
"github.com/devtron-labs/devtron/pkg/auth/user/util"
2223
"github.com/devtron-labs/devtron/pkg/pipeline/draftAwareConfigService"
2324
"net/http"
2425
"strconv"
@@ -125,12 +126,8 @@ func (handler ConfigMapRestHandlerImpl) CMGlobalAddUpdate(w http.ResponseWriter,
125126
//RBAC END
126127
ctx := r.Context()
127128
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
128-
userEmail, err := handler.userAuthService.GetActiveEmailById(userId)
129-
if err != nil {
130-
common.WriteJsonResp(w, fmt.Errorf("userEmail not found by userId"), "userEmail not found by userId", http.StatusNotFound)
131-
return
132-
}
133-
res, err := handler.draftAwareResourceService.CMGlobalAddUpdate(ctx, &configMapRequest, isSuperAdmin, userEmail)
129+
userMetadata := util.GetUserMetadata(r.Context(), userId, isSuperAdmin)
130+
res, err := handler.draftAwareResourceService.CMGlobalAddUpdate(ctx, &configMapRequest, userMetadata)
134131
if err != nil {
135132
handler.Logger.Errorw("service err, CMGlobalAddUpdate", "err", err, "payload", configMapRequest)
136133
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
@@ -175,12 +172,8 @@ func (handler ConfigMapRestHandlerImpl) CMEnvironmentAddUpdate(w http.ResponseWr
175172
//RBAC END
176173
ctx := r.Context()
177174
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
178-
userEmail, err := handler.userAuthService.GetActiveEmailById(userId)
179-
if err != nil {
180-
common.WriteJsonResp(w, fmt.Errorf("userEmail not found by userId"), "userEmail not found by userId", http.StatusNotFound)
181-
return
182-
}
183-
res, err := handler.draftAwareResourceService.CMEnvironmentAddUpdate(ctx, &configMapRequest, isSuperAdmin, userEmail)
175+
userMetadata := util.GetUserMetadata(r.Context(), userId, isSuperAdmin)
176+
res, err := handler.draftAwareResourceService.CMEnvironmentAddUpdate(ctx, &configMapRequest, userMetadata)
184177
if err != nil {
185178
handler.Logger.Errorw("service err, CMEnvironmentAddUpdate", "err", err, "payload", configMapRequest)
186179
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
@@ -378,12 +371,8 @@ func (handler ConfigMapRestHandlerImpl) CSGlobalAddUpdate(w http.ResponseWriter,
378371
//RBAC END
379372
ctx := r.Context()
380373
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
381-
userEmail, err := handler.userAuthService.GetActiveEmailById(userId)
382-
if err != nil {
383-
common.WriteJsonResp(w, fmt.Errorf("userEmail not found by userId"), "userEmail not found by userId", http.StatusNotFound)
384-
return
385-
}
386-
res, err := handler.draftAwareResourceService.CSGlobalAddUpdate(ctx, &configMapRequest, isSuperAdmin, userEmail)
374+
userMetadata := util.GetUserMetadata(r.Context(), userId, isSuperAdmin)
375+
res, err := handler.draftAwareResourceService.CSGlobalAddUpdate(ctx, &configMapRequest, userMetadata)
387376
if err != nil {
388377
handler.Logger.Errorw("service err, CSGlobalAddUpdate", "err", err, "payload", configMapRequest)
389378
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
@@ -429,12 +418,8 @@ func (handler ConfigMapRestHandlerImpl) CSEnvironmentAddUpdate(w http.ResponseWr
429418
//RBAC END
430419
ctx := r.Context()
431420
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
432-
userEmail, err := handler.userAuthService.GetActiveEmailById(userId)
433-
if err != nil {
434-
common.WriteJsonResp(w, fmt.Errorf("userEmail not found by userId"), "userEmail not found by userId", http.StatusNotFound)
435-
return
436-
}
437-
res, err := handler.draftAwareResourceService.CSEnvironmentAddUpdate(ctx, &configMapRequest, isSuperAdmin, userEmail)
421+
userMetadata := util.GetUserMetadata(r.Context(), userId, isSuperAdmin)
422+
res, err := handler.draftAwareResourceService.CSEnvironmentAddUpdate(ctx, &configMapRequest, userMetadata)
438423
if err != nil {
439424
handler.Logger.Errorw("service err, CSEnvironmentAddUpdate", "err", err, "payload", configMapRequest)
440425
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
@@ -548,17 +533,13 @@ func (handler ConfigMapRestHandlerImpl) CMGlobalDelete(w http.ResponseWriter, r
548533
//RBAC END
549534
ctx := r.Context()
550535
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
551-
userEmail, err := handler.userAuthService.GetActiveEmailById(userId)
552-
if err != nil {
553-
common.WriteJsonResp(w, fmt.Errorf("userEmail not found by userId"), "userEmail not found by userId", http.StatusNotFound)
554-
return
555-
}
536+
userMetadata := util.GetUserMetadata(r.Context(), userId, isSuperAdmin)
556537
deleteReq := &bean.ConfigDataRequest{
557538
Id: id,
558539
AppId: appId,
559540
UserId: userId,
560541
}
561-
res, err := handler.draftAwareResourceService.CMGlobalDelete(ctx, name, deleteReq, isSuperAdmin, userEmail)
542+
res, err := handler.draftAwareResourceService.CMGlobalDelete(ctx, name, deleteReq, userMetadata)
562543
if err != nil {
563544
handler.Logger.Errorw("service err, CMGlobalDelete", "err", err, "appId", appId, "id", id, "name", name)
564545
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
@@ -614,17 +595,13 @@ func (handler ConfigMapRestHandlerImpl) CMEnvironmentDelete(w http.ResponseWrite
614595
//RBAC END
615596
ctx := r.Context()
616597
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
617-
userEmail, err := handler.userAuthService.GetActiveEmailById(userId)
618-
if err != nil {
619-
common.WriteJsonResp(w, fmt.Errorf("userEmail not found by userId"), "userEmail not found by userId", http.StatusNotFound)
620-
return
621-
}
598+
userMetadata := util.GetUserMetadata(r.Context(), userId, isSuperAdmin)
622599
deleteReq := &bean.ConfigDataRequest{
623600
Id: id,
624601
AppId: appId,
625602
UserId: userId,
626603
}
627-
res, err := handler.draftAwareResourceService.CMEnvironmentDelete(ctx, name, deleteReq, isSuperAdmin, userEmail)
604+
res, err := handler.draftAwareResourceService.CMEnvironmentDelete(ctx, name, deleteReq, userMetadata)
628605
if err != nil {
629606
handler.Logger.Errorw("service err, CMEnvironmentDelete", "err", err, "appId", appId, "envId", envId, "id", id)
630607
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
@@ -666,17 +643,13 @@ func (handler ConfigMapRestHandlerImpl) CSGlobalDelete(w http.ResponseWriter, r
666643
//RBAC END
667644
ctx := r.Context()
668645
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
669-
userEmail, err := handler.userAuthService.GetActiveEmailById(userId)
670-
if err != nil {
671-
common.WriteJsonResp(w, fmt.Errorf("userEmail not found by userId"), "userEmail not found by userId", http.StatusNotFound)
672-
return
673-
}
646+
userMetadata := util.GetUserMetadata(r.Context(), userId, isSuperAdmin)
674647
deleteReq := &bean.ConfigDataRequest{
675648
Id: id,
676649
AppId: appId,
677650
UserId: userId,
678651
}
679-
res, err := handler.draftAwareResourceService.CSGlobalDelete(ctx, name, deleteReq, isSuperAdmin, userEmail)
652+
res, err := handler.draftAwareResourceService.CSGlobalDelete(ctx, name, deleteReq, userMetadata)
680653
if err != nil {
681654
handler.Logger.Errorw("service err, CSGlobalDelete", "err", err, "appId", appId, "id", id, "name", name)
682655
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
@@ -732,17 +705,13 @@ func (handler ConfigMapRestHandlerImpl) CSEnvironmentDelete(w http.ResponseWrite
732705
//RBAC END
733706
ctx := r.Context()
734707
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
735-
userEmail, err := handler.userAuthService.GetActiveEmailById(userId)
736-
if err != nil {
737-
common.WriteJsonResp(w, fmt.Errorf("userEmail not found by userId"), "userEmail not found by userId", http.StatusNotFound)
738-
return
739-
}
708+
userMetadata := util.GetUserMetadata(r.Context(), userId, isSuperAdmin)
740709
deleteReq := &bean.ConfigDataRequest{
741710
Id: id,
742711
AppId: appId,
743712
UserId: userId,
744713
}
745-
res, err := handler.draftAwareResourceService.CSEnvironmentDelete(ctx, name, deleteReq, isSuperAdmin, userEmail)
714+
res, err := handler.draftAwareResourceService.CSEnvironmentDelete(ctx, name, deleteReq, userMetadata)
746715
if err != nil {
747716
handler.Logger.Errorw("service err, CSEnvironmentDelete", "err", err, "appId", appId, "envId", envId, "id", id)
748717
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)

api/restHandler/app/appList/AppListingRestHandler.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
util4 "github.com/devtron-labs/devtron/pkg/appStore/util"
3939
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
4040
"github.com/devtron-labs/devtron/pkg/auth/user"
41+
"github.com/devtron-labs/devtron/pkg/auth/user/bean"
4142
bean5 "github.com/devtron-labs/devtron/pkg/cluster/bean"
4243
bean2 "github.com/devtron-labs/devtron/pkg/cluster/environment/bean"
4344
common2 "github.com/devtron-labs/devtron/pkg/deployment/common"
@@ -49,6 +50,7 @@ import (
4950
k8sApplication "github.com/devtron-labs/devtron/pkg/k8s/application"
5051
"github.com/devtron-labs/devtron/pkg/pipeline"
5152
bean6 "github.com/devtron-labs/devtron/pkg/team/bean"
53+
util2 "github.com/devtron-labs/devtron/util"
5254
"github.com/devtron-labs/devtron/util/rbac"
5355
"github.com/go-pg/pg"
5456
"github.com/gorilla/mux"
@@ -471,6 +473,11 @@ func (handler AppListingRestHandlerImpl) FetchOverviewAppsByEnvironment(w http.R
471473
}
472474

473475
func (handler AppListingRestHandlerImpl) FetchAppDetailsV2(w http.ResponseWriter, r *http.Request) {
476+
userId, err := handler.userService.GetLoggedInUser(r)
477+
if userId == 0 || err != nil {
478+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
479+
return
480+
}
474481
vars := mux.Vars(r)
475482
token := r.Header.Get("token")
476483
appId, err := strconv.Atoi(vars["app-id"])
@@ -488,14 +495,21 @@ func (handler AppListingRestHandlerImpl) FetchAppDetailsV2(w http.ResponseWriter
488495
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), nil, http.StatusForbidden)
489496
return
490497
}
498+
isSuperAdmin := handler.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionCreate, "*")
499+
userEmail := util2.GetEmailFromContext(r.Context())
500+
userMetadata := &bean.UserMetadata{
501+
UserEmailId: userEmail,
502+
IsUserSuperAdmin: isSuperAdmin,
503+
UserId: userId,
504+
}
491505
appDetail, err := handler.appListingService.FetchAppDetails(r.Context(), appId, envId)
492506
if err != nil {
493507
handler.logger.Errorw("service err, FetchAppDetailsV2", "err", err, "appId", appId, "envId", envId)
494508
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
495509
return
496510
}
497511

498-
appDetail, err = handler.updateApprovalConfigDataInAppDetailResp(appDetail, appId, envId)
512+
appDetail, err = handler.updateApprovalConfigDataInAppDetailResp(r.Context(), appDetail, appId, envId, userMetadata)
499513
if err != nil {
500514
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
501515
return

api/restHandler/app/appList/AppListingRestHandler_ent.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package appList
22

33
import (
4+
"context"
45
"github.com/devtron-labs/devtron/api/bean/AppView"
6+
userBean "github.com/devtron-labs/devtron/pkg/auth/user/bean"
57
"net/http"
68
)
79

@@ -14,6 +16,6 @@ func (handler AppListingRestHandlerImpl) FetchAutocompleteJobCiPipelines(w http.
1416
func (handler AppListingRestHandlerImpl) GetAllAppEnvsFromResourceNames(w http.ResponseWriter, r *http.Request) {
1517
}
1618

17-
func (handler AppListingRestHandlerImpl) updateApprovalConfigDataInAppDetailResp(appDetail AppView.AppDetailContainer, appId, envId int) (AppView.AppDetailContainer, error) {
19+
func (handler AppListingRestHandlerImpl) updateApprovalConfigDataInAppDetailResp(ctx context.Context, appDetail AppView.AppDetailContainer, appId, envId int, userMetadata *userBean.UserMetadata) (AppView.AppDetailContainer, error) {
1820
return appDetail, nil
1921
}

0 commit comments

Comments
 (0)