From 234eb74920d7a5c972c693480234ce52e9e56a7c Mon Sep 17 00:00:00 2001 From: Brian Dukes Date: Tue, 7 Nov 2023 15:27:43 -0600 Subject: [PATCH] Fix exception saving folder permissions Fixes #5864 --- .../Permissions/FolderPermissionInfo.cs | 30 +++++------ .../DotNetNuke.Tests.Core.csproj | 1 + .../Permissions/PermissionProviderTests.cs | 54 +++++++++++++++++++ 3 files changed, 70 insertions(+), 15 deletions(-) create mode 100644 DNN Platform/Tests/DotNetNuke.Tests.Core/Security/Permissions/PermissionProviderTests.cs diff --git a/DNN Platform/Library/Security/Permissions/FolderPermissionInfo.cs b/DNN Platform/Library/Security/Permissions/FolderPermissionInfo.cs index 8c21335f988..e8a6504b32b 100644 --- a/DNN Platform/Library/Security/Permissions/FolderPermissionInfo.cs +++ b/DNN Platform/Library/Security/Permissions/FolderPermissionInfo.cs @@ -1,15 +1,15 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information namespace DotNetNuke.Security.Permissions { using System; using System.Data; - using System.Xml.Serialization; + using System.Xml.Serialization; using DotNetNuke.Abstractions.Security.Permissions; using DotNetNuke.Common.Utilities; - using DotNetNuke.Entities.Modules; + using DotNetNuke.Entities.Modules; using Newtonsoft.Json; [Serializable] @@ -39,7 +39,7 @@ public FolderPermissionInfo() /// /// A PermissionInfo object. public FolderPermissionInfo(PermissionInfo permission) - : this((IPermissionInfo)permission) + : this((IPermissionDefinitionInfo)permission) { } @@ -48,7 +48,7 @@ public FolderPermissionInfo(PermissionInfo permission) /// Constructs a new FolderPermissionInfo. /// /// A PermissionInfo object. - public FolderPermissionInfo(IPermissionInfo permission) + public FolderPermissionInfo(IPermissionDefinitionInfo permission) : this() { var @this = (IPermissionDefinitionInfo)this; @@ -59,7 +59,7 @@ public FolderPermissionInfo(IPermissionInfo permission) @this.PermissionName = permission.PermissionName; } - [XmlIgnore] + [XmlIgnore] [JsonIgnore] [Obsolete($"Deprecated in DotNetNuke 9.13.1. Use {nameof(IFolderPermissionInfo)}.{nameof(IFolderPermissionInfo.FolderPermissionId)} instead. Scheduled for removal in v11.0.0.")] public int FolderPermissionID @@ -75,7 +75,7 @@ public int FolderPermissionID } } - [XmlIgnore] + [XmlIgnore] [JsonIgnore] int IFolderPermissionInfo.FolderPermissionId { @@ -90,7 +90,7 @@ int IFolderPermissionInfo.FolderPermissionId } } - [XmlIgnore] + [XmlIgnore] [JsonIgnore] [Obsolete($"Deprecated in DotNetNuke 9.13.1. Use {nameof(IFolderPermissionInfo)}.{nameof(IFolderPermissionInfo.FolderId)} instead. Scheduled for removal in v11.0.0.")] public int FolderID @@ -106,7 +106,7 @@ public int FolderID } } - [XmlIgnore] + [XmlIgnore] [JsonIgnore] [Obsolete($"Deprecated in DotNetNuke 9.13.1. Use {nameof(IFolderPermissionInfo)}.{nameof(IFolderPermissionInfo.FolderId)} instead. Scheduled for removal in v11.0.0.")] int IFolderPermissionInfo.FolderId @@ -122,7 +122,7 @@ int IFolderPermissionInfo.FolderId } } - [XmlIgnore] + [XmlIgnore] [JsonIgnore] [Obsolete($"Deprecated in DotNetNuke 9.13.1. Use {nameof(IFolderPermissionInfo)}.{nameof(IFolderPermissionInfo.PortalId)} instead. Scheduled for removal in v11.0.0.")] public int PortalID @@ -138,7 +138,7 @@ public int PortalID } } - [XmlIgnore] + [XmlIgnore] [JsonIgnore] [Obsolete($"Deprecated in DotNetNuke 9.13.1. Use {nameof(IFolderPermissionInfo)}.{nameof(IFolderPermissionInfo.PortalId)} instead. Scheduled for removal in v11.0.0.")] int IFolderPermissionInfo.PortalId @@ -170,7 +170,7 @@ public string FolderPath /// Gets or sets the Key ID. /// An Integer. - [XmlIgnore] + [XmlIgnore] [JsonIgnore] public int KeyID { @@ -190,7 +190,7 @@ public int KeyID public void Fill(IDataReader dr) { this.FillInternal(dr); - + var @this = (IFolderPermissionInfo)this; @this.FolderPermissionId = Null.SetNullInteger(dr["FolderPermissionID"]); @this.FolderId = Null.SetNullInteger(dr["FolderID"]); diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Core/DotNetNuke.Tests.Core.csproj b/DNN Platform/Tests/DotNetNuke.Tests.Core/DotNetNuke.Tests.Core.csproj index 4bef29c9b2b..c7b005ec7f3 100644 --- a/DNN Platform/Tests/DotNetNuke.Tests.Core/DotNetNuke.Tests.Core.csproj +++ b/DNN Platform/Tests/DotNetNuke.Tests.Core/DotNetNuke.Tests.Core.csproj @@ -208,6 +208,7 @@ + diff --git a/DNN Platform/Tests/DotNetNuke.Tests.Core/Security/Permissions/PermissionProviderTests.cs b/DNN Platform/Tests/DotNetNuke.Tests.Core/Security/Permissions/PermissionProviderTests.cs new file mode 100644 index 00000000000..7319410cc93 --- /dev/null +++ b/DNN Platform/Tests/DotNetNuke.Tests.Core/Security/Permissions/PermissionProviderTests.cs @@ -0,0 +1,54 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information + +namespace DotNetNuke.Tests.Core.Security.Permissions; + +using System.Collections.Generic; +using DotNetNuke.Common.Utilities; +using DotNetNuke.ComponentModel; +using DotNetNuke.Data; +using DotNetNuke.Security.Permissions; +using DotNetNuke.Services.Cache; +using DotNetNuke.Services.FileSystem; +using DotNetNuke.Tests.Utilities.Fakes; +using Moq; +using NUnit.Framework; + +[TestFixture] +public class PermissionProviderTests +{ + [Test] + public void SaveFolderPermissions_DoesNotThrow() + { + var permissions = new List(); + var cache = new Dictionary { { CachingProvider.GetCacheKey(DataCache.PermissionsCacheKey), permissions }, }; + var fakeCachingProvider = new FakeCachingProvider(cache); + ComponentFactory.RegisterComponentInstance(fakeCachingProvider); + + ComponentFactory.RegisterComponentInstance(Mock.Of()); + + var permissionProvider = new PermissionProvider(); + + IFolderInfo folder = new FolderInfo(initialiseEmptyPermissions: true); + Assert.DoesNotThrow(() => permissionProvider.SaveFolderPermissions(folder)); + } + + [Test] + public void SaveFolderPermissions_WithPermissions_DoesNotThrow() + { + var readPermission = new PermissionInfo { PermissionCode = "SYSTEM_FOLDER", PermissionKey = "READ", PermissionID = 1, }; + var permissions = new List { readPermission, }; + var cache = new Dictionary { { CachingProvider.GetCacheKey(DataCache.PermissionsCacheKey), permissions }, }; + var fakeCachingProvider = new FakeCachingProvider(cache); + ComponentFactory.RegisterComponentInstance(fakeCachingProvider); + + ComponentFactory.RegisterComponentInstance(Mock.Of()); + + var permissionProvider = new PermissionProvider(); + + IFolderInfo folder = new FolderInfo(initialiseEmptyPermissions: true); + folder.FolderPermissions.Add(new FolderPermissionInfo(readPermission) { AllowAccess = true, RoleID = -2, UserID = -1, FolderID = 2, }); + Assert.DoesNotThrow(() => permissionProvider.SaveFolderPermissions(folder)); + } +}