@@ -73,8 +73,6 @@ export class UmbDocumentWorkspaceContext
73
73
74
74
#isTrashedContext = new UmbIsTrashedEntityContext ( this ) ;
75
75
#publishingContext?: typeof UMB_DOCUMENT_PUBLISHING_WORKSPACE_CONTEXT . TYPE ;
76
- #userCanCreate = false ;
77
- #userCanUpdate = false ;
78
76
79
77
constructor ( host : UmbControllerHost ) {
80
78
super ( host , {
@@ -126,52 +124,19 @@ export class UmbDocumentWorkspaceContext
126
124
this . #publishingContext = context ;
127
125
} ) ;
128
126
129
- createExtensionApiByAlias ( this , UMB_DOCUMENT_USER_PERMISSION_CONDITION_ALIAS , [
130
- {
131
- config : {
132
- allOf : [ UMB_USER_PERMISSION_DOCUMENT_CREATE ] ,
133
- } ,
134
- onChange : ( permitted : boolean ) => {
135
- if ( permitted === this . #userCanCreate) return ;
136
- this . #userCanCreate = permitted ;
137
- this . #setReadOnlyStateForUserPermission(
138
- UMB_USER_PERMISSION_DOCUMENT_CREATE ,
139
- this . #userCanCreate,
140
- 'You do not have permission to create documents.' ,
141
- ) ;
142
- } ,
143
- } ,
144
- ] ) ;
145
-
146
- createExtensionApiByAlias ( this , UMB_DOCUMENT_USER_PERMISSION_CONDITION_ALIAS , [
147
- {
148
- config : {
149
- allOf : [ UMB_USER_PERMISSION_DOCUMENT_UPDATE ] ,
150
- } ,
151
- onChange : ( permitted : boolean ) => {
152
- if ( permitted === this . #userCanUpdate) return ;
153
- this . #userCanUpdate = permitted ;
154
- this . #setReadOnlyStateForUserPermission(
155
- UMB_USER_PERMISSION_DOCUMENT_UPDATE ,
156
- this . #userCanUpdate,
157
- 'You do not have permission to update documents.' ,
158
- ) ;
159
- } ,
160
- } ,
161
- ] ) ;
162
-
163
- this . observe ( this . variants , ( ) => {
164
- this . #setReadOnlyStateForUserPermission(
165
- UMB_USER_PERMISSION_DOCUMENT_CREATE ,
166
- this . #userCanCreate,
167
- 'You do not have permission to create documents.' ,
168
- ) ;
169
-
170
- this . #setReadOnlyStateForUserPermission(
171
- UMB_USER_PERMISSION_DOCUMENT_UPDATE ,
172
- this . #userCanUpdate,
173
- 'You do not have permission to update documents.' ,
174
- ) ;
127
+ this . observe ( this . isNew , ( isNew ) => {
128
+ if ( isNew === undefined ) return ;
129
+ if ( isNew ) {
130
+ this . #enforceUserPermission(
131
+ UMB_USER_PERMISSION_DOCUMENT_CREATE ,
132
+ 'You do not have permission to create documents.' ,
133
+ ) ;
134
+ } else {
135
+ this . #enforceUserPermission(
136
+ UMB_USER_PERMISSION_DOCUMENT_UPDATE ,
137
+ 'You do not have permission to update documents.' ,
138
+ ) ;
139
+ }
175
140
} ) ;
176
141
177
142
this . routes . setRoutes ( [
@@ -225,6 +190,22 @@ export class UmbDocumentWorkspaceContext
225
190
] ) ;
226
191
}
227
192
193
+ #enforceUserPermission( verb : string , message : string ) {
194
+ // We set the initial permission state to false because the condition is false by default and only execute the callback if it changes.
195
+ this . #handleUserPermissionChange( verb , false , message ) ;
196
+
197
+ createExtensionApiByAlias ( this , UMB_DOCUMENT_USER_PERMISSION_CONDITION_ALIAS , [
198
+ {
199
+ config : {
200
+ allOf : [ verb ] ,
201
+ } ,
202
+ onChange : ( permitted : boolean ) => {
203
+ this . #handleUserPermissionChange( verb , permitted , message ) ;
204
+ } ,
205
+ } ,
206
+ ] ) ;
207
+ }
208
+
228
209
override resetState ( ) : void {
229
210
super . resetState ( ) ;
230
211
this . #isTrashedContext. setIsTrashed ( false ) ;
@@ -425,7 +406,7 @@ export class UmbDocumentWorkspaceContext
425
406
return new UmbDocumentPropertyDatasetContext ( host , this , variantId ) ;
426
407
}
427
408
428
- async #setReadOnlyStateForUserPermission ( identifier : string , permitted : boolean , message : string ) {
409
+ async #handleUserPermissionChange ( identifier : string , permitted : boolean , message : string ) {
429
410
if ( permitted ) {
430
411
this . readOnlyGuard ?. removeRule ( identifier ) ;
431
412
return ;
@@ -434,6 +415,9 @@ export class UmbDocumentWorkspaceContext
434
415
this . readOnlyGuard ?. addRule ( {
435
416
unique : identifier ,
436
417
message,
418
+ /* This guard is a bit backwards. The rule is permitted to be read-only.
419
+ If the user does not have permission, we set it to true = permitted to be read-only. */
420
+ permitted : true ,
437
421
} ) ;
438
422
}
439
423
0 commit comments