UNPKG

@pnp/sp

Version:

pnp - provides a fluent api for working with SharePoint REST

82 lines 2.95 kB
import { PermissionKind } from "./types.js"; import { SPInstance, SPQueryable } from "../spqueryable.js"; import { spPost } from "../operations.js"; /** * Gets the effective permissions for the user supplied * * @param loginName The claims username for the user (ex: i:0#.f|membership|user@domain.com) */ export async function getUserEffectivePermissions(loginName) { const q = SPInstance(this, "getUserEffectivePermissions(@user)"); q.query.set("@user", `'${loginName}'`); return q(); } /** * Gets the effective permissions for the current user */ export async function getCurrentUserEffectivePermissions() { return SPQueryable(this, "EffectiveBasePermissions")(); } /** * Breaks the security inheritance at this level optinally copying permissions and clearing subscopes * * @param copyRoleAssignments If true the permissions are copied from the current parent scope * @param clearSubscopes Optional. true to make all child securable objects inherit role assignments from the current object */ export async function breakRoleInheritance(copyRoleAssignments = false, clearSubscopes = false) { await spPost(SPQueryable(this, `breakroleinheritance(copyroleassignments=${copyRoleAssignments}, clearsubscopes=${clearSubscopes})`)); } /** * Removes the local role assignments so that it re-inherit role assignments from the parent object. * */ export async function resetRoleInheritance() { await spPost(SPQueryable(this, "resetroleinheritance")); } /** * Determines if a given user has the appropriate permissions * * @param loginName The user to check * @param permission The permission being checked */ export async function userHasPermissions(loginName, permission) { const perms = await getUserEffectivePermissions.call(this, loginName); return this.hasPermissions(perms, permission); } /** * Determines if the current user has the requested permissions * * @param permission The permission we wish to check */ export async function currentUserHasPermissions(permission) { const perms = await getCurrentUserEffectivePermissions.call(this); return this.hasPermissions(perms, permission); } /** * Taken from sp.js, checks the supplied permissions against the mask * * @param value The security principal's permissions on the given object * @param perm The permission checked against the value */ /* eslint-disable no-bitwise */ export function hasPermissions(value, perm) { if (!perm) { return true; } if (perm === PermissionKind.FullMask) { return (value.High & 32767) === 32767 && value.Low === 65535; } perm = perm - 1; let num = 1; if (perm >= 0 && perm < 32) { num = num << perm; return 0 !== (value.Low & num); } else if (perm >= 32 && perm < 64) { num = num << perm - 32; return 0 !== (value.High & num); } return false; } /* eslint-enable no-bitwise */ //# sourceMappingURL=funcs.js.map