Skip to content

Commit

Permalink
manager access
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivam-nagar23 committed Sep 27, 2024
1 parent 7c570d0 commit a489336
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
46 changes: 31 additions & 15 deletions api/auth/user/UserRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/devtron-labs/devtron/pkg/auth/user/helper"
"github.com/devtron-labs/devtron/pkg/auth/user/repository"
util3 "github.com/devtron-labs/devtron/pkg/auth/user/util"
"github.com/devtron-labs/devtron/pkg/team"
"github.com/go-pg/pg"
"github.com/gorilla/schema"
"net/http"
Expand Down Expand Up @@ -78,18 +79,21 @@ type UserRestHandlerImpl struct {
enforcer casbin.Enforcer
roleGroupService user2.RoleGroupService
userCommonService user2.UserCommonService
teamService team.TeamService
}

func NewUserRestHandlerImpl(userService user2.UserService, validator *validator.Validate,
logger *zap.SugaredLogger, enforcer casbin.Enforcer, roleGroupService user2.RoleGroupService,
userCommonService user2.UserCommonService) *UserRestHandlerImpl {
userCommonService user2.UserCommonService,
teamService team.TeamService) *UserRestHandlerImpl {
userAuthHandler := &UserRestHandlerImpl{
userService: userService,
validator: validator,
logger: logger,
enforcer: enforcer,
roleGroupService: roleGroupService,
userCommonService: userCommonService,
teamService: teamService,
}
return userAuthHandler
}
Expand Down Expand Up @@ -245,13 +249,11 @@ func (handler UserRestHandlerImpl) GetById(w http.ResponseWriter, r *http.Reques
filteredRoleFilter := make([]bean.RoleFilter, 0)
isManagerOfAnyApp := false
if res.RoleFilters != nil && len(res.RoleFilters) > 0 {
for _, filter := range res.RoleFilters {
if len(filter.Team) > 0 {
if ok := handler.enforcer.Enforce(token, casbin.ResourceUser, casbin.ActionGet, filter.Team); ok {
isManagerOfAnyApp = true
break
}
}
isManagerOfAnyApp, err = handler.CheckManagerOfAnyAppAccess(token)
if err != nil {
handler.logger.Errorw("rbac Check error, GetById", "err", err, "id", id)
common.WriteJsonResp(w, err, "Failed to get by id", http.StatusInternalServerError)
return
}
}
// sending all permission in case of super admin or manager of any app
Expand All @@ -272,6 +274,22 @@ func (handler UserRestHandlerImpl) GetById(w http.ResponseWriter, r *http.Reques
common.WriteJsonResp(w, err, res, http.StatusOK)
}

func (handler UserRestHandlerImpl) CheckManagerOfAnyAppAccess(token string) (bool, error) {
var isManagerOfAnyApp bool
teams, err := handler.teamService.FetchAllActive()
if err != nil {
handler.logger.Errorw("error encountered in CheckManagerOfAnyAppAccess", "err", err)
return false, err
}
for _, project := range teams {
if ok := handler.enforcer.Enforce(token, casbin.ResourceUser, casbin.ActionGet, project.Name); ok {
isManagerOfAnyApp = true
break
}
}
return isManagerOfAnyApp, nil
}

func (handler UserRestHandlerImpl) GetAllV2(w http.ResponseWriter, r *http.Request) {
var decoder = schema.NewDecoder()
userId, err := handler.userService.GetLoggedInUser(r)
Expand Down Expand Up @@ -577,13 +595,11 @@ func (handler UserRestHandlerImpl) FetchRoleGroupById(w http.ResponseWriter, r *
filteredRoleFilter := make([]bean.RoleFilter, 0)
isManagerOfAnyApp := false
if res.RoleFilters != nil && len(res.RoleFilters) > 0 {
for _, filter := range res.RoleFilters {
if len(filter.Team) > 0 {
if ok := handler.enforcer.Enforce(token, casbin.ResourceUser, casbin.ActionGet, filter.Team); ok {
isManagerOfAnyApp = true
break
}
}
isManagerOfAnyApp, err = handler.CheckManagerOfAnyAppAccess(token)
if err != nil {
handler.logger.Errorw("rbac Check error, GetById", "err", err, "id", id)
common.WriteJsonResp(w, err, "Failed to get by id", http.StatusInternalServerError)
return
}
}
if isManagerOfAnyApp || res.SuperAdmin {
Expand Down
2 changes: 1 addition & 1 deletion cmd/external-app/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a489336

Please sign in to comment.