From 143e0af7cfcd503672c8b571512ee619e7fe10d0 Mon Sep 17 00:00:00 2001 From: Jevon Date: Sat, 14 May 2022 09:17:11 +0100 Subject: [PATCH] Restore `IsGranted` extension method deprecated in Castle.Core Castler.Core deprecated CAS handling (https://github.com/castleproject/Core/pull/502/), this commit restores the relevant method and associated test. #612 - Updating Windsor to support Castle.Core@5.0.0 and modern TFMs --- .../Compatibility/PermissionUtilTests.cs | 38 +++++++++++++++++++ .../Compatibility/PermissionUtil.cs | 35 +++++++++++++++++ .../DefaultComponentActivator.cs | 5 ++- .../MicroKernel/DefaultKernel.cs | 6 ++- 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 src/Castle.Windsor.Tests/Compatibility/PermissionUtilTests.cs create mode 100644 src/Castle.Windsor/Compatibility/PermissionUtil.cs diff --git a/src/Castle.Windsor.Tests/Compatibility/PermissionUtilTests.cs b/src/Castle.Windsor.Tests/Compatibility/PermissionUtilTests.cs new file mode 100644 index 0000000000..3f4471acd4 --- /dev/null +++ b/src/Castle.Windsor.Tests/Compatibility/PermissionUtilTests.cs @@ -0,0 +1,38 @@ +// Copyright 2004-2022 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#if FEATURE_SECURITY_PERMISSIONS + +namespace Castle.Windsor.Tests.Compatibility +{ + using System.Security.Permissions; + + using Castle.Windsor.Compatibility; + using NUnit.Framework; + + [TestFixture] + public class PermissionUtilTests + { + [Test] + public void Correctly_determines_permissions() + { + // Execution has to be always granted. Otherwise this code wouldn't run in the first place. + var securityPermission = new SecurityPermission(SecurityPermissionFlag.Execution); + + Assert.IsTrue(securityPermission.IsGranted()); + } + } +} + +#endif \ No newline at end of file diff --git a/src/Castle.Windsor/Compatibility/PermissionUtil.cs b/src/Castle.Windsor/Compatibility/PermissionUtil.cs new file mode 100644 index 0000000000..f6d52bb609 --- /dev/null +++ b/src/Castle.Windsor/Compatibility/PermissionUtil.cs @@ -0,0 +1,35 @@ +// Copyright 2004-2022 Castle Project - http://www.castleproject.org/ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#if FEATURE_SECURITY_PERMISSIONS + +namespace Castle.Windsor.Compatibility +{ + using System; + using System.Security; + using System.Security.Permissions; + + public static class PermissionUtil + { + public static bool IsGranted(this IPermission permission) + { + var permissionSet = new PermissionSet(PermissionState.None); + permissionSet.AddPermission(permission); + + return permissionSet.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet); + } + } +} + +#endif \ No newline at end of file diff --git a/src/Castle.Windsor/MicroKernel/ComponentActivator/DefaultComponentActivator.cs b/src/Castle.Windsor/MicroKernel/ComponentActivator/DefaultComponentActivator.cs index f183cfa50c..d2e5c0d7f7 100644 --- a/src/Castle.Windsor/MicroKernel/ComponentActivator/DefaultComponentActivator.cs +++ b/src/Castle.Windsor/MicroKernel/ComponentActivator/DefaultComponentActivator.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2012 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2022 Castle Project - http://www.castleproject.org/ // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -26,6 +26,9 @@ namespace Castle.MicroKernel.ComponentActivator using Castle.Core.Internal; using Castle.DynamicProxy; using Castle.MicroKernel.Context; +#if FEATURE_SECURITY_PERMISSIONS + using Castle.Windsor.Compatibility; +#endif /// /// Standard implementation of . Handles the selection of the best constructor, fills the writable properties the component exposes, run the commission and diff --git a/src/Castle.Windsor/MicroKernel/DefaultKernel.cs b/src/Castle.Windsor/MicroKernel/DefaultKernel.cs index ed1e75629b..b112895b06 100644 --- a/src/Castle.Windsor/MicroKernel/DefaultKernel.cs +++ b/src/Castle.Windsor/MicroKernel/DefaultKernel.cs @@ -1,4 +1,4 @@ -// Copyright 2004-2012 Castle Project - http://www.castleproject.org/ +// Copyright 2004-2022 Castle Project - http://www.castleproject.org/ // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -15,7 +15,6 @@ namespace Castle.MicroKernel { using System; - using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -44,6 +43,9 @@ namespace Castle.MicroKernel using Castle.MicroKernel.SubSystems.Conversion; using Castle.MicroKernel.SubSystems.Naming; using Castle.MicroKernel.SubSystems.Resource; +#if FEATURE_SECURITY_PERMISSIONS + using Castle.Windsor.Compatibility; +#endif using Castle.Windsor.Diagnostics; ///