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 "unauthenticated users" role in the list. #4095

Merged
merged 4 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,125 +2,137 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information

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;

/// <summary>
/// Services used for common components.
/// </summary>
[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");

[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<RoleGroupInfo>()
.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<SuggestionDto>());
}

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<UserInfo>()
.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<SuggestionDto>());
}

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()
{
Value = r.RoleID,
Label = r.RoleName,
});

return this.Request.CreateResponse(HttpStatusCode.OK, matchedRoles);
}
catch (Exception ex)
{
Logger.Error(ex);
return this.Request.CreateResponse(HttpStatusCode.InternalServerError, new { Error = ex.Message });
}
}
}
}
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;

/// <summary>
/// Services used for common components.
/// </summary>
[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<RoleGroupInfo>()
.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<SuggestionDto>());
}

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<UserInfo>()
.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<SuggestionDto>());
}

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) > -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 });
}
}
}
}