Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] : Multiple project owner backend. #4536

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
142 changes: 141 additions & 1 deletion chaoscenter/authentication/api/handlers/rest/project_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,29 @@ func GetActiveProjectMembers(service services.ApplicationService) gin.HandlerFun
}
}

// GetActiveProjectOwners godoc
//
// @Summary Get active project Owners.
// @Description Return list of active project owners.
// @Tags ProjectRouter
// @Param state path string true "State"
// @Accept json
// @Produce json
// @Failure 500 {object} response.ErrServerError
// @Success 200 {object} response.Response{}
// @Router /get_project_owners/:project_id/:state [get]
func GetActiveProjectOwners(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
projectID := c.Param("project_id")
owners, err := service.GetProjectOwners(projectID)
if err != nil {
c.JSON(utils.ErrorStatusCodes[utils.ErrServerError], presenter.CreateErrorResponse(utils.ErrServerError))
return
}
c.JSON(http.StatusOK, gin.H{"data": owners})
}
}

// getInvitation returns the Invitation status
func getInvitation(service services.ApplicationService, member entities.MemberInput) (entities.Invitation, error) {
project, err := service.GetProjectByProjectID(member.ProjectID)
Expand Down Expand Up @@ -380,7 +403,7 @@ func SendInvitation(service services.ApplicationService) gin.HandlerFunc {
return
}
// Validating member role
if member.Role == nil || (*member.Role != entities.RoleEditor && *member.Role != entities.RoleViewer) {
if member.Role == nil || (*member.Role != entities.RoleEditor && *member.Role != entities.RoleViewer && *member.Role != entities.RoleOwner) {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRole], presenter.CreateErrorResponse(utils.ErrInvalidRole))
return
}
Expand Down Expand Up @@ -568,7 +591,21 @@ func LeaveProject(service services.ApplicationService) gin.HandlerFunc {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
return
}

if member.Role != nil && *member.Role == entities.RoleOwner {
owners, err := service.GetProjectOwners(member.ProjectID)
if err != nil {
log.Error(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrServerError], presenter.CreateErrorResponse(utils.ErrServerError))
return
}

if len(owners) == 1 {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], gin.H{"message": "Cannot leave project. There must be at least one owner."})
return
}
}

err = validations.RbacValidator(c.MustGet("uid").(string), member.ProjectID,
validations.MutationRbacRules["leaveProject"],
string(entities.AcceptedInvitation),
Expand Down Expand Up @@ -726,6 +763,68 @@ func UpdateProjectName(service services.ApplicationService) gin.HandlerFunc {
}
}

// UpdateMemberRole godoc
//
// @Summary Update member role.
// @Description Return updated member role.
// @Tags ProjectRouter
// @Accept json
// @Produce json
// @Failure 400 {object} response.ErrInvalidRequest
// @Failure 401 {object} response.ErrUnauthorized
// @Failure 500 {object} response.ErrServerError
// @Success 200 {object} response.Response{}
// @Router /update_member_role [post]
//
// UpdateMemberRole is used to update a member role in the project
func UpdateMemberRole(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
var member entities.MemberInput
err := c.BindJSON(&member)
if err != nil {
log.Warn(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRequest], presenter.CreateErrorResponse(utils.ErrInvalidRequest))
return
}


// Validating member role
if member.Role == nil || (*member.Role != entities.RoleEditor && *member.Role != entities.RoleViewer && *member.Role != entities.RoleOwner) {
c.JSON(utils.ErrorStatusCodes[utils.ErrInvalidRole], presenter.CreateErrorResponse(utils.ErrInvalidRole))
return
}

err = validations.RbacValidator(c.MustGet("uid").(string),
member.ProjectID,
validations.MutationRbacRules["updateMemberRole"],
string(entities.AcceptedInvitation),
service)
if err != nil {
log.Warn(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrUnauthorized],
presenter.CreateErrorResponse(utils.ErrUnauthorized))
return
}

uid := c.MustGet("uid").(string)
if uid == member.UserID {
c.JSON(http.StatusBadRequest, gin.H{"message": "User cannot change their own role."})
return
}

err = service.UpdateMemberRole(member.ProjectID, member.UserID, member.Role)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also add one more check for a case: There is only 1 owner and that owner is trying update their role to viewer/editor. In this case there will be no project owner. 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure I'll do that

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a check to not allow the logged in user to change their own project roles. This will cover all the conditions. Please check on feasibility of this approach and race conditions if any

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that would be better. cc: @aryan-bhokare

