Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Umbraco.Core/Services/IUserIdKeyResolver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace Umbraco.Cms.Core.Services;
namespace Umbraco.Cms.Core.Services;

/// <summary>
/// Defines methods for resolving user keys to user IDs and vice versa without retrieving full user details.
/// </summary>
public interface IUserIdKeyResolver
{
/// <summary>
Expand Down
30 changes: 27 additions & 3 deletions src/Umbraco.Infrastructure/Services/Implement/UserIdKeyResolver.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using NPoco;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Services;
Expand All @@ -9,8 +9,13 @@

namespace Umbraco.Cms.Infrastructure.Services.Implement;

// It's okay that we never clear this, since you can never change a user's key/id
// and it'll be caught by the services if it doesn't exist.
/// <summary>
/// Implements <see cref="IUserIdKeyResolver"/> for resolving user keys to user IDs and vice versa without retrieving full user details.
/// </summary>
/// <remarks>
/// It's okay that we never clear this, since you can never change a user's key/id
/// and it'll be caught by the services if it doesn't exist.
/// </remarks>
internal sealed class UserIdKeyResolver : IUserIdKeyResolver
{
private readonly IScopeProvider _scopeProvider;
Expand All @@ -19,6 +24,9 @@ internal sealed class UserIdKeyResolver : IUserIdKeyResolver
private readonly SemaphoreSlim _keytToIdLock = new(1, 1);
private readonly SemaphoreSlim _idToKeyLock = new(1, 1);

/// <summary>
/// Initializes a new instance of the <see cref="UserIdKeyResolver"/> class.
/// </summary>
public UserIdKeyResolver(IScopeProvider scopeProvider) => _scopeProvider = scopeProvider;

/// <inheritdoc/>
Expand All @@ -28,6 +36,14 @@ public async Task<int> GetAsync(Guid key)
/// <inheritdoc/>
public async Task<Attempt<int>> TryGetAsync(Guid key)
{
// The super-user Id and key is known, so we don't need a look-up here.
if (key == Constants.Security.SuperUserKey)
{
#pragma warning disable CS0618 // Type or member is obsolete
return Attempt.Succeed(Constants.Security.SuperUserId);
#pragma warning restore CS0618 // Type or member is obsolete
}

if (_keyToId.TryGetValue(key, out int id))
{
return Attempt.Succeed(id);
Expand Down Expand Up @@ -76,6 +92,14 @@ public async Task<Guid> GetAsync(int id)
/// <inheritdoc/>
public async Task<Attempt<Guid>> TryGetAsync(int id)
{
// The super-user Id and key is known, so we don't need a look-up here.
#pragma warning disable CS0618 // Type or member is obsolete
if (id is Constants.Security.SuperUserId)
#pragma warning restore CS0618 // Type or member is obsolete
{
return Attempt.Succeed(Constants.Security.SuperUserKey);
}

if (_idToKey.TryGetValue(id, out Guid key))
{
return Attempt.Succeed(key);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using NUnit.Framework;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Models.Membership;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Tests.Common.Testing;
using Umbraco.Cms.Tests.Integration.Testing;
Expand Down
Loading