@@ -243,7 +243,28 @@ func (r *dynamicRenderer) getWorkspaceOwnerData(ctx context.Context, ownerID uui
243243return nil // already fetched
244244}
245245
246- user ,err := r .db .GetUserByID (ctx ,ownerID )
246+ owner ,err := WorkspaceOwner (ctx ,r .db ,r .data .templateVersion .OrganizationID ,ownerID )
247+ if err != nil {
248+ return err
249+ }
250+
251+ r .currentOwner = owner
252+ return nil
253+ }
254+
255+ func (r * dynamicRenderer )Close () {
256+ r .once .Do (r .close )
257+ }
258+
259+ func ProvisionerVersionSupportsDynamicParameters (version string )bool {
260+ major ,minor ,err := apiversion .Parse (version )
261+ // If the api version is not valid or less than 1.6, we need to use the static parameters
262+ useStaticParams := err != nil || major < 1 || (major == 1 && minor < 6 )
263+ return ! useStaticParams
264+ }
265+
266+ func WorkspaceOwner (ctx context.Context ,db database.Store ,org uuid.UUID ,ownerID uuid.UUID ) (* previewtypes.WorkspaceOwner ,error ) {
267+ user ,err := db .GetUserByID (ctx ,ownerID )
247268if err != nil {
248269// If the user failed to read, we also try to read the user from their
249270// organization member. You only need to be able to read the organization member
@@ -252,37 +273,37 @@ func (r *dynamicRenderer) getWorkspaceOwnerData(ctx context.Context, ownerID uui
252273// Only the terraform files can therefore leak more information than the
253274// caller should have access to. All this info should be public assuming you can
254275// read the user though.
255- mem ,err := database .ExpectOne (r . db .OrganizationMembers (ctx , database.OrganizationMembersParams {
256- OrganizationID :r . data . templateVersion . OrganizationID ,
276+ mem ,err := database .ExpectOne (db .OrganizationMembers (ctx , database.OrganizationMembersParams {
277+ OrganizationID :org ,
257278UserID :ownerID ,
258279IncludeSystem :true ,
259280}))
260281if err != nil {
261- return xerrors .Errorf ("fetch user: %w" ,err )
282+ return nil , xerrors .Errorf ("fetch user: %w" ,err )
262283}
263284
264285// Org member fetched, so use the provisioner context to fetch the user.
265286//nolint:gocritic // Has the correct permissions, and matches the provisioning flow.
266- user ,err = r . db .GetUserByID (dbauthz .AsProvisionerd (ctx ),mem .OrganizationMember .UserID )
287+ user ,err = db .GetUserByID (dbauthz .AsProvisionerd (ctx ),mem .OrganizationMember .UserID )
267288if err != nil {
268- return xerrors .Errorf ("fetch user: %w" ,err )
289+ return nil , xerrors .Errorf ("fetch user: %w" ,err )
269290}
270291}
271292
272293// nolint:gocritic // This is kind of the wrong query to use here, but it
273294// matches how the provisioner currently works. We should figure out
274295// something that needs less escalation but has the correct behavior.
275- row ,err := r . db .GetAuthorizationUserRoles (dbauthz .AsProvisionerd (ctx ),ownerID )
296+ row ,err := db .GetAuthorizationUserRoles (dbauthz .AsProvisionerd (ctx ),ownerID )
276297if err != nil {
277- return xerrors .Errorf ("user roles: %w" ,err )
298+ return nil , xerrors .Errorf ("user roles: %w" ,err )
278299}
279300roles ,err := row .RoleNames ()
280301if err != nil {
281- return xerrors .Errorf ("expand roles: %w" ,err )
302+ return nil , xerrors .Errorf ("expand roles: %w" ,err )
282303}
283304ownerRoles := make ([]previewtypes.WorkspaceOwnerRBACRole ,0 ,len (roles ))
284305for _ ,it := range roles {
285- if it .OrganizationID != uuid .Nil && it .OrganizationID != r . data . templateVersion . OrganizationID {
306+ if it .OrganizationID != uuid .Nil && it .OrganizationID != org {
286307continue
287308}
288309var orgID string
@@ -298,28 +319,28 @@ func (r *dynamicRenderer) getWorkspaceOwnerData(ctx context.Context, ownerID uui
298319// The correct public key has to be sent. This will not be leaked
299320// unless the template leaks it.
300321// nolint:gocritic
301- key ,err := r . db .GetGitSSHKey (dbauthz .AsProvisionerd (ctx ),ownerID )
322+ key ,err := db .GetGitSSHKey (dbauthz .AsProvisionerd (ctx ),ownerID )
302323if err != nil && ! xerrors .Is (err ,sql .ErrNoRows ) {
303- return xerrors .Errorf ("ssh key: %w" ,err )
324+ return nil , xerrors .Errorf ("ssh key: %w" ,err )
304325}
305326
306327// The groups need to be sent to preview. These groups are not exposed to the
307328// user, unless the template does it through the parameters. Regardless, we need
308329// the correct groups, and a user might not have read access.
309330// nolint:gocritic
310- groups ,err := r . db .GetGroups (dbauthz .AsProvisionerd (ctx ), database.GetGroupsParams {
311- OrganizationID :r . data . templateVersion . OrganizationID ,
331+ groups ,err := db .GetGroups (dbauthz .AsProvisionerd (ctx ), database.GetGroupsParams {
332+ OrganizationID :org ,
312333HasMemberID :ownerID ,
313334})
314335if err != nil {
315- return xerrors .Errorf ("groups: %w" ,err )
336+ return nil , xerrors .Errorf ("groups: %w" ,err )
316337}
317338groupNames := make ([]string ,0 ,len (groups ))
318339for _ ,it := range groups {
319340groupNames = append (groupNames ,it .Group .Name )
320341}
321342
322- r . currentOwner = & previewtypes.WorkspaceOwner {
343+ return & previewtypes.WorkspaceOwner {
323344ID :user .ID .String (),
324345Name :user .Username ,
325346FullName :user .Name ,
@@ -328,17 +349,5 @@ func (r *dynamicRenderer) getWorkspaceOwnerData(ctx context.Context, ownerID uui
328349RBACRoles :ownerRoles ,
329350SSHPublicKey :key .PublicKey ,
330351Groups :groupNames ,
331- }
332- return nil
333- }
334-
335- func (r * dynamicRenderer )Close () {
336- r .once .Do (r .close )
337- }
338-
339- func ProvisionerVersionSupportsDynamicParameters (version string )bool {
340- major ,minor ,err := apiversion .Parse (version )
341- // If the api version is not valid or less than 1.6, we need to use the static parameters
342- useStaticParams := err != nil || major < 1 || (major == 1 && minor < 6 )
343- return ! useStaticParams
352+ },nil
344353}