mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
chore: reorder prebuilt workspace authorization logic (#18506)
## Description Follow-up from PR https://github.com/coder/coder/pull/18333 Related with: https://github.com/coder/coder/pull/18333#discussion_r2159300881 This changes the authorization logic to first try the normal workspace authorization check, and only if the resource is a prebuilt workspace, fall back to the prebuilt workspace authorization check. Since prebuilt workspaces are a subset of workspaces, the normal workspace check is more likely to succeed. This is a small optimization to reduce unnecessary prebuilt authorization calls.
This commit is contained in:
@ -391,17 +391,16 @@ func (api *API) postWorkspaceBuilds(rw http.ResponseWriter, r *http.Request) {
|
||||
tx,
|
||||
api.FileCache,
|
||||
func(action policy.Action, object rbac.Objecter) bool {
|
||||
if auth := api.Authorize(r, action, object); auth {
|
||||
return true
|
||||
}
|
||||
// Special handling for prebuilt workspace deletion
|
||||
if object.RBACObject().Type == rbac.ResourceWorkspace.Type && action == policy.ActionDelete {
|
||||
if workspaceObj, ok := object.(database.Workspace); ok {
|
||||
// Try prebuilt-specific authorization first
|
||||
if auth := api.Authorize(r, action, workspaceObj.AsPrebuild()); auth {
|
||||
return auth
|
||||
}
|
||||
if action == policy.ActionDelete {
|
||||
if workspaceObj, ok := object.(database.PrebuiltWorkspaceResource); ok && workspaceObj.IsPrebuild() {
|
||||
return api.Authorize(r, action, workspaceObj.AsPrebuild())
|
||||
}
|
||||
}
|
||||
// Fallback to default authorization
|
||||
return api.Authorize(r, action, object)
|
||||
return false
|
||||
},
|
||||
audit.WorkspaceBuildBaggageFromRequest(r),
|
||||
)
|
||||
|
Reference in New Issue
Block a user