From a07cfbbf4facdb833da981af41ff0c9dad4252fd Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 18 Sep 2020 14:50:34 +0800 Subject: [PATCH 1/4] enable "unauthenticated users" role in the list. Fix #4094 --- .../Services/ComponentsController.cs | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs index fdca009d9a4..779217ea051 100644 --- a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs @@ -1,7 +1,9 @@ -// 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 + +using DotNetNuke.Common; + namespace Dnn.PersonaBar.UI.Services { using System; @@ -104,17 +106,27 @@ public HttpResponseMessage GetSuggestionRoles(string keyword, int roleGroupId, i return this.Request.CreateResponse(HttpStatusCode.OK, new List()); } - var matchedRoles = RoleController.Instance.GetRoles(this.PortalId) - .Where(r => (roleGroupId == -2 || r.RoleGroupID == roleGroupId) - && r.RoleName.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) > -1 - && r.Status == RoleStatus.Approved) - .Select(r => new SuggestionDto() + var portalRoles = RoleController.Instance.GetRoles(this.PortalId); + + + var matchedRoles = portalRoles.Where(r => (roleGroupId == -2 || r.RoleGroupID == roleGroupId) + && r.RoleName.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) > Null.NullInteger + && r.SecurityMode != SecurityMode.SocialGroup + && r.Status == RoleStatus.Approved).ToList(); + + if (roleGroupId <= Null.NullInteger + && Globals.glbRoleUnauthUserName.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) > Null.NullInteger) + { + matchedRoles.Add(new RoleInfo { RoleID = int.Parse(Globals.glbRoleUnauthUser), RoleName = Globals.glbRoleUnauthUserName }); + } + + var data = matchedRoles.OrderBy(r => r.RoleName).Select(r => new SuggestionDto() { Value = r.RoleID, - Label = r.RoleName, + Label = r.RoleName }); - return this.Request.CreateResponse(HttpStatusCode.OK, matchedRoles); + return this.Request.CreateResponse(HttpStatusCode.OK, data); } catch (Exception ex) { From e419f7ee0d088aefd060caf604a7bb02cfa0df1b Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 18 Sep 2020 15:01:19 +0800 Subject: [PATCH 2/4] save the page permissions for unauthticated users. --- .../Components/Pages/PagesControllerImpl.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Dnn.AdminExperience/Dnn.PersonaBar.Extensions/Components/Pages/PagesControllerImpl.cs b/Dnn.AdminExperience/Dnn.PersonaBar.Extensions/Components/Pages/PagesControllerImpl.cs index 20f399fe8ef..b1bcb76bfc8 100644 --- a/Dnn.AdminExperience/Dnn.PersonaBar.Extensions/Components/Pages/PagesControllerImpl.cs +++ b/Dnn.AdminExperience/Dnn.PersonaBar.Extensions/Components/Pages/PagesControllerImpl.cs @@ -812,8 +812,9 @@ public void SavePagePermissions(TabInfo tab, PagePermissions permissions) { foreach (var rolePermission in permissions.RolePermissions.Where(NoLocked())) { - if (rolePermission.RoleId.ToString() == Globals.glbRoleAllUsers || - RoleController.Instance.GetRoleById(portalSettings.PortalId, rolePermission.RoleId) != null) + if (rolePermission.RoleId.ToString() == Globals.glbRoleAllUsers + || rolePermission.RoleId.ToString() == Globals.glbRoleUnauthUser + || RoleController.Instance.GetRoleById(portalSettings.PortalId, rolePermission.RoleId) != null) { foreach (var permission in rolePermission.Permissions) { From 563e24c97b3d45cb368b7d6a23910f0b5a4b5f11 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 25 Sep 2020 09:40:12 +0800 Subject: [PATCH 3/4] update code by review. --- .../Services/ComponentsController.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs index 779217ea051..151cd529dfa 100644 --- a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information +using System.Globalization; using DotNetNuke.Common; namespace Dnn.PersonaBar.UI.Services @@ -35,6 +36,8 @@ public class ComponentsController : PersonaBarApiController public string LocalResourcesFile => Path.Combine("~/DesktopModules/admin/Dnn.PersonaBar/App_LocalResources/SharedResources.resx"); + private int UnauthUserRoleId => int.Parse(Globals.glbRoleUnauthUser, CultureInfo.InvariantCulture); + [HttpGet] public HttpResponseMessage GetRoleGroups(bool reload = false) { @@ -106,18 +109,15 @@ public HttpResponseMessage GetSuggestionRoles(string keyword, int roleGroupId, i return this.Request.CreateResponse(HttpStatusCode.OK, new List()); } - var portalRoles = RoleController.Instance.GetRoles(this.PortalId); - - - var matchedRoles = portalRoles.Where(r => (roleGroupId == -2 || r.RoleGroupID == roleGroupId) - && r.RoleName.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) > Null.NullInteger - && r.SecurityMode != SecurityMode.SocialGroup + var matchedRoles = RoleController.Instance.GetRoles(this.PortalId) + .Where(r => (roleGroupId == -2 || r.RoleGroupID == roleGroupId) + && r.RoleName.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) > -1 && r.Status == RoleStatus.Approved).ToList(); if (roleGroupId <= Null.NullInteger && Globals.glbRoleUnauthUserName.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) > Null.NullInteger) { - matchedRoles.Add(new RoleInfo { RoleID = int.Parse(Globals.glbRoleUnauthUser), RoleName = Globals.glbRoleUnauthUserName }); + matchedRoles.Add(new RoleInfo { RoleID = this.UnauthUserRoleId, RoleName = Globals.glbRoleUnauthUserName }); } var data = matchedRoles.OrderBy(r => r.RoleName).Select(r => new SuggestionDto() From 8c867e082129fda1e8f1d0d89e3f7bea956764e7 Mon Sep 17 00:00:00 2001 From: Mitchel Sellers Date: Tue, 29 Sep 2020 19:07:35 -0500 Subject: [PATCH 4/4] Update Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs explciit -1 Co-authored-by: Brian Dukes --- .../Services/ComponentsController.cs | 274 +++++++++--------- 1 file changed, 137 insertions(+), 137 deletions(-) diff --git a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs index 151cd529dfa..e480eca29a5 100644 --- a/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs +++ b/Dnn.AdminExperience/Library/Dnn.PersonaBar.UI/Services/ComponentsController.cs @@ -1,138 +1,138 @@ -// 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 - -using System.Globalization; -using DotNetNuke.Common; - -namespace Dnn.PersonaBar.UI.Services -{ - using System; - using System.Collections.Generic; - using System.IO; - using System.Linq; - using System.Net; - using System.Net.Http; - using System.Web.Http; - - using Dnn.PersonaBar.Library; - using Dnn.PersonaBar.Library.Attributes; - using Dnn.PersonaBar.UI.Services.DTO; - using DotNetNuke.Common.Utilities; - using DotNetNuke.Entities.Users; - using DotNetNuke.Instrumentation; - using DotNetNuke.Security.Roles; - using DotNetNuke.Services.Localization; - using DotNetNuke.Web.Api; - using DotNetNuke.Web.Api.Internal; - - /// - /// Services used for common components. - /// - [MenuPermission(Scope = ServiceScope.Regular)] - public class ComponentsController : PersonaBarApiController - { - private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(ComponentsController)); - - public string LocalResourcesFile => Path.Combine("~/DesktopModules/admin/Dnn.PersonaBar/App_LocalResources/SharedResources.resx"); - - private int UnauthUserRoleId => int.Parse(Globals.glbRoleUnauthUser, CultureInfo.InvariantCulture); - - [HttpGet] - public HttpResponseMessage GetRoleGroups(bool reload = false) - { - try - { - if (!this.UserInfo.IsInRole(this.PortalSettings.AdministratorRoleName) && !PagePermissionsAttributesHelper.HasTabPermission("VIEW")) - { - return this.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, Localization.GetString("UnauthorizedRequest", this.LocalResourcesFile)); - } - - if (reload) - { - DataCache.RemoveCache(string.Format(DataCache.RoleGroupsCacheKey, this.PortalId)); - } - - var groups = RoleController.GetRoleGroups(this.PortalId) - .Cast() - .Select(RoleGroupDto.FromRoleGroupInfo); - - return this.Request.CreateResponse(HttpStatusCode.OK, groups); - } - catch (Exception ex) - { - Logger.Error(ex); - return this.Request.CreateResponse(HttpStatusCode.InternalServerError, new { Error = ex.Message }); - } - } - - [HttpGet] - public HttpResponseMessage GetSuggestionUsers(string keyword, int count) - { - try - { - if (string.IsNullOrEmpty(keyword)) - { - return this.Request.CreateResponse(HttpStatusCode.OK, new List()); - } - - var displayMatch = keyword + "%"; - var totalRecords = 0; - var totalRecords2 = 0; - var matchedUsers = UserController.GetUsersByDisplayName(this.PortalId, displayMatch, 0, count, - ref totalRecords, false, false); - matchedUsers.AddRange(UserController.GetUsersByUserName(this.PortalId, displayMatch, 0, count, ref totalRecords2, false, false)); - var finalUsers = matchedUsers - .Cast() - .Where(x => x.Membership.Approved) - .Select(u => new SuggestionDto() - { - Value = u.UserID, - Label = $"{u.DisplayName}", - }); - - return this.Request.CreateResponse(HttpStatusCode.OK, finalUsers.ToList().GroupBy(x => x.Value).Select(group => group.First())); - } - catch (Exception ex) - { - Logger.Error(ex); - return this.Request.CreateResponse(HttpStatusCode.InternalServerError, new { Error = ex.Message }); - } - } - - public HttpResponseMessage GetSuggestionRoles(string keyword, int roleGroupId, int count) - { - try - { - if (string.IsNullOrEmpty(keyword)) - { - return this.Request.CreateResponse(HttpStatusCode.OK, new List()); - } - +// 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 + +using System.Globalization; +using DotNetNuke.Common; + +namespace Dnn.PersonaBar.UI.Services +{ + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Net; + using System.Net.Http; + using System.Web.Http; + + using Dnn.PersonaBar.Library; + using Dnn.PersonaBar.Library.Attributes; + using Dnn.PersonaBar.UI.Services.DTO; + using DotNetNuke.Common.Utilities; + using DotNetNuke.Entities.Users; + using DotNetNuke.Instrumentation; + using DotNetNuke.Security.Roles; + using DotNetNuke.Services.Localization; + using DotNetNuke.Web.Api; + using DotNetNuke.Web.Api.Internal; + + /// + /// Services used for common components. + /// + [MenuPermission(Scope = ServiceScope.Regular)] + public class ComponentsController : PersonaBarApiController + { + private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(ComponentsController)); + + public string LocalResourcesFile => Path.Combine("~/DesktopModules/admin/Dnn.PersonaBar/App_LocalResources/SharedResources.resx"); + + private int UnauthUserRoleId => int.Parse(Globals.glbRoleUnauthUser, CultureInfo.InvariantCulture); + + [HttpGet] + public HttpResponseMessage GetRoleGroups(bool reload = false) + { + try + { + if (!this.UserInfo.IsInRole(this.PortalSettings.AdministratorRoleName) && !PagePermissionsAttributesHelper.HasTabPermission("VIEW")) + { + return this.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, Localization.GetString("UnauthorizedRequest", this.LocalResourcesFile)); + } + + if (reload) + { + DataCache.RemoveCache(string.Format(DataCache.RoleGroupsCacheKey, this.PortalId)); + } + + var groups = RoleController.GetRoleGroups(this.PortalId) + .Cast() + .Select(RoleGroupDto.FromRoleGroupInfo); + + return this.Request.CreateResponse(HttpStatusCode.OK, groups); + } + catch (Exception ex) + { + Logger.Error(ex); + return this.Request.CreateResponse(HttpStatusCode.InternalServerError, new { Error = ex.Message }); + } + } + + [HttpGet] + public HttpResponseMessage GetSuggestionUsers(string keyword, int count) + { + try + { + if (string.IsNullOrEmpty(keyword)) + { + return this.Request.CreateResponse(HttpStatusCode.OK, new List()); + } + + var displayMatch = keyword + "%"; + var totalRecords = 0; + var totalRecords2 = 0; + var matchedUsers = UserController.GetUsersByDisplayName(this.PortalId, displayMatch, 0, count, + ref totalRecords, false, false); + matchedUsers.AddRange(UserController.GetUsersByUserName(this.PortalId, displayMatch, 0, count, ref totalRecords2, false, false)); + var finalUsers = matchedUsers + .Cast() + .Where(x => x.Membership.Approved) + .Select(u => new SuggestionDto() + { + Value = u.UserID, + Label = $"{u.DisplayName}", + }); + + return this.Request.CreateResponse(HttpStatusCode.OK, finalUsers.ToList().GroupBy(x => x.Value).Select(group => group.First())); + } + catch (Exception ex) + { + Logger.Error(ex); + return this.Request.CreateResponse(HttpStatusCode.InternalServerError, new { Error = ex.Message }); + } + } + + public HttpResponseMessage GetSuggestionRoles(string keyword, int roleGroupId, int count) + { + try + { + if (string.IsNullOrEmpty(keyword)) + { + return this.Request.CreateResponse(HttpStatusCode.OK, new List()); + } + var matchedRoles = RoleController.Instance.GetRoles(this.PortalId) - .Where(r => (roleGroupId == -2 || r.RoleGroupID == roleGroupId) - && r.RoleName.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) > -1 - && r.Status == RoleStatus.Approved).ToList(); - - if (roleGroupId <= Null.NullInteger - && Globals.glbRoleUnauthUserName.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) > Null.NullInteger) - { - matchedRoles.Add(new RoleInfo { RoleID = this.UnauthUserRoleId, RoleName = Globals.glbRoleUnauthUserName }); - } - - var data = matchedRoles.OrderBy(r => r.RoleName).Select(r => new SuggestionDto() - { - Value = r.RoleID, - Label = r.RoleName - }); - - return this.Request.CreateResponse(HttpStatusCode.OK, data); - } - catch (Exception ex) - { - Logger.Error(ex); - return this.Request.CreateResponse(HttpStatusCode.InternalServerError, new { Error = ex.Message }); - } - } - } -} + .Where(r => (roleGroupId == -2 || r.RoleGroupID == roleGroupId) + && r.RoleName.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) > -1 + && r.Status == RoleStatus.Approved).ToList(); + + if (roleGroupId <= Null.NullInteger + && Globals.glbRoleUnauthUserName.IndexOf(keyword, StringComparison.InvariantCultureIgnoreCase) > -1) + { + matchedRoles.Add(new RoleInfo { RoleID = this.UnauthUserRoleId, RoleName = Globals.glbRoleUnauthUserName }); + } + + var data = matchedRoles.OrderBy(r => r.RoleName).Select(r => new SuggestionDto() + { + Value = r.RoleID, + Label = r.RoleName + }); + + return this.Request.CreateResponse(HttpStatusCode.OK, data); + } + catch (Exception ex) + { + Logger.Error(ex); + return this.Request.CreateResponse(HttpStatusCode.InternalServerError, new { Error = ex.Message }); + } + } + } +}