@@ -515,18 +515,6 @@ func createWorkspace(
515515 return
516516 }
517517
518- // The user also needs permission to use the template. At this point they have
519- // read perms, but not necessarily "use"
520- if ! api .Authorize (r , policy .ActionUse , template ) {
521- httpapi .Write (ctx , rw , http .StatusForbidden , codersdk.Response {
522- Message : fmt .Sprintf ("Unauthorized access to use the template %q." , template .Name ),
523- Detail : "Although you are able to view the template, you are unable to create a workspace using it. " +
524- "Please contact an administrator about your permissions if you feel this is an error." ,
525- Validations : nil ,
526- })
527- return
528- }
529-
530518 // Update audit log's organization
531519 auditReq .UpdateOrganizationID (template .OrganizationID )
532520
@@ -537,6 +525,18 @@ func createWorkspace(
537525 httpapi .ResourceNotFound (rw )
538526 return
539527 }
528+ // The user also needs permission to use the template. At this point they have
529+ // read perms, but not necessarily "use". This is also checked in `db.InsertWorkspace`.
530+ // Doing this up front can save some work below if the user doesn't have permission.
531+ if ! api .Authorize (r , policy .ActionUse , template ) {
532+ httpapi .Write (ctx , rw , http .StatusForbidden , codersdk.Response {
533+ Message : fmt .Sprintf ("Unauthorized access to use the template %q." , template .Name ),
534+ Detail : "Although you are able to view the template, you are unable to create a workspace using it. " +
535+ "Please contact an administrator about your permissions if you feel this is an error." ,
536+ Validations : nil ,
537+ })
538+ return
539+ }
540540
541541 templateAccessControl := (* (api .AccessControlStore .Load ())).GetTemplateAccessControl (template )
542542 if templateAccessControl .IsDeprecated () {
0 commit comments