@@ -28,9 +28,11 @@ export class SecurityTrimmedControl extends React.Component<ISecurityTrimmedCont
28
28
public componentDidUpdate ( prevProps : ISecurityTrimmedControlProps , prevState : ISecurityTrimmedControlState ) : void {
29
29
// Check permissions only if necessary
30
30
if ( prevProps . level !== this . props . level ||
31
- prevProps . permissions !== this . props . permissions ||
32
- prevProps . relativeLibOrListUrl !== this . props . relativeLibOrListUrl ||
33
- prevProps . remoteSiteUrl !== this . props . remoteSiteUrl ) {
31
+ prevProps . permissions !== this . props . permissions ||
32
+ prevProps . relativeLibOrListUrl !== this . props . relativeLibOrListUrl ||
33
+ prevProps . remoteSiteUrl !== this . props . remoteSiteUrl ||
34
+ prevProps . folderPath !== this . props . folderPath ||
35
+ prevProps . itemId !== this . props . itemId ) {
34
36
this . checkPermissions ( ) ;
35
37
}
36
38
}
@@ -60,6 +62,10 @@ export class SecurityTrimmedControl extends React.Component<ISecurityTrimmedCont
60
62
} else if ( level === PermissionLevel . remoteListOrLib ) {
61
63
// Check permissions on remote list/library
62
64
this . checkRemoteListOrLibPermissions ( ) ;
65
+ } else if ( level === PermissionLevel . remoteListItem ) {
66
+ this . checkRemoteListItem ( ) ;
67
+ } else if ( level === PermissionLevel . remoteFolder ) {
68
+ this . checkRemoteFolder ( ) ;
63
69
}
64
70
}
65
71
@@ -110,41 +116,76 @@ export class SecurityTrimmedControl extends React.Component<ISecurityTrimmedCont
110
116
* Check the user its permissions on the remote list or library
111
117
*/
112
118
private async checkRemoteListOrLibPermissions ( ) {
113
- const { context , remoteSiteUrl, relativeLibOrListUrl, permissions } = this . props ;
119
+ const { remoteSiteUrl, relativeLibOrListUrl, permissions } = this . props ;
114
120
// Check if all properties are provided
115
121
if ( remoteSiteUrl && relativeLibOrListUrl && permissions ) {
116
122
const apiUrl = `${ remoteSiteUrl } /_api/web/GetList(@listUrl)/EffectiveBasePermissions?@listUrl='${ encodeURIComponent ( relativeLibOrListUrl ) } '` ;
117
- const result = await context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) . then ( data => data . json ( ) ) ;
118
- // Check if a result was retrieved
119
- if ( result ) {
120
- // Check if an error was retrieved
121
- if ( result . error ) {
122
- // Do not allow rendering when there was an error
123
- this . setState ( {
124
- allowRender : false
125
- } ) ;
126
- console . error ( `Error retrieved while checking user's remote list or library permissions.` ) ;
127
- return ;
128
- }
123
+ const hasPermissions = await this . checkRemotePermissions ( apiUrl ) ;
124
+ this . setState ( {
125
+ allowRender : hasPermissions
126
+ } ) ;
127
+ }
128
+ }
129
129
130
- // Check the result high and low value are returned
131
- if ( typeof result . High !== "undefined" && typeof result . Low !== "undefined" ) {
132
- // Create the permission mask
133
- const permission = new SPPermission ( result ) ;
134
- const hasPermissions = permission . hasAllPermissions ( ...permissions ) ;
130
+ /**
131
+ * Check permissions on item level
132
+ */
133
+ private async checkRemoteListItem ( ) {
134
+ const { remoteSiteUrl, relativeLibOrListUrl, permissions, itemId } = this . props ;
135
+ // Check if all properties are provided
136
+ if ( remoteSiteUrl && relativeLibOrListUrl && permissions && itemId ) {
137
+ const apiUrl = `${ remoteSiteUrl } /_api/web/GetList(@listUrl)/Items(${ itemId } )/EffectiveBasePermissions?@listUrl='${ encodeURIComponent ( relativeLibOrListUrl ) } '` ;
138
+ const hasPermissions = await this . checkRemotePermissions ( apiUrl ) ;
139
+ this . setState ( {
140
+ allowRender : hasPermissions
141
+ } ) ;
142
+ }
143
+ }
135
144
136
- this . setState ( {
137
- allowRender : hasPermissions
138
- } ) ;
139
- return ;
140
- }
141
- } else {
142
- this . setState ( {
143
- allowRender : false
144
- } ) ;
145
- console . error ( `No result value was retrieved when checking the user's remote list or library permissions.` ) ;
146
- return ;
145
+ /**
146
+ * Check permissions on folder
147
+ */
148
+ private async checkRemoteFolder ( ) {
149
+ const { remoteSiteUrl, relativeLibOrListUrl, permissions, folderPath } = this . props ;
150
+ // Check if all properties are provided
151
+ if ( remoteSiteUrl && relativeLibOrListUrl && permissions && folderPath ) {
152
+ const folderByServerRelativeUrl : string = `${ encodeURIComponent ( relativeLibOrListUrl ) } /${ encodeURIComponent ( folderPath ) } ` ;
153
+ const apiUrl = `${ remoteSiteUrl } /_api/web/GetFolderByServerRelativeUrl(@folderByServerRelativeUrl)/ListItemAllFields/EffectiveBasePermissions?@folderByServerRelativeUrl='${ folderByServerRelativeUrl } '` ;
154
+ const hasPermissions = await this . checkRemotePermissions ( apiUrl ) ;
155
+ this . setState ( {
156
+ allowRender : hasPermissions
157
+ } ) ;
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Check the permissions
163
+ *
164
+ * @param apiUrl
165
+ */
166
+ private async checkRemotePermissions ( apiUrl : string ) {
167
+ const { context, permissions } = this . props ;
168
+ const data = await context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) ;
169
+ // Check if a result was retrieved
170
+ if ( data && data . ok ) {
171
+ const result = await data . json ( ) ;
172
+ // Check if an error was retrieved
173
+ if ( result . error ) {
174
+ // Do not allow rendering when there was an error
175
+ console . error ( `Error retrieved while checking permissions` ) ;
176
+ return false ;
177
+ }
178
+
179
+ // Check the result high and low value are returned
180
+ if ( typeof result . High !== "undefined" && typeof result . Low !== "undefined" ) {
181
+ // Create the permission mask
182
+ const permission = new SPPermission ( result ) ;
183
+ const hasPermissions = permission . hasAllPermissions ( ...permissions ) ;
184
+ return hasPermissions ;
147
185
}
186
+ } else {
187
+ console . error ( `No result value was retrieved when checking the user's permissions.` ) ;
188
+ return false ;
148
189
}
149
190
}
150
191
0 commit comments