log.Error(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrServerError], presenter.CreateErrorResponse(utils.ErrServerError))
return
}

c.JSON(http.StatusOK, gin.H{
"message": "Successfully updated Role",
})
}
}

// GetOwnerProjects godoc
//
// @Summary Get projects owner.
Expand Down Expand Up @@ -796,3 +895,44 @@ func GetProjectRole(service services.ApplicationService) gin.HandlerFunc {

}
}

// DeleteProject godoc
//
// @Description Delete a project.
// @Tags ProjectRouter
// @Accept json
// @Produce json
// @Failure 400 {object} response.ErrProjectNotFound
// @Failure 500 {object} response.ErrServerError
// @Success 200 {object} response.Response{}
// @Router /delete_project/{project_id} [post]
//
// DeleteProject is used to delete a project.
func DeleteProject (service services.ApplicationService) gin.HandlerFunc {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the resources related to the deleted project should also be deleted. This change can go in a different PR

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets do that in different pr.

return func(c *gin.Context) {
projectID := c.Param("project_id")

err := validations.RbacValidator(c.MustGet("uid").(string),
projectID,
validations.MutationRbacRules["deleteProject"],
string(entities.AcceptedInvitation),
service)
if err != nil {
log.Warn(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrUnauthorized],
presenter.CreateErrorResponse(utils.ErrUnauthorized))
return
}

err = service.DeleteProject(projectID)
if err != nil {
log.Error(err)
c.JSON(utils.ErrorStatusCodes[utils.ErrServerError], presenter.CreateErrorResponse(utils.ErrServerError))
return
}

c.JSON(http.StatusOK, gin.H{
"message": "Successfully deleted project.",
})
}
}
15 changes: 15 additions & 0 deletions chaoscenter/authentication/api/mocks/rest_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ func (m *MockedApplicationService) UpdateProjectName(projectID, projectName stri
return args.Error(0)
}

func (m *MockedApplicationService) UpdateMemberRole(projectID, userID string, role *entities.MemberRole) error {
args := m.Called(projectID, userID, role)
return args.Error(0)
}

