@@ -14,6 +14,8 @@ export const Language = {
14
14
successMessage : "Successfully updated workspace schedule." ,
15
15
}
16
16
17
+ type Permissions = Record < keyof ReturnType < typeof permissionsToCheck > , boolean >
18
+
17
19
export interface WorkspaceScheduleContext {
18
20
formErrors ?: FieldErrors
19
21
getWorkspaceError ?: Error | unknown
@@ -23,8 +25,27 @@ export interface WorkspaceScheduleContext {
23
25
* machine is partially influenced by workspaceXService.
24
26
*/
25
27
workspace ?: TypesGen . Workspace
28
+ // permissions
29
+ userId ?: string
30
+ permissions ?: Permissions
31
+ checkPermissionsError ?: Error | unknown
26
32
}
27
33
34
+ export const checks = {
35
+ updateWorkspace : "updateWorkspace" ,
36
+ } as const
37
+
38
+ const permissionsToCheck = ( workspace : TypesGen . Workspace ) => ( {
39
+ [ checks . updateWorkspace ] : {
40
+ object : {
41
+ resource_type : "workspace" ,
42
+ resource_id : workspace . id ,
43
+ owner_id : workspace . owner_id ,
44
+ } ,
45
+ action : "update" ,
46
+ } ,
47
+ } )
48
+
28
49
export type WorkspaceScheduleEvent =
29
50
| { type : "GET_WORKSPACE" ; username : string ; workspaceName : string }
30
51
| {
@@ -60,7 +81,7 @@ export const workspaceSchedule = createMachine(
60
81
src : "getWorkspace" ,
61
82
id : "getWorkspace" ,
62
83
onDone : {
63
- target : "presentForm " ,
84
+ target : "gettingPermissions " ,
64
85
actions : [ "assignWorkspace" ] ,
65
86
} ,
66
87
onError : {
@@ -70,6 +91,25 @@ export const workspaceSchedule = createMachine(
70
91
} ,
71
92
tags : "loading" ,
72
93
} ,
94
+ gettingPermissions : {
95
+ entry : "clearGetPermissionsError" ,
96
+ invoke : {
97
+ src : "checkPermissions" ,
98
+ id : "checkPermissions" ,
99
+ onDone : [
100
+ {
101
+ actions : [ "assignPermissions" ] ,
102
+ target : "presentForm" ,
103
+ } ,
104
+ ] ,
105
+ onError : [
106
+ {
107
+ actions : "assignGetPermissionsError" ,
108
+ target : "error" ,
109
+ } ,
110
+ ] ,
111
+ } ,
112
+ } ,
73
113
presentForm : {
74
114
on : {
75
115
SUBMIT_SCHEDULE : "submittingSchedule" ,
@@ -113,8 +153,19 @@ export const workspaceSchedule = createMachine(
113
153
assignGetWorkspaceError : assign ( {
114
154
getWorkspaceError : ( _ , event ) => event . data ,
115
155
} ) ,
156
+ assignPermissions : assign ( {
157
+ // Setting event.data as Permissions to be more stricted. So we know
158
+ // what permissions we asked for.
159
+ permissions : ( _ , event ) => event . data as Permissions ,
160
+ } ) ,
161
+ assignGetPermissionsError : assign ( {
162
+ checkPermissionsError : ( _ , event ) => event . data ,
163
+ } ) ,
164
+ clearGetPermissionsError : assign ( {
165
+ checkPermissionsError : ( _ ) => undefined ,
166
+ } ) ,
116
167
clearContext : ( ) => {
117
- assign ( { workspace : undefined } )
168
+ assign ( { workspace : undefined , permissions : undefined } )
118
169
} ,
119
170
clearGetWorkspaceError : ( context ) => {
120
171
assign ( { ...context , getWorkspaceError : undefined } )
@@ -134,6 +185,15 @@ export const workspaceSchedule = createMachine(
134
185
getWorkspace : async ( _ , event ) => {
135
186
return await API . getWorkspaceByOwnerAndName ( event . username , event . workspaceName )
136
187
} ,
188
+ checkPermissions : async ( context ) => {
189
+ if ( context . workspace && context . userId ) {
190
+ return await API . checkUserPermissions ( context . userId , {
191
+ checks : permissionsToCheck ( context . workspace ) ,
192
+ } )
193
+ } else {
194
+ throw Error ( "Cannot check permissions without both workspace and user id" )
195
+ }
196
+ } ,
137
197
submitSchedule : async ( context , event ) => {
138
198
if ( ! context . workspace ?. id ) {
139
199
// This state is theoretically impossible, but helps TS
0 commit comments