1 | import { body } from "@pnp/queryable";
|
2 | import { isArray, jsS } from "@pnp/core";
|
3 | import { SPCollection, SPInstance } from "../spqueryable.js";
|
4 | import { extractWebUrl } from "../utils/extract-web-url.js";
|
5 | import { Web } from "../webs/types.js";
|
6 | import { SharingRole, RoleType, } from "./types.js";
|
7 | import { spPost } from "../operations.js";
|
8 | import { RoleDefinitions } from "../security/types.js";
|
9 | import { emptyGuid } from "../types.js";
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | export async function shareObject(o, options, bypass = false) {
|
17 | if (bypass) {
|
18 |
|
19 | return sendShareObjectRequest(o, options);
|
20 | }
|
21 |
|
22 | options = {
|
23 | group: null,
|
24 | includeAnonymousLinkInEmail: false,
|
25 | propagateAcl: false,
|
26 | useSimplifiedRoles: true,
|
27 | ...options,
|
28 | };
|
29 | const roleValue = await getRoleValue(options.role, options.group);
|
30 |
|
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 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | export function getShareLink(kind, expiration = null) {
|
57 |
|
58 | const expString = expiration !== null ? expiration.toISOString() : null;
|
59 |
|
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 |
|
74 |
|
75 |
|
76 |
|
77 | export function checkPermissions(recipients) {
|
78 | const o = SPInstance(this, "checkPermissions");
|
79 | return spPost(o, body({ recipients }));
|
80 | }
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | export function getSharingInformation(request = null, expands = []) {
|
89 | const o = SPInstance(this, "getSharingInformation");
|
90 | return spPost(o.expand(...expands), body({ request }));
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | export function getObjectSharingSettings(useSimplifiedRoles = true) {
|
98 | const o = SPInstance(this, "getObjectSharingSettings");
|
99 | return spPost(o, body({ useSimplifiedRoles }));
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 | export function unshareObject() {
|
105 | return spPost(SPInstance(this, "unshareObject"));
|
106 | }
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | export function deleteLinkByKind(linkKind) {
|
113 | return spPost(SPInstance(this, "deleteLinkByKind"), body({ linkKind }));
|
114 | }
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | export function unshareLink(linkKind, shareId = emptyGuid) {
|
122 | return spPost(SPInstance(this, "unshareLink"), body({ linkKind, shareId }));
|
123 | }
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | export async function shareWith(o, loginNames, role, requireSignin = false, propagateAcl = false, emailData) {
|
134 |
|
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 |
|
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 | }
|
164 | async function sendShareObjectRequest(o, options) {
|
165 | const w = Web([this, extractWebUrl(o.toUrl())]);
|
166 | return spPost(w.expand("UsersWithAccessRequests", "GroupsSharedWith"), body(options));
|
167 | }
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 | async function getRoleValue(role, group) {
|
175 |
|
176 | if (group !== undefined && group !== null) {
|
177 | switch (group) {
|
178 | case RoleType.Contributor: {
|
179 | const g1 = await Web("_api/web", "associatedmembergroup").select("Id")();
|
180 | return `group: ${g1.Id}`;
|
181 | }
|
182 | case RoleType.Reader:
|
183 | case RoleType.Guest: {
|
184 | const g2 = await Web("_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("_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 |
|
\ | No newline at end of file |