UNPKG

@mikezimm/npmfunctions

Version:
150 lines 10.2 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.savePermissionHistory = void 0; var fetch_1 = require("./fetch"); var normal_1 = require("./normal"); var constants_1 = require("../Time/constants"); /** * This function is for automatically saving permissions from a web, list or library to list for later comparison. * In Easy Contents, it's fired upon viewing rail function to view list permissions. * It's also intended to be used in Pivot Tiles when clicking to view list and web permissions. * * It does require the list and web with the correct struture to save and then be recoverd in this webpart for comparison. * * So it's only going to execute in certain tenanats. * If you see this and want to re-purpose it, update the function to suit your needs and adjust the window.location.origin check * * Best practice is just to update your site and list Url in strings: * Or just create the site: SharePointAssist * And create the list: Assists * And add the columns listed below in the save item "analyticsListPermissionsHistory": "PermissionsHistory", * */ function savePermissionHistory(analyticsWeb, analyticsList, SiteLink, webTitle, saveTitle, TargetSite, TargetList, itemInfo1, itemInfo2, result, RichTextJSON1, Setting, RichTextJSON2, RichTextJSON3, userName, BaseTrace) { return __awaiter(this, void 0, void 0, function () { var prefetchStart, pickedWebguid, theListId, fetchColumns, items, lastIsSame, lastTimeCurrentUserSaved, saveThisSnapshot, checkedOtherUsers, foundMyItem, prefetchEnd, timeToPreFetch, errMessage; return __generator(this, function (_a) { switch (_a.label) { case 0: prefetchStart = new Date(); pickedWebguid = TargetSite.split('|')[1]; theListId = TargetList.split('|')[1]; fetchColumns = ['Created', 'Modified', 'Author/Name', 'Author/Id', 'Author/Title', 'Id', 'Title', 'zzzRichText3', 'zzzText3', 'Result', 'WebID', 'SiteID', 'CollectionUrl', 'ListID' ]; return [4 /*yield*/, (0, fetch_1.fetchAnalytics)(analyticsWeb, analyticsList, pickedWebguid, theListId, true, fetchColumns, 200, BaseTrace)]; case 1: items = _a.sent(); lastIsSame = false; // console.log('RichTextJSON3', RichTextJSON3 ); RichTextJSON3 = JSON.stringify(RichTextJSON3); lastTimeCurrentUserSaved = null; saveThisSnapshot = false; checkedOtherUsers = false; foundMyItem = false; //2022-08-29: Refactored return statement to fix compile warning. //Wrapped items.map in if ( items ) check to make sure items has a value. if (items) { items.map(function (item, index) { if (saveThisSnapshot === false && foundMyItem === false) { var itemFromCurrentUser = false; var itemAny = item; if (itemAny.Author.Name.indexOf(userName) > -1) { // console.log('You saved this item:', item ); lastTimeCurrentUserSaved = new Date(item.Created); itemFromCurrentUser = true; foundMyItem = true; } var userDeltaTime = itemFromCurrentUser === false ? null : prefetchStart.getTime() - lastTimeCurrentUserSaved.getTime(); if (itemFromCurrentUser === true && userDeltaTime > constants_1.msPerDay) { //one day = 24*60*60*1000 saveThisSnapshot = true; //Save this item if the current user has not saved permissions in last 24 hours } else if (checkedOtherUsers === false && lastIsSame === false) { //this check happens if //This section checks if the current item has the same permissions settings as the current check var zzzRichText3 = item.zzzRichText3.replace(/\\\"/g, '"'); zzzRichText3 = zzzRichText3.slice(1, -1); //Have to remove the leading and trailing "" // console.log('zzzRichText3 length = ', zzzRichText3.length, zzzRichText3 ); var thisHasUniqueRoleAssignments = itemInfo2.split('|')[0]; console.log('compareHasUnique ~ perm.ts 83', item.zzzText3, thisHasUniqueRoleAssignments); if (item.zzzText3 === thisHasUniqueRoleAssignments && zzzRichText3.length === RichTextJSON3.length && zzzRichText3 === RichTextJSON3) { lastIsSame = true; } checkedOtherUsers = true; //This is used so it only does this check one time. } } }); //Final check for other conditions to save. if (saveThisSnapshot === true) { //No need for further checks } if (foundMyItem === false) { //automatically save since this user never saved permissions saveThisSnapshot = true; } else if (lastIsSame === false) { //Save if the last item is not the same as current permissions saveThisSnapshot = true; } prefetchEnd = new Date(); timeToPreFetch = prefetchEnd.getTime() - prefetchStart.getTime(); itemInfo2 += '||Time to check old Permissions: ' + items.length + ' snaps' + ' / ' + timeToPreFetch + 'ms'; console.log('savePermissionHistory lastIsSame', lastIsSame); if (saveThisSnapshot === true) { RichTextJSON1 = JSON.stringify(RichTextJSON1); RichTextJSON2 = JSON.stringify(RichTextJSON2); (0, normal_1.saveAnalytics)(analyticsWeb, analyticsList, //analyticsWeb, analyticsList, SiteLink, webTitle, //serverRelativeUrl, webTitle, saveTitle, TargetSite, TargetList, //saveTitle, TargetSite, TargetList itemInfo1, itemInfo2, result, //itemInfo1, itemInfo2, result, RichTextJSON1, Setting, RichTextJSON2, RichTextJSON3); //richText, Setting, richText2, richText3 } } else { errMessage = "Unable to fetch permission analytics...."; console.log("npmFunctions/Analytics/Permissions: ".concat(errMessage)); return [2 /*return*/, errMessage]; } return [2 /*return*/]; } }); }); } exports.savePermissionHistory = savePermissionHistory; //# sourceMappingURL=permissions.js.map