Skip to content

Commit

Permalink
Merge pull request #184 from joshtyf/refactor/delete-membership-endpoint
Browse files Browse the repository at this point in the history
Fix middleware to allow frontend to not include role when calling delete membership api endpoint
  • Loading branch information
Zheng-Zhi-Qiang authored Jun 27, 2024
2 parents a8f51a2 + 9d8ed3a commit 032cefd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
6 changes: 3 additions & 3 deletions backend/src/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ func (s *ServerHandler) registerRoutes(r *mux.Router) {

// Membership
r.Handle("/api/membership", isAuthenticated(handleGetMembershipsForUser(s.logger, s.psqlClient), s.logger)).Methods("GET")
r.Handle("/api/membership", isAuthenticated(getOrgIdFromRequestBody(validateMembershipChange(s.psqlClient, isOrgAdmin(s.psqlClient, handleCreateMembership(s.logger, s.psqlClient), s.logger), s.logger), s.logger), s.logger)).Methods("POST").Headers("Content-Type", "application/json")
r.Handle("/api/membership", isAuthenticated(getOrgIdFromRequestBody(validateMembershipChange(s.psqlClient, isOrgAdmin(s.psqlClient, handleUpdateMembership(s.logger, s.psqlClient), s.logger), s.logger), s.logger), s.logger)).Methods("PATCH").Headers("Content-Type", "application/json")
r.Handle("/api/membership", isAuthenticated(getOrgIdFromRequestBody(validateMembershipChange(s.psqlClient, isOrgAdmin(s.psqlClient, handleDeleteMembership(s.logger, s.psqlClient), s.logger), s.logger), s.logger), s.logger)).Methods("DELETE").Headers("Content-Type", "application/json")
r.Handle("/api/membership", isAuthenticated(getOrgIdFromRequestBody(validateMembershipChangeRequest(s.psqlClient, isOrgAdmin(s.psqlClient, validateTargetMembershipRoleGranted(handleCreateMembership(s.logger, s.psqlClient), s.logger), s.logger), s.logger), s.logger), s.logger)).Methods("POST").Headers("Content-Type", "application/json")
r.Handle("/api/membership", isAuthenticated(getOrgIdFromRequestBody(validateMembershipChangeRequest(s.psqlClient, isOrgAdmin(s.psqlClient, validateTargetMembershipRoleGranted(handleUpdateMembership(s.logger, s.psqlClient), s.logger), s.logger), s.logger), s.logger), s.logger)).Methods("PATCH").Headers("Content-Type", "application/json")
r.Handle("/api/membership", isAuthenticated(getOrgIdFromRequestBody(validateMembershipChangeRequest(s.psqlClient, isOrgAdmin(s.psqlClient, handleDeleteMembership(s.logger, s.psqlClient), s.logger), s.logger), s.logger), s.logger)).Methods("DELETE").Headers("Content-Type", "application/json")
r.Handle("/api/membership/ownership_transfer", isAuthenticated(getOrgIdFromRequestBody(isOrgOwner(s.psqlClient, handleOwnershipTransfer(s.logger, s.psqlClient), s.logger), s.logger), s.logger)).Methods("POST").Headers("Content-Type", "application/json")
}

Expand Down
42 changes: 28 additions & 14 deletions backend/src/server/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ func getOrgIdUsingSrId(mongoClient *mongo.Client, next http.Handler, logger logg
})
}

func validateMembershipChange(postgresClient *sql.DB, next http.Handler, logger logger.ServerLogger) http.Handler {
func validateMembershipChangeRequest(postgresClient *sql.DB, next http.Handler, logger logger.ServerLogger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
org_id := r.Context().Value(util.OrgContextKey{}).(int)
requestorMembership, err := getMembership(org_id, postgresClient, r)
Expand All @@ -303,19 +303,6 @@ func validateMembershipChange(postgresClient *sql.DB, next http.Handler, logger
return
}

err = models.ValidateRole(targetMembership.Role)
if err != nil {
logger.Error("unable to add/update membership as role is invalid")
encode(w, r, http.StatusBadRequest, newHandlerError(err, http.StatusBadRequest))
return
}

if targetMembership.Role == models.Owner {
logger.Error("unable to grant/delete ownership")
encode(w, r, http.StatusForbidden, newHandlerError(ErrUnableModifyOwnership, http.StatusForbidden))
return
}

targetExistingMembership, err := database.NewMembership(postgresClient).GetMembershipByUserAndOrgId(targetMembership.UserId, targetMembership.OrgId)
if err != nil && !errors.Is(err, sql.ErrNoRows) {
logger.Error(fmt.Sprintf("unable to verify subject role: %s", err))
Expand All @@ -328,6 +315,33 @@ func validateMembershipChange(postgresClient *sql.DB, next http.Handler, logger
encode(w, r, http.StatusForbidden, newHandlerError(ErrUnauthorised, http.StatusForbidden))
return
}

next.ServeHTTP(w, r)
})
}

func validateTargetMembershipRoleGranted(next http.Handler, logger logger.ServerLogger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
targetMembership, err := decode[models.MembershipModel](r)
if err != nil {
logger.Error(fmt.Sprintf("failed to parse json request body: %s", err))
encode(w, r, http.StatusBadRequest, newHandlerError(ErrJsonParseError, http.StatusBadRequest))
return
}

err = models.ValidateRole(targetMembership.Role)
if err != nil {
logger.Error("unable to add/update membership as role is invalid")
encode(w, r, http.StatusBadRequest, newHandlerError(err, http.StatusBadRequest))
return
}

if targetMembership.Role == models.Owner {
logger.Error("unable to grant ownership")
encode(w, r, http.StatusForbidden, newHandlerError(ErrUnableModifyOwnership, http.StatusForbidden))
return
}

next.ServeHTTP(w, r)
})
}
Expand Down

0 comments on commit 032cefd

Please sign in to comment.