Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable resetting application context user to null #4415

Merged
merged 10 commits into from
Dec 30, 2024
12 changes: 11 additions & 1 deletion Source/Csla.TestHelpers/ApplicationContextManagerUnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
namespace Csla.TestHelpers
using System.Security.Principal;

namespace Csla.TestHelpers
{
public class ApplicationContextManagerUnitTests : Core.ApplicationContextManagerAsyncLocal
{
public IPrincipal LastSetUserPrincipal { get; private set; }

public Guid InstanceId { get; private set; } = Guid.NewGuid();

public DateTime CreatedAt { get; private set; } = DateTime.Now;

public override void SetUser(IPrincipal principal)
{
LastSetUserPrincipal = principal;
base.SetUser(principal);
}
}
}
3 changes: 1 addition & 2 deletions Source/Csla.TestHelpers/TestDIContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,9 @@ public static TestDIContext CreateContext(Action<CslaOptions> customCslaOptions,
var services = new ServiceCollection();

// Add Csla
//services.AddSingleton<Core.IContextManager, ApplicationContextManagerUnitTests>();
services.TryAddSingleton<Server.Dashboard.IDashboard, Server.Dashboard.Dashboard>();
services.AddCsla(customCslaOptions);
services.AddSingleton<Core.IContextManager, ApplicationContextManagerUnitTests>();
services.AddCsla(customCslaOptions);

serviceProvider = services.BuildServiceProvider();

Expand Down
2 changes: 1 addition & 1 deletion Source/Csla.test/DPException/DataPortalExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void CheckInnerExceptionsOnSave()
#if (NETFRAMEWORK)
Assert.AreEqual(".Net SqlClient Data Provider", exceptionSource);
#else
Assert.AreEqual("Core .Net SqlClient Data Provider", exceptionSource);
Assert.AreEqual("Core Microsoft SqlClient Data Provider", exceptionSource);
#endif

//verify that the implemented method, DataPortal_OnDataPortal
Expand Down
27 changes: 27 additions & 0 deletions Source/Csla.test/DataPortal/DataPortalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
using Csla.Rules;
using Csla.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Csla.Testing.Business.DataPortal;
using Csla.Server;
using System.Security.Principal;
using FluentAssertions.Execution;

namespace Csla.Test.DataPortal
{
Expand Down Expand Up @@ -311,6 +315,29 @@ public async Task WhenCreatingANewObjectThePortalMustWaitAfterCreateUntilTheObje
}
}

[TestMethod]
public async Task CleanupShouldSetThePrincipalToAnUnathenticatedOne()
{
// We have to use an extra DI context here to setup the TestableDataPortal and set necessary options
var diContext = TestDIContextFactory.CreateContext(options =>
{
options.Services.AddTransient<TestableDataPortal>();
options.Security(s => s.FlowSecurityPrincipalFromClient = true);
options.DataPortal(dpo => dpo.AddServerSideDataPortal());
});

var applicationContext = diContext.CreateTestApplicationContext();
var contextManager = (ApplicationContextManagerUnitTests)applicationContext.ContextManager;
var dp = diContext.ServiceProvider.GetRequiredService<TestableDataPortal>();
_ = await dp.Create(typeof(TestBO), null, new DataPortalContext(applicationContext, applicationContext.Principal, true, "en-US", "en-US", new Core.ContextDictionary()), true);

using (new AssertionScope())
{
contextManager.LastSetUserPrincipal.Should().NotBeNull();
contextManager.LastSetUserPrincipal.Should().Match<IPrincipal>(p => !p.Identity.IsAuthenticated);
}
}

private void ClientPortal_DataPortalInvoke(DataPortalEventArgs obj)
{
TestResults.Add("dpinvoke", "true");
Expand Down
6 changes: 3 additions & 3 deletions Source/Csla/ApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public ApplicationContext(ApplicationContextAccessor applicationContextAccessor)
/// </summary>
public ClaimsPrincipal Principal
{
get { return (ClaimsPrincipal)ContextManager.GetUser(); }
set { ContextManager.SetUser(value); }
get { return (ClaimsPrincipal)User; }
set { User = value; }
}

/// <summary>
Expand All @@ -64,7 +64,7 @@ public ClaimsPrincipal Principal
public IPrincipal User
{
get { return ContextManager.GetUser(); }
set { ContextManager.SetUser(value); }
set { ContextManager.SetUser(value ?? new ClaimsPrincipal(new ClaimsIdentity())); }
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions Source/Csla/Server/DataPortal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//-----------------------------------------------------------------------

using System.Diagnostics.CodeAnalysis;
using System.Security.Claims;
using System.Security.Principal;
using Csla.Configuration;
using Csla.Properties;
Expand Down
2 changes: 2 additions & 0 deletions docs/Upgrading to CSLA 9.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ Supporting nullable types means that some APIs have changed to support nullable
* Type `Csla.Serialization.SerializationFormatterFactory` was removed. To get the serializer resolve an instance of type `ISerializationFormatter`. For example `ApplicationContext.GetRequiredService<ISerializationFormatter>()`(from within a business object)
* `Csla.Core.IParent`
* `ApplyEditChild` and `RemoveChild` changed from `void` to `Task` return type
* `void Csla.Core.IContextManager.SetUser(IPrincipal principal)` does not accept `null` anymore
* This change only affects you when you use the `IContextManager` directly. If you use `ApplicationContext.User` CSLA will take care of translating it into a non-null IPrincipal.

#### Using Timeout in `HttpProxy` and `HttpCompressionProxy`

Expand Down
Loading