From 7897fa0dfb4c6ad641900af7d50c69b6f4a7ebf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 18 Aug 2022 08:46:13 +0300 Subject: [PATCH] Implemented AuthenticatedSimpleStateCheckerSerializerContributor. --- ...SimpleStateCheckerSerializerContributor.cs | 36 ++++++++++++++++ ...StateCheckerSerializerContributor_Tests.cs | 41 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/AuthenticatedSimpleStateCheckerSerializerContributor.cs create mode 100644 framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests.cs diff --git a/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/AuthenticatedSimpleStateCheckerSerializerContributor.cs b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/AuthenticatedSimpleStateCheckerSerializerContributor.cs new file mode 100644 index 0000000000..97b3ae9b0f --- /dev/null +++ b/framework/src/Volo.Abp.Authorization/Volo/Abp/Authorization/Permissions/AuthenticatedSimpleStateCheckerSerializerContributor.cs @@ -0,0 +1,36 @@ +using System.Text.Json.Nodes; +using Volo.Abp.DependencyInjection; +using Volo.Abp.SimpleStateChecking; + +namespace Volo.Abp.Authorization.Permissions; + +public class AuthenticatedSimpleStateCheckerSerializerContributor : + ISimpleStateCheckerSerializerContributor, + ISingletonDependency +{ + public string SerializeToJson(ISimpleStateChecker checker) + where TState : IHasSimpleStateCheckers + { + if (checker is not RequireAuthenticatedSimpleStateChecker requireAuthenticatedSimpleStateChecker) + { + return null; + } + + var jsonObject = new JsonObject { + ["T"] = "A" + }; + + return jsonObject.ToJsonString(); + } + + public ISimpleStateChecker Deserialize(JsonObject jsonObject) + where TState : IHasSimpleStateCheckers + { + if (jsonObject["T"]?.ToString() != "A") + { + return null; + } + + return new RequireAuthenticatedSimpleStateChecker(); + } +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests.cs b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests.cs new file mode 100644 index 0000000000..b6ae8bace4 --- /dev/null +++ b/framework/test/Volo.Abp.Authorization.Tests/Volo/Abp/Authorization/GlobalFeaturesSimpleStateCheckerSerializerContributor_Tests.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; +using System.Text.Json.Nodes; +using Shouldly; +using Volo.Abp.Authorization.Permissions; +using Volo.Abp.SimpleStateChecking; +using Xunit; + +namespace Volo.Abp.Authorization; + +public class AuthenticatedSimpleStateCheckerSerializerContributor_Tests +{ + [Fact] + public void Should_Serialize_RequireGlobalFeaturesSimpleStateChecker() + { + var serializer = new AuthenticatedSimpleStateCheckerSerializerContributor(); + + var result = serializer.SerializeToJson( + new RequireAuthenticatedSimpleStateChecker() + ); + + result.ShouldBe("{\"T\":\"A\"}"); + } + + [Fact] + public void Should_Deserialize_RequireGlobalFeaturesSimpleStateChecker() + { + var serializer = new AuthenticatedSimpleStateCheckerSerializerContributor(); + + var jsonObject = (JsonObject)JsonNode.Parse("{\"T\":\"A\"}"); + var checker = serializer.Deserialize(jsonObject); + + checker.ShouldBeOfType>(); + var globalFeaturesSimpleStateChecker = checker as RequireAuthenticatedSimpleStateChecker; + globalFeaturesSimpleStateChecker.ShouldNotBeNull(); + } + + private class MyState : IHasSimpleStateCheckers + { + public List> StateCheckers { get; } = new(); + } +} \ No newline at end of file