Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: cancel task rbac (#2934)

p0ny30944fbdd7cc
diff --git a/server/stage.go b/server/stage.go
index 52d434e2f7..aab1650357 100644
--- a/server/stage.go
+++ b/server/stage.go
@@ -44,7 +44,7 @@ func (s *Server) registerStageRoutes(g *echo.Group) {
}
// pick any task in the stage to validate
// because all tasks in the same stage share the issue & environment.
- ok, err := s.canPrincipalChangeTaskStatus(ctx, currentPrincipalID, tasks[0])
+ ok, err := s.canPrincipalChangeTaskStatus(ctx, currentPrincipalID, tasks[0], stageAllTaskStatusPatch.Status)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to validate if the principal can change task status").SetInternal(err)
}
diff --git a/server/task.go b/server/task.go
index 2f84b3c5dc..d860d21917 100644
--- a/server/task.go
+++ b/server/task.go
@@ -199,7 +199,7 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Task not found with ID %d", taskID))
}
- ok, err := s.canPrincipalChangeTaskStatus(ctx, currentPrincipalID, task)
+ ok, err := s.canPrincipalChangeTaskStatus(ctx, currentPrincipalID, task, taskStatusPatch.Status)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to validate if the principal can change task status").SetInternal(err)
}
@@ -272,6 +272,15 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid task status transition from %v to %v.", task.Status, api.TaskCanceled))
}
+ currentPrincipalID := c.Get(getPrincipalIDContextKey()).(int)
+ ok, err := s.canPrincipalChangeTaskStatus(ctx, currentPrincipalID, task, api.TaskCanceled)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to validate if the principal can change task status").SetInternal(err)
+ }
+ if !ok {
+ return echo.NewHTTPError(http.StatusUnauthorized, "Not allowed to change task status")
+ }
+
s.TaskScheduler.runningExecutorsMutex.Lock()
cancel, ok := s.TaskScheduler.runningExecutorsCancel[taskID]
s.TaskScheduler.runningExecutorsMutex.Unlock()
@@ -616,7 +625,13 @@ func (s *Server) canPrincipalBeAssignee(ctx context.Context, principalID int, en
}
// canPrincipalChangeTaskStatus validates if the principal has the privilege to update task status, judging from the principal role and the environment policy.
-func (s *Server) canPrincipalChangeTaskStatus(ctx context.Context, principalID int, task *api.Task) (bool, error) {
+func (s *Server) canPrincipalChangeTaskStatus(ctx context.Context, principalID int, task *api.Task, toStatus api.TaskStatus) (bool, error) {
+ // The creator can cancel task.
+ if toStatus == api.TaskCanceled {
+ if principalID == task.CreatorID {
+ return true, nil
+ }
+ }
// the workspace owner and DBA roles can always change task status.
principal, err := s.store.GetPrincipalByID(ctx, principalID)
if err != nil {