Skip to content

Commit

Permalink
Added optimization to delegated access lookups.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaellwest committed Oct 3, 2024
1 parent e186548 commit 0c8fb7e
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/Spe/Core/Settings/DelegatedAccessManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Security.Accounts;
using Sitecore.SecurityModel;
using Spe.Core.Diagnostics;
using System;
using System.Collections.Concurrent;
Expand All @@ -20,28 +21,35 @@ public static class DelegatedAccessManager

public const string DelegatedItemPath = "/sitecore/system/Modules/PowerShell/Delegated Access";

private static bool _isInitialized = false;

public static void Invalidate()
{
PowerShellLog.Audit($"Clearing {nameof(DelegatedAccessEntry)} entries.");
_accessEntries.Clear();
_delegatedItems.Clear();
_isInitialized = false;
}

private static IEnumerable<Item> GetDelegatedItems()
{
if (_delegatedItems.Any())
if (_isInitialized)
{
return _delegatedItems;
}

var db = Factory.GetDatabase(ApplicationSettings.ScriptLibraryDb);
var delegatedItems = db.GetItem(DelegatedItemPath)
.Axes.GetDescendants()
.Where(d => d.TemplateID == Templates.DelegatedAccess.Id);
using (new SecurityDisabler())
{
var db = Factory.GetDatabase(ApplicationSettings.ScriptLibraryDb);
var delegatedItems = db.GetItem(DelegatedItemPath)
.Axes.GetDescendants()
.Where(d => d.TemplateID == Templates.DelegatedAccess.Id);

_delegatedItems.AddRange(delegatedItems);
_delegatedItems.AddRange(delegatedItems);
}

return delegatedItems;
_isInitialized = true;
return _delegatedItems;
}

private static DelegatedAccessEntry GetDelegatedAccessEntry(User currentUser, Item scriptItem)
Expand Down

0 comments on commit 0c8fb7e

Please sign in to comment.