@kwiz/common 1.0.132 → 1.0.133

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/package.json +1 -1
  2. package/.github/workflows/npm-publish.yml +0 -24
  3. package/src/_dependencies.ts +0 -13
  4. package/src/config.ts +0 -19
  5. package/src/exports-index.ts +0 -4
  6. package/src/helpers/Guid.ts +0 -182
  7. package/src/helpers/base64.ts +0 -174
  8. package/src/helpers/browser.test.js +0 -13
  9. package/src/helpers/browser.ts +0 -1505
  10. package/src/helpers/browserinfo.ts +0 -293
  11. package/src/helpers/collections.base.test.js +0 -26
  12. package/src/helpers/collections.base.ts +0 -438
  13. package/src/helpers/collections.ts +0 -108
  14. package/src/helpers/color.ts +0 -55
  15. package/src/helpers/cookies.ts +0 -60
  16. package/src/helpers/date.test.js +0 -120
  17. package/src/helpers/date.ts +0 -189
  18. package/src/helpers/debug.ts +0 -187
  19. package/src/helpers/diagrams.ts +0 -44
  20. package/src/helpers/emails.ts +0 -7
  21. package/src/helpers/eval.ts +0 -5
  22. package/src/helpers/exports-index.ts +0 -30
  23. package/src/helpers/file.test.js +0 -51
  24. package/src/helpers/file.ts +0 -64
  25. package/src/helpers/flatted.ts +0 -150
  26. package/src/helpers/functions.ts +0 -17
  27. package/src/helpers/graph/calendar.types.ts +0 -11
  28. package/src/helpers/graph/exports-index.ts +0 -1
  29. package/src/helpers/http.ts +0 -70
  30. package/src/helpers/images.ts +0 -23
  31. package/src/helpers/json.ts +0 -45
  32. package/src/helpers/md5.ts +0 -190
  33. package/src/helpers/objects.test.js +0 -34
  34. package/src/helpers/objects.ts +0 -275
  35. package/src/helpers/promises.test.js +0 -38
  36. package/src/helpers/promises.ts +0 -166
  37. package/src/helpers/random.ts +0 -27
  38. package/src/helpers/scheduler/exports-index.ts +0 -1
  39. package/src/helpers/scheduler/scheduler.test.js +0 -104
  40. package/src/helpers/scheduler/scheduler.ts +0 -132
  41. package/src/helpers/sharepoint.ts +0 -797
  42. package/src/helpers/strings.test.js +0 -123
  43. package/src/helpers/strings.ts +0 -338
  44. package/src/helpers/typecheckers.test.js +0 -35
  45. package/src/helpers/typecheckers.ts +0 -267
  46. package/src/helpers/url.test.js +0 -44
  47. package/src/helpers/url.ts +0 -208
  48. package/src/helpers/urlhelper.ts +0 -112
  49. package/src/index.ts +0 -6
  50. package/src/types/auth.ts +0 -63
  51. package/src/types/common.types.ts +0 -16
  52. package/src/types/exports-index.ts +0 -15
  53. package/src/types/flatted.types.ts +0 -60
  54. package/src/types/globals.types.ts +0 -7
  55. package/src/types/graph/calendar.types.ts +0 -81
  56. package/src/types/graph/exports-index.ts +0 -1
  57. package/src/types/knownscript.types.ts +0 -19
  58. package/src/types/libs/datajs.types.ts +0 -29
  59. package/src/types/libs/exports-index.ts +0 -3
  60. package/src/types/libs/ics.types.ts +0 -31
  61. package/src/types/libs/msal.types.ts +0 -58
  62. package/src/types/locales.ts +0 -126
  63. package/src/types/localstoragecache.types.ts +0 -9
  64. package/src/types/location.types.ts +0 -28
  65. package/src/types/moment.ts +0 -12
  66. package/src/types/regex.types.ts +0 -17
  67. package/src/types/rest.types.ts +0 -96
  68. package/src/types/sharepoint.types.ts +0 -1467
  69. package/src/types/sharepoint.utils.types.ts +0 -307
  70. package/src/utils/auth/common.ts +0 -119
  71. package/src/utils/auth/discovery.test.js +0 -13
  72. package/src/utils/auth/discovery.ts +0 -132
  73. package/src/utils/auth/exports-index.ts +0 -2
  74. package/src/utils/base64.ts +0 -28
  75. package/src/utils/consolelogger.ts +0 -334
  76. package/src/utils/date.ts +0 -173
  77. package/src/utils/emails.ts +0 -25
  78. package/src/utils/exports-index.ts +0 -11
  79. package/src/utils/knownscript.ts +0 -287
  80. package/src/utils/localstoragecache.ts +0 -447
  81. package/src/utils/rest.ts +0 -502
  82. package/src/utils/script.ts +0 -171
  83. package/src/utils/sharepoint.rest/common.ts +0 -160
  84. package/src/utils/sharepoint.rest/date.ts +0 -63
  85. package/src/utils/sharepoint.rest/exports-index.ts +0 -11
  86. package/src/utils/sharepoint.rest/file.folder.ts +0 -685
  87. package/src/utils/sharepoint.rest/item.ts +0 -547
  88. package/src/utils/sharepoint.rest/list.ts +0 -1572
  89. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +0 -775
  90. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +0 -275
  91. package/src/utils/sharepoint.rest/listutils/common.ts +0 -207
  92. package/src/utils/sharepoint.rest/listutils/exports-index.ts +0 -3
  93. package/src/utils/sharepoint.rest/location.ts +0 -142
  94. package/src/utils/sharepoint.rest/navigation-links.ts +0 -87
  95. package/src/utils/sharepoint.rest/user-search.ts +0 -253
  96. package/src/utils/sharepoint.rest/user.ts +0 -559
  97. package/src/utils/sharepoint.rest/web.ts +0 -1385
  98. package/src/utils/sod.ts +0 -195
