2017-11-25 20:29:58 -05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
using System.Linq.Expressions;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Objects;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
namespace SharedLibraryCore.Services
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
|
2017-11-25 20:29:58 -05:00
|
|
|
|
public class ClientService : Interfaces.IEntityService<EFClient>
|
|
|
|
|
{
|
|
|
|
|
public async Task<EFClient> Create(EFClient entity)
|
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
bool hasExistingAlias = false;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
// get all aliases by IP
|
2017-11-29 19:35:50 -05:00
|
|
|
|
var aliases = await context.Aliases
|
|
|
|
|
.Include(a => a.Link)
|
|
|
|
|
.Where(a => a.IPAddress == entity.IPAddress)
|
|
|
|
|
.ToListAsync();
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
2017-11-29 19:35:50 -05:00
|
|
|
|
// see if they have a matching IP + Name but new NetworkId
|
2018-02-17 01:13:38 -05:00
|
|
|
|
var existingAlias = aliases.FirstOrDefault(a => a.Name == entity.Name);
|
2017-11-29 19:35:50 -05:00
|
|
|
|
// if existing alias matches link them
|
|
|
|
|
EFAliasLink aliasLink = existingAlias?.Link;
|
|
|
|
|
// if no exact matches find the first IP that matches
|
|
|
|
|
aliasLink = aliasLink ?? aliases.FirstOrDefault()?.Link;
|
|
|
|
|
// if no exact or IP matches, create new link
|
|
|
|
|
aliasLink = aliasLink ?? new EFAliasLink()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// this has to be set here because we can't evalute it properly later
|
|
|
|
|
hasExistingAlias = existingAlias != null;
|
|
|
|
|
|
|
|
|
|
// if no existing alias create new alias
|
|
|
|
|
existingAlias = existingAlias ?? new EFAlias()
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
Active = true,
|
2017-11-29 19:35:50 -05:00
|
|
|
|
DateAdded = DateTime.UtcNow,
|
|
|
|
|
IPAddress = entity.IPAddress,
|
|
|
|
|
Link = aliasLink,
|
2017-11-25 20:29:58 -05:00
|
|
|
|
Name = entity.Name,
|
2017-11-29 19:35:50 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var client = new EFClient()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
// set the level to the level of the existing client if they have the same IP + Name but new NetworkId
|
2018-02-21 20:29:23 -05:00
|
|
|
|
// fixme: issues?
|
2017-11-29 19:35:50 -05:00
|
|
|
|
Level = hasExistingAlias ?
|
2018-02-23 02:06:13 -05:00
|
|
|
|
context.Clients.First(c => c.AliasLinkId == existingAlias.LinkId).Level :
|
2018-02-15 23:01:28 -05:00
|
|
|
|
Player.Permission.User,
|
2017-11-25 20:29:58 -05:00
|
|
|
|
FirstConnection = DateTime.UtcNow,
|
|
|
|
|
Connections = 1,
|
|
|
|
|
LastConnection = DateTime.UtcNow,
|
|
|
|
|
Masked = false,
|
|
|
|
|
NetworkId = entity.NetworkId,
|
2017-11-29 19:35:50 -05:00
|
|
|
|
AliasLink = aliasLink,
|
2018-04-04 15:38:34 -04:00
|
|
|
|
CurrentAlias = existingAlias,
|
2017-11-25 20:29:58 -05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
context.Clients.Add(client);
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<EFClient> Delete(EFClient entity)
|
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
var client = context.Clients
|
|
|
|
|
.Single(e => e.ClientId == entity.ClientId);
|
2017-11-25 20:29:58 -05:00
|
|
|
|
entity.Active = false;
|
|
|
|
|
context.Entry(entity).State = EntityState.Modified;
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IList<EFClient>> Find(Func<EFClient, bool> e)
|
|
|
|
|
{
|
2018-02-07 00:19:06 -05:00
|
|
|
|
return await Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
using (var context = new DatabaseContext())
|
2018-02-11 20:17:20 -05:00
|
|
|
|
{
|
2018-02-07 00:19:06 -05:00
|
|
|
|
return context.Clients
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Include(c => c.CurrentAlias)
|
|
|
|
|
.Include(c => c.AliasLink.Children)
|
|
|
|
|
.Where(e).ToList();
|
2018-02-11 20:17:20 -05:00
|
|
|
|
}
|
2018-02-07 00:19:06 -05:00
|
|
|
|
});
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<EFClient> Get(int entityID)
|
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2018-02-10 23:33:42 -05:00
|
|
|
|
{
|
2018-05-14 13:55:10 -04:00
|
|
|
|
context.ChangeTracker.AutoDetectChangesEnabled = false;
|
|
|
|
|
context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
|
|
|
|
|
|
|
|
|
var iqClient = from client in context.Clients
|
|
|
|
|
.Include(c => c.CurrentAlias)
|
|
|
|
|
.Include(c => c.AliasLink.Children)
|
|
|
|
|
where client.ClientId == entityID
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
Client = client,
|
|
|
|
|
LinkedAccounts = (from linkedClient in context.Clients
|
|
|
|
|
where client.AliasLinkId == linkedClient.AliasLinkId
|
|
|
|
|
select new
|
|
|
|
|
{
|
|
|
|
|
linkedClient.ClientId,
|
|
|
|
|
linkedClient.NetworkId
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
var foundClient = await iqClient.FirstOrDefaultAsync();
|
|
|
|
|
|
2018-05-24 15:48:57 -04:00
|
|
|
|
if (foundClient == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2018-05-14 13:55:10 -04:00
|
|
|
|
foundClient.Client.LinkedAccounts = new Dictionary<int, long>();
|
|
|
|
|
// todo: find out the best way to do this
|
|
|
|
|
// I'm doing this here because I don't know the best way to have multiple awaits in the query
|
|
|
|
|
foreach (var linked in foundClient.LinkedAccounts)
|
|
|
|
|
foundClient.Client.LinkedAccounts.Add(linked.ClientId, linked.NetworkId);
|
|
|
|
|
|
|
|
|
|
return foundClient.Client;
|
2018-02-10 23:33:42 -05:00
|
|
|
|
}
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-10 23:33:42 -05:00
|
|
|
|
public async Task<EFClient> GetUnique(long entityAttribute)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
return await context.Clients
|
2017-11-29 19:35:50 -05:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Include(c => c.CurrentAlias)
|
2017-11-25 20:29:58 -05:00
|
|
|
|
.Include(c => c.AliasLink.Children)
|
2018-02-10 23:33:42 -05:00
|
|
|
|
.SingleOrDefaultAsync(c => c.NetworkId == (long)entityAttribute);
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<EFClient> Update(EFClient entity)
|
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
// grab the context version of the entity
|
|
|
|
|
var client = context.Clients
|
|
|
|
|
.Include(c => c.AliasLink)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
.Include(c => c.CurrentAlias)
|
2017-11-29 19:35:50 -05:00
|
|
|
|
.Single(e => e.ClientId == entity.ClientId);
|
|
|
|
|
|
|
|
|
|
// if their level has been changed
|
|
|
|
|
if (entity.Level != client.Level)
|
|
|
|
|
{
|
|
|
|
|
// get all clients that use the same aliasId
|
2018-05-24 15:48:57 -04:00
|
|
|
|
var matchingClients = context.Clients
|
|
|
|
|
.Where(c => c.CurrentAliasId == client.CurrentAliasId);
|
2017-11-29 19:35:50 -05:00
|
|
|
|
|
|
|
|
|
// update all related clients level
|
2018-05-24 15:48:57 -04:00
|
|
|
|
await matchingClients.ForEachAsync(c => c.Level = (client.Level == Player.Permission.Banned) ?
|
2017-11-29 19:35:50 -05:00
|
|
|
|
client.Level : entity.Level);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// their alias has been updated and not yet saved
|
|
|
|
|
if (entity.CurrentAlias.AliasId == 0)
|
|
|
|
|
{
|
|
|
|
|
client.CurrentAlias = new EFAlias()
|
|
|
|
|
{
|
|
|
|
|
Active = true,
|
|
|
|
|
DateAdded = DateTime.UtcNow,
|
|
|
|
|
IPAddress = entity.CurrentAlias.IPAddress,
|
|
|
|
|
Name = entity.CurrentAlias.Name,
|
|
|
|
|
Link = client.AliasLink
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-06 02:22:19 -05:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
client.CurrentAliasId = entity.CurrentAliasId;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-29 19:35:50 -05:00
|
|
|
|
// set remaining non-navigation properties that may have been updated
|
|
|
|
|
client.Level = entity.Level;
|
|
|
|
|
client.LastConnection = entity.LastConnection;
|
|
|
|
|
client.Connections = entity.Connections;
|
|
|
|
|
client.FirstConnection = entity.FirstConnection;
|
|
|
|
|
client.Masked = entity.Masked;
|
2018-04-04 15:38:34 -04:00
|
|
|
|
client.TotalConnectionTime = entity.TotalConnectionTime;
|
|
|
|
|
client.Password = entity.Password;
|
|
|
|
|
client.PasswordSalt = entity.PasswordSalt;
|
2017-11-29 19:35:50 -05:00
|
|
|
|
|
|
|
|
|
// update in database
|
2017-11-25 20:29:58 -05:00
|
|
|
|
await context.SaveChangesAsync();
|
2017-11-29 19:35:50 -05:00
|
|
|
|
|
|
|
|
|
// this is set so future updates don't trigger a new alias add
|
|
|
|
|
if (entity.CurrentAlias.AliasId == 0)
|
2018-02-07 00:19:06 -05:00
|
|
|
|
entity.CurrentAlias.AliasId = client.CurrentAlias.AliasId;
|
2017-11-29 19:35:50 -05:00
|
|
|
|
return client;
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-07 00:19:06 -05:00
|
|
|
|
#region ServiceSpecific
|
2017-11-25 20:29:58 -05:00
|
|
|
|
public async Task<IList<EFClient>> GetOwners()
|
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
|
|
|
|
return await context.Clients
|
2018-04-09 15:17:10 -04:00
|
|
|
|
.Where(c => c.Level == Player.Permission.Owner)
|
2017-11-29 19:35:50 -05:00
|
|
|
|
.ToListAsync();
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-16 23:24:03 -05:00
|
|
|
|
public async Task<bool> IsAuthenticated(int clientIP)
|
|
|
|
|
{
|
|
|
|
|
using (var context = new DatabaseContext())
|
|
|
|
|
{
|
|
|
|
|
var iqMatching = from alias in context.Aliases
|
|
|
|
|
where alias.IPAddress == clientIP
|
|
|
|
|
join client in context.Clients
|
|
|
|
|
on alias.LinkId equals client.AliasLinkId
|
|
|
|
|
where client.Level > Player.Permission.Trusted
|
|
|
|
|
select client;
|
|
|
|
|
|
|
|
|
|
return (await iqMatching.CountAsync()) > 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-25 20:29:58 -05:00
|
|
|
|
public async Task<IList<EFClient>> GetPrivilegedClients()
|
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2018-02-11 20:17:20 -05:00
|
|
|
|
{
|
2018-05-14 13:55:10 -04:00
|
|
|
|
context.ChangeTracker.AutoDetectChangesEnabled = false;
|
|
|
|
|
context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
|
|
|
|
|
|
|
|
|
|
var iqClients = from client in context.Clients
|
|
|
|
|
.Include(c => c.CurrentAlias)
|
|
|
|
|
where client.Level >= Player.Permission.Trusted
|
|
|
|
|
select client;
|
|
|
|
|
|
|
|
|
|
return await iqClients.ToListAsync();
|
2018-02-11 20:17:20 -05:00
|
|
|
|
}
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-11 20:17:20 -05:00
|
|
|
|
public async Task<IList<EFClient>> GetClientByName(string name)
|
|
|
|
|
{
|
2018-04-21 18:18:20 -04:00
|
|
|
|
if (name.Length < 3)
|
|
|
|
|
return new List<EFClient>();
|
|
|
|
|
|
2018-02-11 20:17:20 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
|
|
|
|
{
|
|
|
|
|
var iqClients = (from alias in context.Aliases
|
2018-04-09 15:17:10 -04:00
|
|
|
|
.AsNoTracking()
|
2018-05-14 13:55:10 -04:00
|
|
|
|
where alias.Name.ToLower()
|
|
|
|
|
.Contains(name.ToLower())
|
|
|
|
|
join link in context.AliasLinks
|
|
|
|
|
on alias.LinkId equals link.AliasLinkId
|
|
|
|
|
join client in context.Clients
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
on alias.LinkId equals client.AliasLinkId
|
|
|
|
|
select client)
|
2018-04-09 15:17:10 -04:00
|
|
|
|
.Distinct()
|
|
|
|
|
.Include(c => c.CurrentAlias)
|
|
|
|
|
.Include(c => c.AliasLink.Children);
|
2018-02-23 02:06:13 -05:00
|
|
|
|
|
|
|
|
|
return await iqClients.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IList<EFClient>> GetClientByIP(int ipAddress)
|
|
|
|
|
{
|
|
|
|
|
using (var context = new DatabaseContext())
|
|
|
|
|
{
|
|
|
|
|
var iqClients = (from alias in context.Aliases
|
2018-04-09 15:17:10 -04:00
|
|
|
|
.AsNoTracking()
|
2018-05-14 13:55:10 -04:00
|
|
|
|
where alias.IPAddress == ipAddress
|
|
|
|
|
join link in context.AliasLinks
|
|
|
|
|
on alias.LinkId equals link.AliasLinkId
|
|
|
|
|
join client in context.Clients
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
on alias.LinkId equals client.AliasLinkId
|
|
|
|
|
select client)
|
2018-04-09 15:17:10 -04:00
|
|
|
|
.Distinct()
|
|
|
|
|
.Include(c => c.CurrentAlias)
|
|
|
|
|
.Include(c => c.AliasLink.Children);
|
2018-02-11 20:17:20 -05:00
|
|
|
|
|
|
|
|
|
return await iqClients.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-25 20:29:58 -05:00
|
|
|
|
|
|
|
|
|
public async Task<IList<EFClient>> GetRecentClients(int offset, int count)
|
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
|
|
|
|
return await context.Clients
|
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.Include(c => c.CurrentAlias)
|
|
|
|
|
.Include(p => p.AliasLink)
|
|
|
|
|
.OrderByDescending(p => p.ClientId)
|
|
|
|
|
.Skip(offset)
|
|
|
|
|
.Take(count)
|
|
|
|
|
.ToListAsync();
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IList<EFClient>> PruneInactivePrivilegedClients(int inactiveDays)
|
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
2017-11-25 20:29:58 -05:00
|
|
|
|
{
|
|
|
|
|
var inactive = await context.Clients.Where(c => c.Level > Objects.Player.Permission.Flagged)
|
2017-11-29 19:35:50 -05:00
|
|
|
|
.AsNoTracking()
|
2017-11-25 20:29:58 -05:00
|
|
|
|
.Where(c => (DateTime.UtcNow - c.LastConnection).TotalDays >= inactiveDays)
|
|
|
|
|
.ToListAsync();
|
2018-04-09 15:17:10 -04:00
|
|
|
|
inactive.ForEach(c => c.Level = Player.Permission.User);
|
2017-11-25 20:29:58 -05:00
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
return inactive;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> GetTotalClientsAsync()
|
|
|
|
|
{
|
2017-11-29 19:35:50 -05:00
|
|
|
|
using (var context = new DatabaseContext())
|
|
|
|
|
return await context.Clients
|
|
|
|
|
.CountAsync();
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task<EFClient> CreateProxy()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
2018-02-09 02:21:25 -05:00
|
|
|
|
|
|
|
|
|
public async Task<int> GetTotalPlayTime()
|
|
|
|
|
{
|
|
|
|
|
using (var context = new DatabaseContext())
|
|
|
|
|
return await context.Clients.SumAsync(c => c.TotalConnectionTime);
|
|
|
|
|
}
|
2018-02-07 00:19:06 -05:00
|
|
|
|
#endregion
|
2017-11-25 20:29:58 -05:00
|
|
|
|
}
|
|
|
|
|
}
|