func (m *MockedApplicationService) GetAggregateProjects(pipeline mongo.Pipeline, opts *options.AggregateOptions) (*mongo.Cursor, error) {
args := m.Called(pipeline, opts)
return args.Get(0).(*mongo.Cursor), args.Error(1)
Expand All @@ -145,6 +150,11 @@ func (m *MockedApplicationService) GetProjectMembers(projectID, state string) ([
return args.Get(0).([]*entities.Member), args.Error(1)
}

func (m *MockedApplicationService) GetProjectOwners(projectID string) ([]*entities.Member, error) {
args := m.Called(projectID)
return args.Get(0).([]*entities.Member), args.Error(1)
}

func (m *MockedApplicationService) ListInvitations(userID string, invitationState entities.Invitation) ([]*entities.Project, error) {
args := m.Called(userID, invitationState)
return args.Get(0).([]*entities.Project), args.Error(1)
Expand Down Expand Up @@ -199,3 +209,8 @@ func (m *MockedApplicationService) RbacValidator(userID, resourceID string, rule
args := m.Called(userID, resourceID, rules, invitationStatus)
return args.Error(0)
}

func (m *MockedApplicationService) DeleteProject(projectID string) error {
args := m.Called(projectID)
return args.Error(0)
}
3 changes: 3 additions & 0 deletions chaoscenter/authentication/api/routes/project_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func ProjectRouter(router *gin.Engine, service services.ApplicationService) {
router.Use(middleware.JwtMiddleware(service))
router.GET("/get_project/:project_id", rest.GetProject(service))
router.GET("/get_project_members/:project_id/:state", rest.GetActiveProjectMembers(service))
router.GET("/get_project_owners/:project_id", rest.GetActiveProjectOwners(service))
router.GET("/get_user_with_project/:username", rest.GetUserWithProject(service))
router.GET("/get_owner_projects", rest.GetOwnerProjects(service))
router.GET("/get_project_role/:project_id", rest.GetProjectRole(service))
Expand All @@ -26,4 +27,6 @@ func ProjectRouter(router *gin.Engine, service services.ApplicationService) {
router.POST("/remove_invitation", rest.RemoveInvitation(service))
router.POST("/leave_project", rest.LeaveProject(service))
router.POST("/update_project_name", rest.UpdateProjectName(service))
router.POST("/update_member_role", rest.UpdateMemberRole(service))
router.POST("/delete_project/:project_id", rest.DeleteProject(service))
}
12 changes: 10 additions & 2 deletions chaoscenter/authentication/pkg/entities/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ type Project struct {
}

type Owner struct {
UserID string `bson:"user_id" json:"userID"`
Username string `bson:"username" json:"username"`
UserID string `bson:"user_id" json:"userID"`
SarthakJain26 marked this conversation as resolved.
Show resolved Hide resolved
Username string `bson:"username" json:"username"`
Invitation Invitation `bson:"invitation" json:"invitation"`
JoinedAt int64 `bson:"joined_at" json:"joinedAt"`
DeactivatedAt *int64 `bson:"deactivated_at,omitempty" json:"deactivatedAt,omitempty"`
}

type MemberStat struct {
Owner *[]Owner `bson:"owner" json:"owner"`
Total int `bson:"total" json:"total"`
Expand Down Expand Up @@ -50,6 +54,10 @@ type CreateProjectInput struct {
UserID string `bson:"user_id" json:"userID"`
}

type DeleteProjectInput struct {
ProjectID string `json:"projectID"`
}

type MemberInput struct {
ProjectID string `json:"projectID"`
UserID string `json:"userID"`
Expand Down
59 changes: 59 additions & 0 deletions chaoscenter/authentication/pkg/project/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ type Repository interface {
RemoveInvitation(projectID string, userID string, invitation entities.Invitation) error
UpdateInvite(projectID string, userID string, invitation entities.Invitation, role *entities.MemberRole) error
UpdateProjectName(projectID string, projectName string) error
UpdateMemberRole(projectID string, userID string, role *entities.MemberRole) error
GetAggregateProjects(pipeline mongo.Pipeline, opts *options.AggregateOptions) (*mongo.Cursor, error)
UpdateProjectState(ctx context.Context, userID string, deactivateTime int64, isDeactivate bool) error
GetOwnerProjects(ctx context.Context, userID string) ([]*entities.Project, error)
GetProjectRole(projectID string, userID string) (*entities.MemberRole, error)
GetProjectMembers(projectID string, state string) ([]*entities.Member, error)
GetProjectOwners(projectID string) ([]*entities.Member, error)
DeleteProject(projectID string) error
ListInvitations(userID string, invitationState entities.Invitation) ([]*entities.Project, error)
}

Expand Down Expand Up @@ -277,6 +280,24 @@ func (r repository) UpdateProjectName(projectID string, projectName string) erro
return nil
}

// UpdateMemberRole : Updates Role of the member in the project.
func (r repository) UpdateMemberRole(projectID string, userID string, role *entities.MemberRole) error {
opts := options.Update().SetArrayFilters(options.ArrayFilters{
Filters: []interface{}{
bson.D{{"elem.user_id", userID}},
},
})
query := bson.D{{"_id", projectID}}
update := bson.D{{"$set", bson.M{"members.$[elem].role": role}}}

_, err := r.Collection.UpdateOne(context.TODO(), query, update, opts)

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources

This query depends on a [user-provided value](1).
if err != nil {
return err
}

return nil
}

// GetAggregateProjects takes a mongo pipeline to retrieve the project details from the database
func (r repository) GetAggregateProjects(pipeline mongo.Pipeline, opts *options.AggregateOptions) (*mongo.Cursor, error) {
results, err := r.Collection.Aggregate(context.TODO(), pipeline, opts)
Expand Down Expand Up @@ -381,6 +402,28 @@ func (r repository) GetOwnerProjects(ctx context.Context, userID string) ([]*ent
return projects, nil
}

// GetProjectOwners takes projectID and returns the owners
func (r repository) GetProjectOwners(projectID string) ([]*entities.Member, error) {
filter := bson.D{{"_id", projectID}}

var project struct {
Members []*entities.Member `bson:"members"`
}
err := r.Collection.FindOne(context.TODO(), filter).Decode(&project)
Fixed Show fixed Hide fixed

Check failure

Code scanning / CodeQL

Database query built from user-controlled sources

This query depends on a [user-provided value](1).
if err != nil {
return nil, err
}

// Filter the members to include only the owners
var owners []*entities.Member
for _, member := range project.Members {
if member.Role == entities.RoleOwner && member.Invitation == entities.AcceptedInvitation {
owners = append(owners, member)
}
}
return owners, nil
}

// GetProjectRole returns the role of a user in the project
func (r repository) GetProjectRole(projectID string, userID string) (*entities.MemberRole, error) {
filter := bson.D{
Expand Down Expand Up @@ -556,3 +599,19 @@ func NewRepo(collection *mongo.Collection) Repository {
Collection: collection,
}
}

// DeleteProject deletes the project with given projectID
func (r repository) DeleteProject(projectID string) error {
query := bson.D{{"_id", projectID}}

result, err := r.Collection.DeleteOne(context.TODO(), query)
if err != nil {
return err
}

if result.DeletedCount == 0 {
return errors.New("no project found with the given projectID")
}

return nil
}
15 changes: 15 additions & 0 deletions chaoscenter/authentication/pkg/services/project_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ type projectService interface {
RemoveInvitation(projectID string, userID string, invitation entities.Invitation) error
UpdateInvite(projectID string, userID string, invitation entities.Invitation, role *entities.MemberRole) error
UpdateProjectName(projectID string, projectName string) error
UpdateMemberRole(projectID string, userID string, role *entities.MemberRole) error
GetAggregateProjects(pipeline mongo.Pipeline, opts *options.AggregateOptions) (*mongo.Cursor, error)
UpdateProjectState(ctx context.Context, userID string, deactivateTime int64, isDeactivate bool) error
GetOwnerProjectIDs(ctx context.Context, userID string) ([]*entities.Project, error)
GetProjectRole(projectID string, userID string) (*entities.MemberRole, error)
GetProjectMembers(projectID string, state string) ([]*entities.Member, error)
GetProjectOwners(projectID string) ([]*entities.Member, error)
DeleteProject(projectID string) error
ListInvitations(userID string, invitationState entities.Invitation) ([]*entities.Project, error)
}

Expand Down Expand Up @@ -64,6 +67,10 @@ func (a applicationService) UpdateProjectName(projectID string, projectName stri
return a.projectRepository.UpdateProjectName(projectID, projectName)
}

func (a applicationService) UpdateMemberRole(projectID string, userID string, role *entities.MemberRole) error {
return a.projectRepository.UpdateMemberRole(projectID, userID, role)
}

func (a applicationService) GetAggregateProjects(pipeline mongo.Pipeline, opts *options.AggregateOptions) (*mongo.Cursor, error) {
return a.projectRepository.GetAggregateProjects(pipeline, opts)
}
Expand All @@ -82,6 +89,14 @@ func (a applicationService) GetProjectMembers(projectID string, state string) ([
return a.projectRepository.GetProjectMembers(projectID, state)
}

func (a applicationService) GetProjectOwners(projectID string) ([]*entities.Member, error) {
return a.projectRepository.GetProjectOwners(projectID)
}

func (a applicationService) ListInvitations(userID string, invitationState entities.Invitation) ([]*entities.Project, error) {
return a.projectRepository.ListInvitations(userID, invitationState)
}

func (a applicationService) DeleteProject(projectID string) error {
return a.projectRepository.DeleteProject(projectID)
}
8 changes: 5 additions & 3 deletions chaoscenter/authentication/pkg/validations/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import "github.com/litmuschaos/litmus/chaoscenter/authentication/pkg/entities"

var MutationRbacRules = map[string][]string{
"sendInvitation": {string(entities.RoleOwner)},
"acceptInvitation": {string(entities.RoleViewer), string(entities.RoleEditor)},
"declineInvitation": {string(entities.RoleViewer),
"acceptInvitation": {string(entities.RoleOwner), string(entities.RoleViewer), string(entities.RoleEditor)},
"declineInvitation": {string(entities.RoleOwner), string(entities.RoleViewer),
string(entities.RoleEditor)},
"removeInvitation": {string(entities.RoleOwner)},
"leaveProject": {string(entities.RoleViewer), string(entities.RoleEditor)},
"leaveProject": {string(entities.RoleOwner), string(entities.RoleViewer), string(entities.RoleEditor)},
"updateProjectName": {string(entities.RoleOwner)},
"updateMemberRole": {string(entities.RoleOwner)},
"deleteProject": {string(entities.RoleOwner)},
"getProject": {string(entities.RoleOwner), string(entities.RoleViewer), string(entities.RoleEditor)},
}
Loading