@@ -1,559 +0,0 @@
1
- import { filterEmptyEntries, firstOrNull, lastOrNull, normalizeGuid } from "../../exports-index";
2
- import { jsonStringify } from "../../helpers/json";
3
- import { ISPPeoplePickerControlFormEntity, IsSPPeoplePickerControlFormEntity, getPrincipalTypeFromPickerEntity, isExternalUser } from "../../helpers/sharepoint";
4
- import { isNotEmptyArray, isNotEmptyString, isNullOrEmptyArray, isNullOrEmptyString, isNullOrNaN, isNullOrUndefined, isNumber } from "../../helpers/typecheckers";
5
- import { encodeURIComponentEX } from "../../helpers/url";
6
- import { contentTypes, jsonTypes } from "../../types/rest.types";
7
- import { ISiteGroupInfo, PrincipalType } from "../../types/sharepoint.types";
8
- import { IGroupInfo, IUserGroupInfo, IUserInfo } from "../../types/sharepoint.utils.types";
9
- import { ConsoleLogger } from "../consolelogger";
10
- import { GetJson, GetJsonSync, longLocalCache, shortLocalCache } from "../rest";
11
- import { GetRestBaseUrl, GetSiteUrl } from "./common";
12
- import { GetSiteId } from "./web";
13
-
14
- const logger = ConsoleLogger.get("utils/sharepoint/user");
15
- var __currentUserId: number = null;
16
- const groupSelect = "Id,Title,Description,CanCurrentUserViewMembership,OnlyAllowMembersViewMembership,IsHiddenInUI,OwnerTitle";
17
- const userSelect = "PrincipalType,Id,LoginName,UserPrincipalName,Title,IsSiteAdmin,Email";
18
-
19
-
20
- /** Get user login name */
21
- export function GetUserLoginName(siteUrl?: string): Promise<string> {
22
- siteUrl = GetSiteUrl(siteUrl);
23
-
24
- if (typeof (_spPageContextInfo) !== "undefined" && typeof (_spPageContextInfo.userPrincipalName) !== "undefined")
25
- //issue 6309 _spPageContextInfo.userLoginName is wrong for external users
26
- return Promise.resolve(_spPageContextInfo.userPrincipalName);
27
-
28
- return GetJson<{ d: { LoginName: string; }; }>(GetRestBaseUrl(siteUrl) + "/web/currentUser/loginName", null, { ...longLocalCache })
29
- .then(r => r.d.LoginName)
30
- .catch<string>(() => null);
31
- }
32
-
33
- /** Get user login name syncronously */
34
- export function GetUserLoginNameSync(siteUrl?: string): string {
35
- siteUrl = GetSiteUrl(siteUrl);
36
-
37
- if (typeof (_spPageContextInfo) !== "undefined" && typeof (_spPageContextInfo.userPrincipalName) !== "undefined")
38
- //issue 6309 _spPageContextInfo.userLoginName is wrong for external users
39
- return _spPageContextInfo.userPrincipalName;
40
-
41
- let res = GetJsonSync<{ d: { LoginName: string; }; }>(GetRestBaseUrl(siteUrl) + "/web/currentUser/loginName", null, { ...longLocalCache });
42
- if (res.success)
43
- return res.result.d.LoginName;
44
- else return null;
45
- }
46
-
47
- function _getCurrentUserRequestUrl(siteUrl: string, expandGroups: boolean) {
48
- siteUrl = GetSiteUrl(siteUrl);
49
-
50
- var url = `${GetRestBaseUrl(siteUrl)}/web/currentUser${expandGroups ? '?$expand=Groups' : ''}`;
51
- return url;
52
- }
53
-
54
- export async function GetCurrentUser(siteUrl?: string, options?: { expandGroups: boolean; refreshCache?: boolean; }): Promise<IUserInfo> {
55
- siteUrl = GetSiteUrl(siteUrl);
56
-
57
- return GetJson<IUserInfo>(_getCurrentUserRequestUrl(siteUrl, options && options.expandGroups), null,
58
- {
59
- ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
60
- allowCache: !options || options.refreshCache !== true
61
- })
62
- .then(user => {
63
- if (user)
64
- __currentUserId = user.Id;
65
- return user;
66
- })
67
- .catch<IUserInfo>(() => null);
68
- }
69
-
70
- export function GetCurrentUserSync(siteUrl?: string, options?: {
71
- /** expand groups only includes SP groups the user is a direct member of. It does not include groups associated through a security group membership, teams or M365 group */
72
- expandGroups: boolean;
73
- }): IUserInfo {
74
- siteUrl = GetSiteUrl(siteUrl);
75
-
76
- let res = GetJsonSync<IUserInfo>(_getCurrentUserRequestUrl(siteUrl, options && options.expandGroups), null,
77
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
78
- if (res.success) {
79
- let user = res.result;
80
- if (user)
81
- __currentUserId = user.Id;
82
-
83
- return user;
84
- }
85
- else return null;
86
- }
87
-
88
- function _getUserRequestUrl(siteUrl: string, userId: number, expandGroups: boolean) {
89
- siteUrl = GetSiteUrl(siteUrl);
90
-
91
- var url = `${GetRestBaseUrl(siteUrl)}/web/GetUserById(${userId})${expandGroups ? '?expand=Groups' : ''}`;
92
- return url;
93
- }
94
-
95
- export async function GetUser(siteUrl?: string, userId?: number, options?: {
96
- /** expand groups only includes SP groups the user is a direct member of. It does not include groups associated through a security group membership, teams or M365 group */
97
- expandGroups: boolean;
98
- }): Promise<IUserInfo> {
99
- siteUrl = GetSiteUrl(siteUrl);
100
-
101
- if (isNullOrNaN(userId) || __currentUserId === userId) return GetCurrentUser(siteUrl, options);
102
- return GetJson<IUserInfo>(_getUserRequestUrl(siteUrl, userId, options && options.expandGroups), null, {
103
- ...shortLocalCache, jsonMetadata: jsonTypes.nometadata
104
- }).then(user => {
105
- return user;
106
- }).catch<IUserInfo>(() => null);
107
- }
108
-
109
- export function GetUserSync(siteUrl?: string, userId?: number, options?: { expandGroups: boolean; }): IUserInfo {
110
- siteUrl = GetSiteUrl(siteUrl);
111
-
112
- if (isNullOrNaN(userId) || __currentUserId === userId) return GetCurrentUserSync(siteUrl, options);
113
-
114
- let res = GetJsonSync<IUserInfo>(_getUserRequestUrl(siteUrl, userId, options && options.expandGroups), null,
115
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
116
- if (res.success) {
117
- let user = res.result;
118
- return user;
119
- }
120
- else return null;
121
- }
122
-
123
- function _getUserByLoginNameRequestUrl(siteUrl: string, loginName: string, expandGroups: boolean) {
124
- siteUrl = GetSiteUrl(siteUrl);
125
-
126
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteUsers/getByLoginName(@u)?@u='${encodeURIComponentEX(loginName, { singleQuoteMultiplier: 2 })}'${expandGroups ? '&expand=Groups' : ''}`;
127
- return url;
128
- }
129
-
130
- export async function GetUserByLogin(siteUrl?: string, loginName?: string, options?: { expandGroups: boolean; }): Promise<IUserInfo> {
131
- siteUrl = GetSiteUrl(siteUrl);
132
-
133
- if (isNullOrEmptyString(loginName)) {
134
- return GetCurrentUser(siteUrl, options);
135
- }
136
-
137
- return GetJson<IUserInfo>(_getUserByLoginNameRequestUrl(siteUrl, loginName, options && options.expandGroups), null,
138
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata })
139
- .then(user => user)
140
- .catch<IUserInfo>(() => null);
141
- }
142
-
143
- export function GetUserByLoginSync(siteUrl?: string, loginName?: string, options?: { expandGroups: boolean; }): IUserInfo {
144
- siteUrl = GetSiteUrl(siteUrl);
145
-
146
- if (isNullOrEmptyString(loginName)) {
147
- return GetCurrentUserSync(siteUrl, options);
148
- }
149
-
150
- let res = GetJsonSync<IUserInfo>(_getUserByLoginNameRequestUrl(siteUrl, loginName, options && options.expandGroups), null,
151
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
152
- if (res.success) {
153
- let user = res.result;
154
- return user;
155
- }
156
-
157
- return null;
158
- }
159
-
160
- function _getEnsureUserRequestUrl(siteUrl: string, loginName: string, expandGroups?: boolean) {
161
- siteUrl = GetSiteUrl(siteUrl);
162
-
163
- var url = `${GetRestBaseUrl(siteUrl)}/web/ensureUser(@u)?@u='${encodeURIComponentEX(loginName, { singleQuoteMultiplier: 2 })}'${expandGroups ? '&expand=Groups' : ''}`;
164
- return url;
165
- }
166
-
167
- export async function EnsureUser(siteUrl: string, userLogin: string, options?: { expandGroups: boolean; }): Promise<IUserInfo> {
168
- siteUrl = GetSiteUrl(siteUrl);
169
-
170
- if (isNullOrEmptyString(userLogin)) return null;
171
-
172
- return GetJson<IUserInfo>(_getEnsureUserRequestUrl(siteUrl, userLogin, options && options.expandGroups), null,
173
- { method: "POST", spWebUrl: siteUrl, jsonMetadata: jsonTypes.nometadata, ...shortLocalCache })
174
- .then(user => {
175
- return user;
176
- })
177
- .catch<IUserInfo>(() => null);
178
- }
179
-
180
- export function EnsureUserSync(siteUrl: string, userLogin: string, options?: { expandGroups: boolean; }): IUserInfo {
181
- siteUrl = GetSiteUrl(siteUrl);
182
-
183
- if (isNullOrEmptyString(userLogin)) return null;
184
-
185
- let res = GetJsonSync<IUserInfo>(_getEnsureUserRequestUrl(siteUrl, userLogin, options && options.expandGroups), null,
186
- { method: "POST", spWebUrl: siteUrl, jsonMetadata: jsonTypes.nometadata, ...shortLocalCache });
187
- if (res.success) {
188
- let user = res.result;
189
- return user;
190
- }
191
- return null;
192
- }
193
-
194
- export function GetOrEnsureUserByLoginSync(siteUrl: string, key: string, options?: { expandGroups: boolean; }) {
195
- let userValue = GetUserByLoginSync(siteUrl, key, options);
196
- if (!userValue) {
197
- userValue = EnsureUserSync(siteUrl, key, options);
198
- }
199
- return userValue;
200
- }
201
-
202
- export async function GetSecurityGroupByTitle(siteUrl: string, title: string): Promise<IUserInfo> {
203
- siteUrl = GetSiteUrl(siteUrl);
204
- //on premise the title/name of security group could be as domain login
205
- //for example, 'KWIZCOM\ad_qa_group'
206
- //split[0] = will contain the domain name (KWIZCOM)
207
- //split[1] = will contain the title (ad_qa_group)
208
- //if split[1] is null, then we didn't get a domain login and the split[0] will just contain the title/name (ad_qa_group) of the group
209
- var split = title.split("\\");
210
- var groupTitle = (split[1] || split[0]).toLowerCase();
211
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteusers?$filter=PrincipalType eq ${PrincipalType.SecurityGroup}`;
212
-
213
- return GetJson<{ value: IUserInfo[]; }>(url, null,
214
- { method: "GET", jsonMetadata: jsonTypes.nometadata, ...shortLocalCache })
215
- .then(securityGroupsResult => {
216
- var securityGroup: IUserInfo = null;
217
- if (securityGroupsResult && securityGroupsResult.value && securityGroupsResult.value.length) {
218
- //first match the full title and fall back to the split title/name
219
- securityGroup = securityGroupsResult.value.filter((secGroup) => {
220
- //this will find security groups on premise where the title/name are saved as 'KWIZCOM\ad_qa_group'
221
- //but will not match when exporting from on premise to online
222
- return secGroup.Title.toLowerCase() === title.toLowerCase();
223
- })[0] || securityGroupsResult.value.filter((secGroup) => {
224
- //this will match settings exported from on premise to online where the title/name of the group changes from 'KWIZCOM\ad_qa_group' to 'AD_QA_GROUP'
225
- return secGroup.Title.toLowerCase() === groupTitle;
226
- })[0];
227
- }
228
- return securityGroup;
229
- })
230
- .catch<IUserInfo>(() => null);
231
- }
232
-
233
- export function GetSecurityGroupByTitleSync(siteUrl: string, title: string): IUserInfo {
234
- siteUrl = GetSiteUrl(siteUrl);
235
- //on premise the title/name of security group could be as domain login
236
- //for example, 'KWIZCOM\ad_qa_group'
237
- //split[0] = will contain the domain name (KWIZCOM)
238
- //split[1] = will contain the title (ad_qa_group)
239
- //if split[1] is null, then we didn't get a domain login and the split[0] will just contain the title/name (ad_qa_group) of the group
240
- var split = title.split("\\");
241
- var groupTitle = (split[1] || split[0]).toLowerCase();
242
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteusers?$filter=PrincipalType eq ${PrincipalType.SecurityGroup}`;
243
-
244
- let securityGroupsResult = GetJsonSync<{ value: IUserInfo[]; }>(url, null,
245
- { method: "GET", jsonMetadata: jsonTypes.nometadata, ...shortLocalCache });
246
-
247
- if (securityGroupsResult && securityGroupsResult.success) {
248
- var securityGroup: IUserInfo = null;
249
- if (securityGroupsResult && securityGroupsResult.result && securityGroupsResult.result.value && securityGroupsResult.result.value.length) {
250
- //first match the full title and fall back to the split title/name
251
- securityGroup = securityGroupsResult.result.value.filter((secGroup) => {
252
- //this will find security groups on premise where the title/name are saved as 'KWIZCOM\ad_qa_group'
253
- //but will not match when exporting from on premise to online
254
- return secGroup.Title.toLowerCase() === title.toLowerCase();
255
- })[0] || securityGroupsResult.result.value.filter((secGroup) => {
256
- //this will match settings exported from on premise to online where the title/name of the group changes from 'KWIZCOM\ad_qa_group' to 'AD_QA_GROUP'
257
- return secGroup.Title.toLowerCase() === groupTitle;
258
- })[0];
259
- }
260
- return securityGroup;
261
- }
262
- return null;
263
- }
264
-
265
- function _getGroupRequestUrl(siteUrl: string, groupId: number) {
266
- siteUrl = GetSiteUrl(siteUrl);
267
-
268
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups/getById(${groupId})?$select=${groupSelect}`;
269
- return url;
270
- }
271
-
272
- function _getGroupUsersRequestUrl(siteUrl: string, groupId: number) {
273
- siteUrl = GetSiteUrl(siteUrl);
274
-
275
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups/getById(${groupId})/Users?$select=${userSelect}`;
276
- return url;
277
- }
278
-
279
- export async function GetGroup(siteUrl?: string, groupId?: number, options?: {
280
- expandUsers: boolean;
281
- refreshCache?: boolean;
282
- }): Promise<IGroupInfo> {
283
- siteUrl = GetSiteUrl(siteUrl);
284
-
285
- return GetJson<IGroupInfo>(_getGroupRequestUrl(siteUrl, groupId), null,
286
- {
287
- ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
288
- allowCache: !options || options.refreshCache !== true
289
- })
290
- .then(async group => {
291
- if (group) {
292
- group.PrincipalType = PrincipalType.SharePointGroup;
293
- group.LoginName = group.Title;
294
- if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
295
- let users = await GetJson<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, groupId), null, {
296
- ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
297
- allowCache: !options || options.refreshCache !== true
298
- });
299
- group.Users = users && users.value;
300
- }
301
- }
302
- return group;
303
- })
304
- .catch<IGroupInfo>(() => null);
305
- }
306
-
307
- export function GetGroupSync(siteUrl?: string, groupId?: number, options?: { expandUsers: boolean; }): IGroupInfo {
308
- siteUrl = GetSiteUrl(siteUrl);
309
-
310
- let res = GetJsonSync<IGroupInfo>(_getGroupRequestUrl(siteUrl, groupId), null,
311
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
312
- if (res.success) {
313
- let group = res.result;
314
- if (group) {
315
- group.PrincipalType = PrincipalType.SharePointGroup;
316
- group.LoginName = group.Title;
317
- if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
318
- let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, groupId), null, { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
319
- group.Users = users.success && users.result && users.result.value;
320
- }
321
- }
322
- return group;
323
- }
324
- else return null;
325
- }
326
-
327
- function _getGroupsRequestUrl(siteUrl: string) {
328
- siteUrl = GetSiteUrl(siteUrl);
329
- var url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups?$select=${groupSelect}`;
330
- return url;
331
- }
332
-
333
- function _getGroupByNameRequestUrl(siteUrl: string, groupName: string) {
334
- var url = `${_getGroupsRequestUrl(siteUrl)}&$filter=LoginName eq '${encodeURIComponentEX(groupName, { singleQuoteMultiplier: 2 })}'`;
335
- return url;
336
- }
337
-
338
- export async function GetGroupByName(siteUrl: string, groupName: string, options?: {
339
- expandUsers: boolean;
340
- refreshCache?: boolean;
341
- }): Promise<IGroupInfo> {
342
- siteUrl = GetSiteUrl(siteUrl);
343
-
344
- let res = await GetJson<{ d: { results: IGroupInfo[]; }; }>(_getGroupByNameRequestUrl(siteUrl, groupName), null,
345
- { ...shortLocalCache, allowCache: !options || options.refreshCache !== true });
346
-
347
- if (res) {
348
- let group = res && res.d && res.d.results && res.d.results[0];
349
- if (group) {
350
- group.PrincipalType = PrincipalType.SharePointGroup;
351
- group.LoginName = group.Title;
352
- if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
353
- let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, group.Id), null, {
354
- ...shortLocalCache, jsonMetadata: jsonTypes.nometadata,
355
- allowCache: !options || options.refreshCache !== true
356
- });
357
- group.Users = users.success && users.result && users.result.value;
358
- }
359
- }
360
- return group;
361
- }
362
- else return null;
363
- }
364
-
365
- export function GetGroupByNameSync(siteUrl: string, groupName: string, options?: { expandUsers: boolean; }): IGroupInfo {
366
- siteUrl = GetSiteUrl(siteUrl);
367
-
368
- let res = GetJsonSync<{ value: IGroupInfo[]; }>(_getGroupByNameRequestUrl(siteUrl, groupName), null,
369
- { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
370
- if (res.success) {
371
- let group = res.result && res.result.value && res.result.value[0];
372
- if (group) {
373
- group.PrincipalType = PrincipalType.SharePointGroup;
374
- group.LoginName = group.Title;
375
- if (options && options.expandUsers && group.CanCurrentUserViewMembership) {
376
- let users = GetJsonSync<{ value: IUserInfo[]; }>(_getGroupUsersRequestUrl(siteUrl, group.Id), null, { ...shortLocalCache, jsonMetadata: jsonTypes.nometadata });
377
- group.Users = users.success && users.result && users.result.value;
378
- }
379
- }
380
- return group;
381
- }
382
- else return null;
383
- }
384
-
385
- export async function GetSiteGroups(siteUrl: string, refreshCache?: boolean) {
386
- siteUrl = GetSiteUrl(siteUrl);
387
- let res = await GetJson<{ d: { results: IGroupInfo[]; }; }>(_getGroupsRequestUrl(siteUrl), null,
388
- { ...shortLocalCache, allowCache: refreshCache !== true });
389
-
390
- if (res) {
391
- let groups = res && res.d && res.d.results || [];
392
- groups.forEach(g => {
393
- g.PrincipalType = PrincipalType.SharePointGroup;
394
- g.LoginName = g.Title;
395
- });
396
- return groups;
397
- }
398
- else return [];
399
- }
400
-
401
- export function GetInfoFromSPPeoplePickerControlFormEntity(entity: ISPPeoplePickerControlFormEntity): IUserInfo | IGroupInfo {
402
- if (IsSPPeoplePickerControlFormEntity(entity)) {
403
- var principalType = getPrincipalTypeFromPickerEntity(entity);
404
- if (isNullOrUndefined(principalType)) {
405
- let userValue = GetOrEnsureUserByLoginSync(null, entity.Key);
406
- if (userValue) {
407
- return userValue;
408
- }
409
- let groupValue = GetGroupByNameSync(null, entity.Key);
410
- if (groupValue) {
411
- return groupValue;
412
- }
413
- } else if (principalType === PrincipalType.SharePointGroup) {
414
- return GetGroupByNameSync(null, entity.Key);
415
- } else {
416
- return GetOrEnsureUserByLoginSync(null, entity.Key);
417
- }
418
- }
419
- return null;
420
- }
421
-
422
- export async function CreateSiteGroup(siteUrl: string, info: { name: string, description: string }): Promise<ISiteGroupInfo> {
423
- let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups`;
424
- let createGroup = await GetJson<{ d: ISiteGroupInfo }>(url, jsonStringify({
425
- __metadata: {
426
- type: "SP.Group"
427
- },
428
- Title: info.name,
429
- Description: info.description
430
- }), { allowCache: false });
431
- return createGroup.d;
432
- }
433
- export async function AddUserToGroup(siteUrl: string, groupId: number, userIdOrLogin: number | string): Promise<void> {
434
- let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups(${groupId})/users`;
435
- if (isNumber(userIdOrLogin)) {
436
- let ensured = await GetUser(siteUrl, userIdOrLogin);
437
- userIdOrLogin = ensured.LoginName;
438
- }
439
-
440
- await GetJson(url, jsonStringify({
441
- LoginName: userIdOrLogin
442
- }), { allowCache: false, jsonMetadata: jsonTypes.nometadata });
443
-
444
- }
445
- export async function RemoveUserFromGroup(siteUrl: string, groupId: number, userId: number): Promise<void> {
446
- let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups(${groupId})/users/removeById(${userId})`;
447
- await GetJson(url, null, { method: "POST", allowCache: false, jsonMetadata: jsonTypes.nometadata });
448
- }
449
-
450
- export async function SetGroupOwner(siteUrl: string, groupId: number, ownerId: number, ownerIsAGroup?: boolean) {
451
- //https://github.com/SharePoint/sp-dev-docs/issues/5031#issuecomment-594710013
452
- //if owner is a group - rest API doens't work.
453
- if (ownerIsAGroup !== true) {
454
- let url = `${GetRestBaseUrl(siteUrl)}/web/siteGroups/getById('${groupId}')/SetUserAsOwner(${ownerId})`;
455
- try {
456
- await GetJson<{ 'odata.null': true }>(url, null, { jsonMetadata: jsonTypes.nometadata, method: "POST" });
457
- return true;
458
- } catch (e) {
459
- logger.error(`SetGroupOwner ${groupId} ${ownerId} error:`);
460
- logger.error(e);
461
- return false;
462
- }
463
- }
464
- else {
465
- try {
466
- let soapUrl = `${GetSiteUrl(siteUrl)}_vti_bin/client.svc/ProcessQuery`;
467
- let siteId = await GetSiteId(siteUrl);
468
- let serviceJSONResponse = await GetJson<{ ErrorInfo?: string }[]>(soapUrl, `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="15.0.0.0" ApplicationName=".NET Library" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">
469
- <Actions>
470
- <SetProperty Id="1" ObjectPathId="2" Name="Owner">
471
- <Parameter ObjectPathId="3" />
472
- </SetProperty>
473
- <Method Name="Update" Id="4" ObjectPathId="2" />
474
- </Actions>
475
- <ObjectPaths>
476
- <Identity Id="2" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:${siteId}:g:${groupId}" />
477
- <Identity Id="3" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:${siteId}:g:${ownerId}" />
478
- </ObjectPaths>
479
- </Request>`, {
480
- headers: {
481
- Accept: jsonTypes.standard,
482
- "content-type": contentTypes.xml
483
- }
484
- });
485
- //logger.json(serviceJSONResponse, "soap result");
486
- return isNullOrEmptyArray(serviceJSONResponse) || isNullOrEmptyString(serviceJSONResponse[0].ErrorInfo);
487
- } catch (e) {
488
- logger.error(`SetGroupOwner via SOAP ${ownerId} ${ownerId} error:`);
489
- logger.error(e);
490
- return false;
491
- }
492
- }
493
- }
494
-
495
- export async function GroupIncludesAllUsers(siteUrl: string, groupId: number) {
496
- try {
497
- if (isNullOrNaN(groupId)) return false;
498
- const groupInfo = await GetGroup(siteUrl, groupId, { expandUsers: true });
499
- if (isNullOrUndefined(groupInfo)) return false;
500
- //special memebr called spo-grid-all-users/{tenant-id} will be added, its not in the AAD or anywhere else.
501
- const includesAllUsers = !isNullOrUndefined(firstOrNull(groupInfo.Users, u => (u.LoginName || "").indexOf("|spo-grid-all-users/") >= 0));
502
- return includesAllUsers;
503
- } catch (e) {
504
- logger.error(e);
505
- return false;
506
- }
507
- }
508
-
509
- /** return array of AAD group IDs, guid, normalized */
510
- export async function GetCurrentUserADGroupMemberships(siteUrl: string) {
511
- let url = `${GetRestBaseUrl(siteUrl)}/SP.Publishing.SitePageService.GetCurrentUserMemberships`;
512
- try {
513
- let result = await GetJson<{ value: string[] }>(url, null, { jsonMetadata: jsonTypes.nometadata });
514
- return isNotEmptyArray(result.value) ? result.value.map(id => normalizeGuid(id)) : [];
515
- } catch (e) {
516
- logger.error(e);
517
- return [];
518
- }
519
- }
520
-
521
- /** checks users groups, then checks for groups that contains all users and that the user is not an external one */
522
- export async function IsUserMemberOfGroup(siteUrl: string, user: { LoginName: string; Id: number; Groups?: IUserGroupInfo[] }, group: { Id: number, LoginName: string }) {
523
- if (isNotEmptyArray(user.Groups)) {
524
- //search user groups for the group by title or id
525
- const found = firstOrNull(user.Groups, userGroup => (isNotEmptyString(group.LoginName) && userGroup.Title === group.LoginName) || (isNumber(group.Id) && userGroup.Id === group.Id));
526
- if (found)
527
- return true;
528
- }
529
-
530
- const groupInfo = await GetGroup(siteUrl, group.Id, { expandUsers: true });
531
- if (!isNullOrUndefined(groupInfo)) {
532
- if (isNotEmptyArray(groupInfo.Users)) {
533
- //search group users memberships directly
534
- const found = firstOrNull(groupInfo.Users, groupUser => (isNotEmptyString(user.LoginName) && groupUser.LoginName === user.LoginName) || (isNumber(user.Id) && groupUser.Id === user.Id));
535
- if (found)
536
- return true;
537
-
538
- //if we looking for current user - we can check GetCurrentUserADGroupMemberships
539
- let currentUser = await GetCurrentUser(siteUrl);
540
- if (currentUser.LoginName === user.LoginName) {
541
- //get user's aad groups
542
- const UserAADGroups = await GetCurrentUserADGroupMemberships(siteUrl);
543
- if (isNotEmptyArray(UserAADGroups)) {
544
- //convert group's users to guids
545
- const groupUserLoginsSplit = filterEmptyEntries(groupInfo.Users.map(u => lastOrNull(u.LoginName.split('|'))));
546
- //see if any of the group members is a guid that is in the user's aad groups
547
- const found = firstOrNull(groupUserLoginsSplit, u => UserAADGroups.includes(normalizeGuid(u)));
548
- if (found)
549
- return true;
550
- }
551
- }
552
- }
553
- //groups that contain all-users special permission will not show up in the user's groups or anywhere else - so test manually.
554
- const includesAllUsers = await GroupIncludesAllUsers(siteUrl, group.Id);
555
- const isCurrentUserExternal = isExternalUser(user.LoginName);
556
- return includesAllUsers && !isCurrentUserExternal;
557
- }
558
- return false;
559
- }