Skip to content

Commit

Permalink
require login for wildcard ip search
Browse files Browse the repository at this point in the history
  • Loading branch information
RaidMax committed Sep 14, 2023
1 parent ec6424b commit 7ecbf85
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
15 changes: 11 additions & 4 deletions Application/QueryHelpers/ClientResourceQueryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
using Data.Models;
using Microsoft.EntityFrameworkCore;
using SharedLibraryCore;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Dtos;
using SharedLibraryCore.Helpers;
using SharedLibraryCore.Interfaces;
using WebfrontCore.Permissions;
using WebfrontCore.QueryHelpers.Models;
using EFClient = Data.Models.Client.EFClient;

namespace IW4MAdmin.Application.QueryHelpers;

public class ClientResourceQueryHelper : IResourceQueryHelper<ClientResourceRequest, ClientResourceResponse>
{
public ApplicationConfiguration _appConfig { get; }
private readonly IDatabaseContextFactory _contextFactory;
private readonly IGeoLocationService _geoLocationService;

Expand All @@ -27,8 +30,10 @@ private class ClientAlias
public EFAlias Alias { get; set; }
}

public ClientResourceQueryHelper(IDatabaseContextFactory contextFactory, IGeoLocationService geoLocationService)
public ClientResourceQueryHelper(IDatabaseContextFactory contextFactory, IGeoLocationService geoLocationService,
ApplicationConfiguration appConfig)
{
_appConfig = appConfig;
_contextFactory = contextFactory;
_geoLocationService = geoLocationService;
}
Expand Down Expand Up @@ -75,7 +80,9 @@ private async Task<ResourceQueryHelperResult<ClientResourceResponse>> StartFromC

if (!string.IsNullOrWhiteSpace(query.ClientIp))
{
clientAliases = SearchByIp(query, clientAliases);
clientAliases = SearchByIp(query, clientAliases,
_appConfig.HasPermission(query.RequesterPermission, WebfrontEntity.ClientIPAddress,
WebfrontPermission.Read));
}

var iqGroupedClientAliases = clientAliases.GroupBy(a => new { a.Client.ClientId, a.Client.LastConnection });
Expand Down Expand Up @@ -203,7 +210,7 @@ private static Expression<Func<ClientAlias, bool>> ExactNameMatch(string lowerCa
}

private static IQueryable<ClientAlias> SearchByIp(ClientResourceRequest query,
IQueryable<ClientAlias> clientAliases)
IQueryable<ClientAlias> clientAliases, bool canSearchIP)
{
var ipString = query.ClientIp.Trim();
var ipAddress = ipString.ConvertToIP();
Expand All @@ -213,7 +220,7 @@ private static IQueryable<ClientAlias> SearchByIp(ClientResourceRequest query,
clientAliases = clientAliases.Where(clientAlias =>
clientAlias.Alias.IPAddress != null && clientAlias.Alias.IPAddress == ipAddress);
}
else
else if(canSearchIP)
{
clientAliases = clientAliases.Where(clientAlias =>
EF.Functions.Like(clientAlias.Alias.SearchableIPAddress, $"{ipString}%"));
Expand Down
3 changes: 2 additions & 1 deletion WebfrontCore/Controllers/Client/ClientController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ public async Task<IActionResult> AdvancedFind(ClientResourceRequest request)
{
ViewBag.Title = Localization["WEBFRONT_SEARCH_RESULTS_TITLE"];
ViewBag.ClientResourceRequest = request;


request.RequesterPermission = Client.Level;
var response = await _clientResourceHelper.QueryResource(request);
return request.Offset > 0
? PartialView("Find/_AdvancedFindList", response.Results)
Expand Down
6 changes: 4 additions & 2 deletions WebfrontCore/QueryHelpers/Models/ClientResourceRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ public class ClientResourceRequest : ClientPaginationRequest
public EFClient.Permission? ClientLevel { get; set; }
public Reference.Game? GameName { get; set; }
public bool IncludeGeolocationData { get; set; } = true;


public EFClient.Permission RequesterPermission { get; set; } = EFClient.Permission.User;

public bool HasData => !string.IsNullOrEmpty(ClientName) || !string.IsNullOrEmpty(ClientIp) ||
!string.IsNullOrEmpty(ClientGuid) || ClientLevel is not null || GameName is not null;
!string.IsNullOrEmpty(ClientGuid) || ClientLevel is not null || GameName is not null;
}

0 comments on commit 7ecbf85

Please sign in to comment.