@@ -846,26 +846,26 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
846
846
847
847
const user = await this . checkUser ( "getWorkspace" ) ;
848
848
849
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
850
- const latestInstancePromise = this . workspaceDb . trace ( ctx ) . findCurrentInstance ( workspaceId ) ;
851
- const teamMembers = await this . organizationService . listMembers ( user . id , workspace . organizationId ) ;
852
- await this . guardAccess ( { kind : "workspace" , subject : workspace , teamMembers : teamMembers } , "get" ) ;
853
- const latestInstance = await latestInstancePromise ;
854
- if ( ! ! latestInstance ) {
855
- await this . guardAccess (
856
- {
857
- kind : "workspaceInstance" ,
858
- subject : latestInstance ,
859
- workspace ,
860
- teamMembers ,
861
- } ,
862
- "get" ,
863
- ) ;
864
- }
849
+ const result = await this . workspaceService . getWorkspace ( user . id , workspaceId , async ( workspace , instance ) => {
850
+ if ( instance ) {
851
+ const teamMembers = await this . organizationService . listMembers ( user . id , workspace . organizationId ) ;
852
+ await this . guardAccess (
853
+ {
854
+ kind : "workspaceInstance" ,
855
+ subject : instance ,
856
+ workspace ,
857
+ teamMembers ,
858
+ } ,
859
+ "get" ,
860
+ ) ;
861
+ } else {
862
+ return this . guardAccess ( { kind : "workspace" , subject : workspace } , "get" ) ;
863
+ }
864
+ } ) ;
865
865
866
866
return {
867
- workspace ,
868
- latestInstance : this . censorInstance ( latestInstance ) ,
867
+ ... result ,
868
+ latestInstance : this . censorInstance ( result . latestInstance ) ,
869
869
} ;
870
870
}
871
871
@@ -876,10 +876,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
876
876
const user = await this . checkAndBlockUser ( "getOwnerToken" ) ;
877
877
878
878
//TODO this requests are only here to populate the resource guard check
879
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
880
- if ( ! workspace ) {
881
- throw new Error ( "owner token not found" ) ;
882
- }
879
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
883
880
await this . guardAccess ( { kind : "workspace" , subject : workspace } , "get" ) ;
884
881
885
882
const latestInstance = await this . workspaceService . getCurrentInstance ( user . id , workspaceId ) ;
@@ -895,7 +892,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
895
892
const user = await this . checkAndBlockUser ( "getIDECredentials" ) ;
896
893
897
894
//TODO this requests are only here to populate the resource guard check
898
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
895
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
899
896
await this . guardAccess ( { kind : "workspace" , subject : workspace } , "get" ) ;
900
897
901
898
return await this . workspaceService . getIDECredentials ( user . id , workspaceId ) ;
@@ -912,7 +909,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
912
909
const user = await this . checkAndBlockUser ( "startWorkspace" , undefined , { workspaceId } ) ;
913
910
914
911
// (gpl) We keep this check here for backwards compatibility, it should be superfluous in the future
915
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
912
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
916
913
await this . guardAccess ( { kind : "workspace" , subject : workspace } , "get" ) ;
917
914
918
915
// (gpl) We keep this check here for backwards compatibility, it should be superfluous in the future
@@ -951,7 +948,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
951
948
const user = await this . checkUser ( "stopWorkspace" , undefined , { workspaceId } ) ;
952
949
const logCtx = { userId : user . id , workspaceId } ;
953
950
954
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
951
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
955
952
if ( workspace . type === "prebuild" ) {
956
953
// If this is a team prebuild, any team member can stop it.
957
954
const teamMembers = await this . organizationService . listMembers ( user . id , workspace . organizationId ) ;
@@ -1031,22 +1028,22 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
1031
1028
1032
1029
const user = await this . checkAndBlockUser ( "updateWorkspaceUserPin" ) ;
1033
1030
1034
- const ws = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1035
- await this . guardAccess ( { kind : "workspace" , subject : ws } , "update" ) ;
1031
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1032
+ await this . guardAccess ( { kind : "workspace" , subject : workspace } , "update" ) ;
1036
1033
1037
1034
switch ( action ) {
1038
1035
case "pin" :
1039
- ws . pinned = true ;
1036
+ workspace . pinned = true ;
1040
1037
break ;
1041
1038
case "unpin" :
1042
- ws . pinned = false ;
1039
+ workspace . pinned = false ;
1043
1040
break ;
1044
1041
case "toggle" :
1045
- ws . pinned = ! ws . pinned ;
1042
+ workspace . pinned = ! workspace . pinned ;
1046
1043
break ;
1047
1044
}
1048
1045
1049
- await this . workspaceService . setPinned ( user . id , ws . id , ws . pinned ) ;
1046
+ await this . workspaceService . setPinned ( user . id , workspace . id , workspace . pinned ) ;
1050
1047
}
1051
1048
1052
1049
public async deleteWorkspace ( ctx : TraceContext , workspaceId : string ) : Promise < void > {
@@ -1055,8 +1052,8 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
1055
1052
1056
1053
const user = await this . checkAndBlockUser ( "deleteWorkspace" ) ;
1057
1054
1058
- const ws = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1059
- await this . guardAccess ( { kind : "workspace" , subject : ws } , "delete" ) ;
1055
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1056
+ await this . guardAccess ( { kind : "workspace" , subject : workspace } , "delete" ) ;
1060
1057
1061
1058
await this . workspaceService . deleteWorkspace ( user . id , workspaceId , "user" ) ;
1062
1059
}
@@ -1067,7 +1064,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
1067
1064
1068
1065
const user = await this . checkAndBlockUser ( "setWorkspaceDescription" ) ;
1069
1066
1070
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1067
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1071
1068
await this . guardAccess ( { kind : "workspace" , subject : workspace } , "update" ) ;
1072
1069
1073
1070
await this . workspaceService . setDescription ( user . id , workspaceId , description ) ;
@@ -1081,22 +1078,13 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
1081
1078
1082
1079
const user = await this . checkUser ( "getWorkspaces" ) ;
1083
1080
1084
- const res = await this . workspaceDb . trace ( ctx ) . find ( {
1085
- limit : 20 ,
1086
- ...options ,
1087
- userId : user . id ,
1088
- includeHeadless : false ,
1081
+ return this . workspaceService . getWorkspaces ( user . id , options , async ( workspace , instance ) => {
1082
+ if ( instance ) {
1083
+ return this . guardAccess ( { kind : "workspaceInstance" , subject : instance , workspace : workspace } , "get" ) ;
1084
+ } else {
1085
+ return this . guardAccess ( { kind : "workspace" , subject : workspace } , "get" ) ;
1086
+ }
1089
1087
} ) ;
1090
- await Promise . all ( res . map ( ( ws ) => this . guardAccess ( { kind : "workspace" , subject : ws . workspace } , "get" ) ) ) ;
1091
- await Promise . all (
1092
- res . map ( ( ws ) =>
1093
- this . guardAccess (
1094
- { kind : "workspaceInstance" , subject : ws . latestInstance , workspace : ws . workspace } ,
1095
- "get" ,
1096
- ) ,
1097
- ) ,
1098
- ) ;
1099
- return res ;
1100
1088
}
1101
1089
1102
1090
public async isWorkspaceOwner ( ctx : TraceContext , workspaceId : string ) : Promise < boolean > {
@@ -1105,7 +1093,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
1105
1093
1106
1094
const user = await this . checkUser ( "isWorkspaceOwner" , undefined , { workspaceId } ) ;
1107
1095
1108
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1096
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1109
1097
await this . guardAccess ( { kind : "workspace" , subject : workspace } , "get" ) ;
1110
1098
return user . id == workspace . ownerId ;
1111
1099
}
@@ -1128,7 +1116,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
1128
1116
1129
1117
const user = await this . checkUser ( "getWorkspaceOwner" ) ;
1130
1118
1131
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1119
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1132
1120
await this . guardAccess ( { kind : "workspace" , subject : workspace } , "get" ) ;
1133
1121
1134
1122
try {
@@ -1149,7 +1137,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
1149
1137
1150
1138
const user = await this . checkAndBlockUser ( "getWorkspaceUsers" , undefined , { workspaceId } ) ;
1151
1139
1152
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1140
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1153
1141
await this . guardAccess ( { kind : "workspace" , subject : workspace } , "get" ) ;
1154
1142
1155
1143
// Note: there's no need to try and guard the users below, they're not complete users but just enough to
@@ -1800,7 +1788,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
1800
1788
1801
1789
const user = await this . checkAndBlockUser ( "updateGitStatus" ) ;
1802
1790
1803
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1791
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1804
1792
const instance = await this . workspaceService . getCurrentInstance ( user . id , workspaceId ) ;
1805
1793
traceWI ( ctx , { instanceId : instance . id } ) ;
1806
1794
await this . guardAccess ( { kind : "workspaceInstance" , subject : instance , workspace } , "update" ) ;
@@ -1818,7 +1806,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
1818
1806
1819
1807
const user = await this . checkAndBlockUser ( "openPort" ) ;
1820
1808
1821
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1809
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1822
1810
const runningInstance = await this . workspaceDb . trace ( ctx ) . findRunningInstance ( workspaceId ) ;
1823
1811
if ( ! runningInstance ) {
1824
1812
log . debug ( { userId : user . id , workspaceId } , "Cannot open port for workspace with no running instance" , {
@@ -1894,7 +1882,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
1894
1882
user : User ,
1895
1883
workspaceId : string ,
1896
1884
) : Promise < { workspace : Workspace ; instance : WorkspaceInstance | undefined } > {
1897
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1885
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
1898
1886
1899
1887
const instance = await this . workspaceDb . trace ( ctx ) . findRunningInstance ( workspaceId ) ;
1900
1888
return { instance, workspace } ;
@@ -2017,7 +2005,7 @@ export class GitpodServerImpl implements GitpodServerWithTracing, Disposable {
2017
2005
2018
2006
async getWorkspaceEnvVars ( ctx : TraceContext , workspaceId : string ) : Promise < EnvVarWithValue [ ] > {
2019
2007
const user = await this . checkUser ( "getWorkspaceEnvVars" ) ;
2020
- const workspace = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
2008
+ const { workspace } = await this . workspaceService . getWorkspace ( user . id , workspaceId ) ;
2021
2009
await this . guardAccess ( { kind : "workspace" , subject : workspace } , "get" ) ;
2022
2010
const envVars = await this . envVarService . resolveEnvVariables (
2023
2011
workspace . ownerId ,
0 commit comments