Skip to content

Commit 36b73a2

Browse files
committed
Merge branch 'Varuuna-dev' into dev
2 parents 3b8d8b9 + 9d47d8c commit 36b73a2

File tree

3 files changed

+90
-33
lines changed

3 files changed

+90
-33
lines changed

src/controls/securityTrimmedControl/ISecurityTrimmedControlProps.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ export interface ISecurityTrimmedControlProps {
2525
* The relative URL of the list or library. Required when you want to check permissions on remote list.
2626
*/
2727
relativeLibOrListUrl?: string;
28+
/**
29+
* Optional. Specify the name of a folder to check the user permissions against. Will be overridden if itemId is present.
30+
*/
31+
folderPath?: string;
32+
/**
33+
* Optional. Specify the ID of the item to check the user permissions against. Takes precedence over folder.
34+
*/
35+
itemId?: number;
2836
}

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: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ export class SecurityTrimmedControl extends React.Component<ISecurityTrimmedCont
2828
public componentDidUpdate(prevProps: ISecurityTrimmedControlProps, prevState: ISecurityTrimmedControlState): void {
2929
// Check permissions only if necessary
3030
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) {
3436
this.checkPermissions();
3537
}
3638
}
@@ -60,6 +62,10 @@ export class SecurityTrimmedControl extends React.Component<ISecurityTrimmedCont
6062
} else if (level === PermissionLevel.remoteListOrLib) {
6163
// Check permissions on remote list/library
6264
this.checkRemoteListOrLibPermissions();
65+
} else if (level === PermissionLevel.remoteListItem) {
66+
this.checkRemoteListItem();
67+
} else if (level === PermissionLevel.remoteFolder) {
68+
this.checkRemoteFolder();
6369
}
6470
}
6571

@@ -110,41 +116,76 @@ export class SecurityTrimmedControl extends React.Component<ISecurityTrimmedCont
110116
* Check the user its permissions on the remote list or library
111117
*/
112118
private async checkRemoteListOrLibPermissions() {
113-
const { context, remoteSiteUrl, relativeLibOrListUrl, permissions } = this.props;
119+
const { remoteSiteUrl, relativeLibOrListUrl, permissions } = this.props;
114120
// Check if all properties are provided
115121
if (remoteSiteUrl && relativeLibOrListUrl && permissions) {
116122
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+
}
129129

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+
}
135144

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;
147185
}
186+
} else {
187+
console.error(`No result value was retrieved when checking the user's permissions.`);
188+
return false;
148189
}
149190
}
150191

0 commit comments

Comments
 (0)