UNPKG

2.95 kBJavaScriptView Raw
1import { PermissionKind } from "./types.js";
2import { SPInstance, SPQueryable } from "../spqueryable.js";
3import { spPost } from "../operations.js";
4/**
5* Gets the effective permissions for the user supplied
6*
7* @param loginName The claims username for the user (ex: i:0#.f|membership|user@domain.com)
8*/
9export async function getUserEffectivePermissions(loginName) {
10 const q = SPInstance(this, "getUserEffectivePermissions(@user)");
11 q.query.set("@user", `'${loginName}'`);
12 return q();
13}
14/**
15 * Gets the effective permissions for the current user
16 */
17export async function getCurrentUserEffectivePermissions() {
18 return SPQueryable(this, "EffectiveBasePermissions")();
19}
20/**
21 * Breaks the security inheritance at this level optinally copying permissions and clearing subscopes
22 *
23 * @param copyRoleAssignments If true the permissions are copied from the current parent scope
24 * @param clearSubscopes Optional. true to make all child securable objects inherit role assignments from the current object
25 */
26export async function breakRoleInheritance(copyRoleAssignments = false, clearSubscopes = false) {
27 await spPost(SPQueryable(this, `breakroleinheritance(copyroleassignments=${copyRoleAssignments}, clearsubscopes=${clearSubscopes})`));
28}
29/**
30 * Removes the local role assignments so that it re-inherit role assignments from the parent object.
31 *
32 */
33export async function resetRoleInheritance() {
34 await spPost(SPQueryable(this, "resetroleinheritance"));
35}
36/**
37 * Determines if a given user has the appropriate permissions
38 *
39 * @param loginName The user to check
40 * @param permission The permission being checked
41 */
42export async function userHasPermissions(loginName, permission) {
43 const perms = await getUserEffectivePermissions.call(this, loginName);
44 return this.hasPermissions(perms, permission);
45}
46/**
47 * Determines if the current user has the requested permissions
48 *
49 * @param permission The permission we wish to check
50 */
51export async function currentUserHasPermissions(permission) {
52 const perms = await getCurrentUserEffectivePermissions.call(this);
53 return this.hasPermissions(perms, permission);
54}
55/**
56 * Taken from sp.js, checks the supplied permissions against the mask
57 *
58 * @param value The security principal's permissions on the given object
59 * @param perm The permission checked against the value
60 */
61/* eslint-disable no-bitwise */
62export function hasPermissions(value, perm) {
63 if (!perm) {
64 return true;
65 }
66 if (perm === PermissionKind.FullMask) {
67 return (value.High & 32767) === 32767 && value.Low === 65535;
68 }
69 perm = perm - 1;
70 let num = 1;
71 if (perm >= 0 && perm < 32) {
72 num = num << perm;
73 return 0 !== (value.Low & num);
74 }
75 else if (perm >= 32 && perm < 64) {
76 num = num << perm - 32;
77 return 0 !== (value.High & num);
78 }
79 return false;
80}
81/* eslint-enable no-bitwise */
82//# sourceMappingURL=funcs.js.map
\No newline at end of file