@@ -62,6 +62,10 @@ export class SecurityTrimmedControl extends React.Component<ISecurityTrimmedCont
62
62
} else if ( level === PermissionLevel . remoteListOrLib ) {
63
63
// Check permissions on remote list/library
64
64
this . checkRemoteListOrLibPermissions ( ) ;
65
+ } else if ( level === PermissionLevel . remoteListItem ) {
66
+ this . checkRemoteListItem ( ) ;
67
+ } else if ( level === PermissionLevel . remoteFolder ) {
68
+ this . checkRemoteFolder ( ) ;
65
69
}
66
70
}
67
71
@@ -112,63 +116,76 @@ export class SecurityTrimmedControl extends React.Component<ISecurityTrimmedCont
112
116
* Check the user its permissions on the remote list or library
113
117
*/
114
118
private async checkRemoteListOrLibPermissions ( ) {
115
- const { context , remoteSiteUrl, relativeLibOrListUrl, permissions } = this . props ;
119
+ const { remoteSiteUrl, relativeLibOrListUrl, permissions } = this . props ;
116
120
// Check if all properties are provided
117
121
if ( remoteSiteUrl && relativeLibOrListUrl && permissions ) {
118
- const apiUrl = this . getUrlByResource ( ) ;
119
- const result = await context . spHttpClient . get ( apiUrl , SPHttpClient . configurations . v1 ) . then ( data => data . json ( ) ) ;
120
- // Check if a result was retrieved
121
- if ( result ) {
122
- // Check if an error was retrieved
123
- if ( result . error ) {
124
- // Do not allow rendering when there was an error
125
- this . setState ( {
126
- allowRender : false
127
- } ) ;
128
- console . error ( `Error retrieved while checking user's remote list or library permissions.` ) ;
129
- return ;
130
- }
131
-
132
- // Check the result high and low value are returned
133
- if ( typeof result . High !== "undefined" && typeof result . Low !== "undefined" ) {
134
- // Create the permission mask
135
- const permission = new SPPermission ( result ) ;
136
- const hasPermissions = permission . hasAllPermissions ( ...permissions ) ;
137
-
138
- this . setState ( {
139
- allowRender : hasPermissions
140
- } ) ;
141
- return ;
142
- }
143
- } else {
144
- this . setState ( {
145
- allowRender : false
146
- } ) ;
147
- console . error ( `No result value was retrieved when checking the user's remote list or library permissions.` ) ;
148
- return ;
149
- }
122
+ const apiUrl = `${ remoteSiteUrl } /_api/web/GetList(@listUrl)/EffectiveBasePermissions?@listUrl='${ encodeURIComponent ( relativeLibOrListUrl ) } '` ;
123
+ const hasPermissions = await this . checkRemotePermissions ( apiUrl ) ;
124
+ this . setState ( {
125
+ allowRender : hasPermissions
126
+ } ) ;
150
127
}
151
128
}
152
129
153
- private getUrlByResource ( ) {
154
- const { remoteSiteUrl, relativeLibOrListUrl, folderPath, itemId } = this . props ;
155
-
156
- // Check permission on a specific item.
157
- if ( itemId ) {
158
- const splitUrl = relativeLibOrListUrl . split ( '/' ) ;
159
- var lastSegment = splitUrl . pop ( ) || splitUrl . pop ( ) ; // Trims trailing slash if it exists.
160
-
161
- return `${ remoteSiteUrl } /_api/web/Lists/GetByTitle(@listTitle)/items(@itemId)/EffectiveBasePermissions?@listTitle='${ encodeURIComponent ( lastSegment ) } '&@itemId='${ itemId } '` ;
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
+ } ) ;
162
142
}
163
- // Check permission on a specific folder.
164
- else if ( folderPath ) {
165
- const folderByServerRelativeUrl : string = `${ encodeURIComponent ( relativeLibOrListUrl ) } /${ encodeURIComponent ( folderPath ) } ` ;
143
+ }
166
144
167
- return `${ remoteSiteUrl } /_api/web/GetFolderByServerRelativeUrl(@folderByServerRelativeUrl)/ListItemAllFields/EffectiveBasePermissions?@folderByServerRelativeUrl='${ folderByServerRelativeUrl } '` ;
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
+ } ) ;
168
158
}
169
- // Check permission on the list or library.
170
- else {
171
- return `${ remoteSiteUrl } /_api/web/GetList(@listUrl)/EffectiveBasePermissions?@listUrl='${ encodeURIComponent ( relativeLibOrListUrl ) } '` ;
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 ;
185
+ }
186
+ } else {
187
+ console . error ( `No result value was retrieved when checking the user's permissions.` ) ;
188
+ return false ;
172
189
}
173
190
}
174
191
0 commit comments