UNPKG

7.52 kBJavaScriptView Raw
1import { body } from "@pnp/queryable";
2import { isArray, jsS } from "@pnp/core";
3import { SPCollection, SPInstance } from "../spqueryable.js";
4import { extractWebUrl } from "../utils/extract-web-url.js";
5import { Web } from "../webs/types.js";
6import { SharingRole, RoleType, } from "./types.js";
7import { spPost } from "../operations.js";
8import { RoleDefinitions } from "../security/types.js";
9import { emptyGuid } from "../types.js";
10/**
11 * Shares an object based on the supplied options
12 *
13 * @param options The set of options to send to the ShareObject method
14 * @param bypass If true any processing is skipped and the options are sent directly to the ShareObject method
15 */
16export async function shareObject(o, options, bypass = false) {
17 if (bypass) {
18 // if the bypass flag is set send the supplied parameters directly to the service
19 return sendShareObjectRequest(o, options);
20 }
21 // extend our options with some defaults
22 options = {
23 group: null,
24 includeAnonymousLinkInEmail: false,
25 propagateAcl: false,
26 useSimplifiedRoles: true,
27 ...options,
28 };
29 const roleValue = await getRoleValue.apply(o, [options.role, options.group]);
30 // handle the multiple input types
31 if (!isArray(options.loginNames)) {
32 options.loginNames = [options.loginNames];
33 }
34 const userStr = jsS(options.loginNames.map(Key => ({ Key })));
35 let postBody = {
36 peoplePickerInput: userStr,
37 roleValue: roleValue,
38 url: options.url,
39 };
40 if (options.emailData !== undefined && options.emailData !== null) {
41 postBody = {
42 emailBody: options.emailData.body,
43 emailSubject: options.emailData.subject !== undefined ? options.emailData.subject : "Shared with you.",
44 sendEmail: true,
45 ...postBody,
46 };
47 }
48 return sendShareObjectRequest(o, postBody);
49}
50/**
51 * Gets a sharing link for the supplied
52 *
53 * @param kind The kind of link to share
54 * @param expiration The optional expiration for this link
55 */
56export function getShareLink(kind, expiration = null) {
57 // date needs to be an ISO string or null
58 const expString = expiration !== null ? expiration.toISOString() : null;
59 // clone using the factory and send the request
60 const o = SPInstance(this, "shareLink");
61 return spPost(o, body({
62 request: {
63 createLink: true,
64 emailData: null,
65 settings: {
66 expiration: expString,
67 linkKind: kind,
68 },
69 },
70 }));
71}
72/**
73 * Checks Permissions on the list of Users and returns back role the users have on the Item.
74 *
75 * @param recipients The array of Entities for which Permissions need to be checked.
76 */
77export function checkPermissions(recipients) {
78 const o = SPInstance(this, "checkPermissions");
79 return spPost(o, body({ recipients }));
80}
81/**
82 * Get Sharing Information.
83 *
84 * @param request The SharingInformationRequest Object.
85 * @param expands Expand more fields.
86 *
87 */
88export function getSharingInformation(request = null, expands = [], selects = ["*"]) {
89 const o = SPInstance(this, "getSharingInformation");
90 return spPost(o.select(...selects).expand(...expands), body({ request }));
91}
92/**
93 * Gets the sharing settings of an item.
94 *
95 * @param useSimplifiedRoles Determines whether to use simplified roles.
96 */
97export function getObjectSharingSettings(useSimplifiedRoles = true) {
98 const o = SPInstance(this, "getObjectSharingSettings");
99 return spPost(o, body({ useSimplifiedRoles }));
100}
101/**
102 * Unshares this object
103 */
104export function unshareObject() {
105 return spPost(SPInstance(this, "unshareObject"));
106}
107/**
108 * Deletes a link by type
109 *
110 * @param kind Deletes a sharing link by the kind of link
111 */
112export function deleteLinkByKind(linkKind) {
113 return spPost(SPInstance(this, "deleteLinkByKind"), body({ linkKind }));
114}
115/**
116 * Removes the specified link to the item.
117 *
118 * @param kind The kind of link to be deleted.
119 * @param shareId
120 */
121export function unshareLink(linkKind, shareId = emptyGuid) {
122 return spPost(SPInstance(this, "unshareLink"), body({ linkKind, shareId }));
123}
124/**
125 * Shares this instance with the supplied users
126 *
127 * @param loginNames Resolved login names to share
128 * @param role The role
129 * @param requireSignin True to require the user is authenticated, otherwise false
130 * @param propagateAcl True to apply this share to all children
131 * @param emailData If supplied an email will be sent with the indicated properties
132 */
133export async function shareWith(o, loginNames, role, requireSignin = false, propagateAcl = false, emailData) {
134 // handle the multiple input types
135 if (!isArray(loginNames)) {
136 loginNames = [loginNames];
137 }
138 const userStr = jsS(loginNames.map(login => {
139 return { Key: login };
140 }));
141 const roleFilter = role === SharingRole.Edit ? RoleType.Contributor : RoleType.Reader;
142 // start by looking up the role definition id we need to set the roleValue
143 const def = await SPCollection([o, extractWebUrl(o.toUrl())], "_api/web/roledefinitions").select("Id").filter(`RoleTypeKind eq ${roleFilter}`)();
144 if (!isArray(def) || def.length < 1) {
145 throw Error(`Could not locate a role defintion with RoleTypeKind ${roleFilter}`);
146 }
147 let postBody = {
148 includeAnonymousLinkInEmail: requireSignin,
149 peoplePickerInput: userStr,
150 propagateAcl: propagateAcl,
151 roleValue: `role:${def[0].Id}`,
152 useSimplifiedRoles: true,
153 };
154 if (emailData !== undefined) {
155 postBody = {
156 ...postBody,
157 emailBody: emailData.body,
158 emailSubject: emailData.subject !== undefined ? emailData.subject : "",
159 sendEmail: true,
160 };
161 }
162 return spPost(SPInstance(o, "shareObject"), body(postBody));
163}
164async function sendShareObjectRequest(o, options) {
165 const w = Web([o, extractWebUrl(o.toUrl())], "/_api/SP.Web.ShareObject");
166 return spPost(w.expand("UsersWithAccessRequests", "GroupsSharedWith"), body(options));
167}
168/**
169 * Calculates the roleValue string used in the sharing query
170 *
171 * @param role The Sharing Role
172 * @param group The Group type
173 */
174async function getRoleValue(role, group) {
175 // we will give group precedence, because we had to make a choice
176 if (group !== undefined && group !== null) {
177 switch (group) {
178 case RoleType.Contributor: {
179 const g1 = await Web([this, "_api/web"], "associatedmembergroup").select("Id")();
180 return `group: ${g1.Id}`;
181 }
182 case RoleType.Reader:
183 case RoleType.Guest: {
184 const g2 = await Web([this, "_api/web"], "associatedvisitorgroup").select("Id")();
185 return `group: ${g2.Id}`;
186 }
187 default:
188 throw Error("Could not determine role value for supplied value. Contributor, Reader, and Guest are supported");
189 }
190 }
191 else {
192 const roleFilter = role === SharingRole.Edit ? RoleType.Contributor : RoleType.Reader;
193 const def = await RoleDefinitions([this, "_api/web"]).select("Id").top(1).filter(`RoleTypeKind eq ${roleFilter}`)();
194 if (def === undefined || (def === null || def === void 0 ? void 0 : def.length) < 1) {
195 throw Error("Could not locate associated role definition for supplied role. Edit and View are supported");
196 }
197 return `role: ${def[0].Id}`;
198 }
199}
200//# sourceMappingURL=funcs.js.map
\No newline at end of file