Skip to content

Commit 9d47d8c

Browse files
committed
#271 - Better implementation for the item and folder checks
1 parent b425799 commit 9d47d8c

File tree

2 files changed

+75
-50
lines changed

2 files changed

+75
-50
lines changed

src/controls/securityTrimmedControl/PermissionLevel.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,13 @@ export enum PermissionLevel {
1717
/**
1818
* Checks permissions on the specified list/library URL in combination with the site URL
1919
*/
20-
remoteListOrLib
20+
remoteListOrLib,
21+
/**
22+
* Check permissions on a specific item in a list/library
23+
*/
24+
remoteListItem,
25+
/**
26+
* Check permissions on a specific folder
27+
*/
28+
remoteFolder
2129
}

src/controls/securityTrimmedControl/SecurityTrimmedControl.tsx

Lines changed: 66 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ export class SecurityTrimmedControl extends React.Component<ISecurityTrimmedCont
6262
} else if (level === PermissionLevel.remoteListOrLib) {
6363
// Check permissions on remote list/library
6464
this.checkRemoteListOrLibPermissions();
65+
} else if (level === PermissionLevel.remoteListItem) {
66+
this.checkRemoteListItem();
67+
} else if (level === PermissionLevel.remoteFolder) {
68+
this.checkRemoteFolder();
6569
}
6670
}
6771

@@ -112,63 +116,76 @@ export class SecurityTrimmedControl extends React.Component<ISecurityTrimmedCont
112116
* Check the user its permissions on the remote list or library
113117
*/
114118
private async checkRemoteListOrLibPermissions() {
115-
const { context, remoteSiteUrl, relativeLibOrListUrl, permissions } = this.props;
119+
const { remoteSiteUrl, relativeLibOrListUrl, permissions } = this.props;
116120
// Check if all properties are provided
117121
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+
});
150127
}
151128
}
152129

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+
});
162142
}
163-
// Check permission on a specific folder.
164-
else if (folderPath) {
165-
const folderByServerRelativeUrl: string = `${encodeURIComponent(relativeLibOrListUrl)}/${encodeURIComponent(folderPath)}`;
143+
}
166144

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+
});
168158
}
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;
172189
}
173190
}
174191

0 commit comments

Comments
 (0)