mirror of
https://github.com/RaidMax/IW4M-Admin.git
synced 2025-06-11 15:52:25 -05:00
More EF Changes.
Added DB importer
This commit is contained in:
@ -16,12 +16,22 @@ namespace SharedLibrary.Services
|
||||
{
|
||||
public async Task<EFAlias> Create(EFAlias entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
throw new Exception();
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity.Link = await context.AliasLinks.FirstAsync(a => a.AliasLinkId == entity.Link.AliasLinkId);
|
||||
var addedEntity = context.Aliases.Add(entity);
|
||||
var alias = new EFAlias()
|
||||
{
|
||||
Active = true,
|
||||
DateAdded = DateTime.UtcNow,
|
||||
IPAddress = entity.IPAddress,
|
||||
Name = entity.Name
|
||||
};
|
||||
|
||||
entity.Link = await context.AliasLinks
|
||||
.FirstAsync(a => a.AliasLinkId == entity.Link.AliasLinkId);
|
||||
context.Aliases.Add(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return addedEntity;
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,11 +42,11 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<EFAlias> Delete(EFAlias entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity = context.Aliases.Single(e => e.AliasId == entity.AliasId);
|
||||
entity.Active = false;
|
||||
context.Entry(entity).State = EntityState.Modified;
|
||||
var alias = context.Aliases
|
||||
.Single(e => e.AliasId == entity.AliasId);
|
||||
alias.Active = false;
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
}
|
||||
@ -44,14 +54,18 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<IList<EFAlias>> Find(Func<EFAlias, bool> expression)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await Task.Run(() => context.Aliases.Where(expression).ToList());
|
||||
using (var context = new DatabaseContext())
|
||||
return await Task.Run(() => context.Aliases
|
||||
.AsNoTracking()
|
||||
.Include(a => a.Link.Children)
|
||||
.Where(expression).ToList());
|
||||
}
|
||||
|
||||
public async Task<EFAlias> Get(int entityID)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
return await context.Aliases
|
||||
.AsNoTracking()
|
||||
.SingleOrDefaultAsync(e => e.AliasId == entityID);
|
||||
}
|
||||
|
||||
@ -62,7 +76,8 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<EFAlias> Update(EFAlias entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
throw new Exception();
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity = context.Aliases.Attach(entity);
|
||||
context.Entry(entity).State = EntityState.Modified;
|
||||
@ -73,7 +88,7 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<EFAliasLink> CreateLink(EFAliasLink link)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
context.AliasLinks.Add(link);
|
||||
await context.SaveChangesAsync();
|
||||
|
@ -11,46 +11,61 @@ using System.Linq.Expressions;
|
||||
|
||||
namespace SharedLibrary.Services
|
||||
{
|
||||
|
||||
public class ClientService : Interfaces.IEntityService<EFClient>
|
||||
{
|
||||
private Dictionary<int, IW4MAdminDatabaseContext> _context;
|
||||
|
||||
public ClientService()
|
||||
{
|
||||
_context = new Dictionary<int, IW4MAdminDatabaseContext>();
|
||||
}
|
||||
public async Task<EFClient> Create(EFClient entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
|
||||
bool hasExistingAlias = false;
|
||||
// get all aliases by IP
|
||||
var alias = await context.Aliases.FirstOrDefaultAsync(a => a.IP == entity.IPAddress);
|
||||
EFAliasLink link = alias?.Link;
|
||||
var aliases = await context.Aliases
|
||||
.Include(a => a.Link)
|
||||
.Where(a => a.IPAddress == entity.IPAddress)
|
||||
.ToListAsync();
|
||||
|
||||
// see if they have a matching IP + Name but new NetworkId
|
||||
var existingAlias = aliases.SingleOrDefault(a => a.Name == entity.Name);
|
||||
// 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()
|
||||
{
|
||||
Active = true,
|
||||
DateAdded = DateTime.UtcNow,
|
||||
IPAddress = entity.IPAddress,
|
||||
Link = aliasLink,
|
||||
Name = entity.Name,
|
||||
};
|
||||
|
||||
var client = new EFClient()
|
||||
{
|
||||
Active = true,
|
||||
Name = entity.Name,
|
||||
// set the level to the level of the existing client if they have the same IP + Name but new NetworkId
|
||||
Level = hasExistingAlias ?
|
||||
context.Clients.First(c => c.CurrentAliasId == existingAlias.AliasId).Level :
|
||||
Objects.Player.Permission.User,
|
||||
FirstConnection = DateTime.UtcNow,
|
||||
Connections = 1,
|
||||
IPAddress = entity.IPAddress,
|
||||
LastConnection = DateTime.UtcNow,
|
||||
Level = Objects.Player.Permission.User,
|
||||
Masked = false,
|
||||
NetworkId = entity.NetworkId,
|
||||
AliasLink = link ?? new EFAliasLink() { Active = true }
|
||||
AliasLink = aliasLink,
|
||||
CurrentAlias = existingAlias
|
||||
};
|
||||
|
||||
client.AliasLink.Children.Add(new EFAlias()
|
||||
{
|
||||
Active = true,
|
||||
DateAdded = DateTime.UtcNow,
|
||||
IP = entity.IPAddress,
|
||||
Link = client.AliasLink,
|
||||
Name = entity.Name
|
||||
});
|
||||
|
||||
context.Clients.Add(client);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
@ -60,11 +75,11 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<EFClient> Delete(EFClient entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity = context.Clients.Single(e => e.ClientId == entity.ClientId);
|
||||
var client = context.Clients
|
||||
.Single(e => e.ClientId == entity.ClientId);
|
||||
entity.Active = false;
|
||||
entity.Level = Objects.Player.Permission.User;
|
||||
context.Entry(entity).State = EntityState.Modified;
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
@ -73,25 +88,31 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<IList<EFClient>> Find(Func<EFClient, bool> e)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
return await Task.Run(() => context.Clients
|
||||
.AsNoTracking()
|
||||
.Include(c => c.CurrentAlias)
|
||||
.Include(c => c.AliasLink.Children)
|
||||
.Where(e).ToList());
|
||||
}
|
||||
|
||||
public async Task<EFClient> Get(int entityID)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await new IW4MAdminDatabaseContext().Clients
|
||||
using (var context = new DatabaseContext())
|
||||
return await new DatabaseContext().Clients
|
||||
.AsNoTracking()
|
||||
.Include(c => c.CurrentAlias)
|
||||
.Include(c => c.AliasLink.Children)
|
||||
.SingleOrDefaultAsync(e => e.ClientId == entityID);
|
||||
}
|
||||
|
||||
public async Task<EFClient> GetUnique(string entityAttribute)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
return await context.Clients
|
||||
.AsNoTracking()
|
||||
.Include(c => c.CurrentAlias)
|
||||
.Include(c => c.AliasLink.Children)
|
||||
.SingleOrDefaultAsync(c => c.NetworkId == entityAttribute);
|
||||
}
|
||||
@ -99,42 +120,97 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<EFClient> Update(EFClient entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity = context.Clients.Attach(entity);
|
||||
context.Entry(entity).State = EntityState.Modified;
|
||||
// grab the context version of the entity
|
||||
var client = context.Clients
|
||||
.Include(c => c.AliasLink)
|
||||
.Include(c=> c.CurrentAlias)
|
||||
.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
|
||||
var matchingClients = await context.Clients
|
||||
.Where(c => c.CurrentAliasId == client.CurrentAliasId)
|
||||
.ToListAsync();
|
||||
|
||||
// update all related clients level
|
||||
matchingClients.ForEach(c => c.Level = (client.Level == Objects.Player.Permission.Banned) ?
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
// 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;
|
||||
client.TotalConnectionTime = entity.TotalConnectionTime;
|
||||
|
||||
// update in database
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
|
||||
// this is set so future updates don't trigger a new alias add
|
||||
if (entity.CurrentAlias.AliasId == 0)
|
||||
entity.CurrentAlias.AliasId = client.CurrentAlias.AliasId;
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
#region ServiceSpecific
|
||||
#region ServiceSpecific
|
||||
public async Task<IList<EFClient>> GetOwners()
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await context.Clients.Where(c => c.Level == Objects.Player.Permission.Owner).ToListAsync();
|
||||
using (var context = new DatabaseContext())
|
||||
return await context.Clients
|
||||
.Where(c => c.Level == Objects.Player.Permission.Owner)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IList<EFClient>> GetPrivilegedClients()
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await new IW4MAdminDatabaseContext().Clients
|
||||
.Where(c => c.Level >= Objects.Player.Permission.Trusted)
|
||||
.ToListAsync();
|
||||
using (var context = new DatabaseContext())
|
||||
return await new DatabaseContext().Clients
|
||||
.AsNoTracking()
|
||||
.Include(c => c.CurrentAlias)
|
||||
.Where(c => c.Level >= Objects.Player.Permission.Trusted)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
public async Task<IList<EFClient>> GetRecentClients(int offset, int count)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await context.Clients.OrderByDescending(p => p.ClientId).Skip(offset).Take(count).ToListAsync();
|
||||
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();
|
||||
}
|
||||
|
||||
public async Task<IList<EFClient>> PruneInactivePrivilegedClients(int inactiveDays)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
var inactive = await context.Clients.Where(c => c.Level > Objects.Player.Permission.Flagged)
|
||||
.AsNoTracking()
|
||||
.Where(c => (DateTime.UtcNow - c.LastConnection).TotalDays >= inactiveDays)
|
||||
.ToListAsync();
|
||||
inactive.ForEach(c => c.Level = Objects.Player.Permission.User);
|
||||
@ -145,14 +221,15 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<int> GetTotalClientsAsync()
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
return await context.Clients.CountAsync();
|
||||
using (var context = new DatabaseContext())
|
||||
return await context.Clients
|
||||
.CountAsync();
|
||||
}
|
||||
|
||||
public Task<EFClient> CreateProxy()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
@ -15,13 +15,29 @@ namespace SharedLibrary.Services
|
||||
{
|
||||
public async Task<EFPenalty> Create(EFPenalty entity)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
entity.Offender = context.Clients.First(e => e.ClientId == entity.Offender.ClientId);
|
||||
entity.Punisher = context.Clients.First(e => e.ClientId == entity.Punisher.ClientId);
|
||||
entity.Link = context.AliasLinks.First(l => l.AliasLinkId == entity.Link.AliasLinkId);
|
||||
if (entity.Expires == DateTime.MinValue)
|
||||
|
||||
// make bans propogate to all aliases
|
||||
if (entity.Type == Objects.Penalty.PenaltyType.Ban)
|
||||
{
|
||||
entity.Expires = DateTime.Parse(System.Data.SqlTypes.SqlDateTime.MaxValue.ToString());
|
||||
await context.Clients
|
||||
.Where(c => c.AliasLinkId == entity.Link.AliasLinkId)
|
||||
.ForEachAsync(c => c.Level = Objects.Player.Permission.Banned);
|
||||
}
|
||||
|
||||
// make flags propogate to all aliases
|
||||
else if (entity.Type == Objects.Penalty.PenaltyType.Flag)
|
||||
{
|
||||
await context.Clients
|
||||
.Where(c => c.AliasLinkId == entity.Link.AliasLinkId)
|
||||
.ForEachAsync(c => c.Level = Objects.Player.Permission.Flagged);
|
||||
}
|
||||
|
||||
context.Penalties.Add(entity);
|
||||
await context.SaveChangesAsync();
|
||||
return entity;
|
||||
@ -40,7 +56,7 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<IList<EFPenalty>> Find(Func<EFPenalty, bool> expression)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
return await Task.Run(() => context.Penalties
|
||||
.Include(p => p.Offender)
|
||||
@ -68,11 +84,12 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<IList<EFPenalty>> GetRecentPenalties(int count, int offset)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
return await context.Penalties
|
||||
.Include(p => p.Offender)
|
||||
.Include(p => p.Punisher)
|
||||
.Where(p => p.Active)
|
||||
.AsNoTracking()
|
||||
.Include(p => p.Offender.CurrentAlias)
|
||||
.Include(p => p.Punisher.CurrentAlias)
|
||||
.Where(p => p.Active)
|
||||
.OrderByDescending(p => p.When)
|
||||
.Skip(offset)
|
||||
.Take(count)
|
||||
@ -81,18 +98,19 @@ namespace SharedLibrary.Services
|
||||
|
||||
public async Task<IList<EFPenalty>> GetClientPenaltiesAsync(int clientId)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
return await context.Penalties
|
||||
.Where(p => p.OffenderId == clientId)
|
||||
.Where(p => p.Active)
|
||||
.Include(p => p.Offender)
|
||||
.Include(p => p.Punisher)
|
||||
.ToListAsync();
|
||||
.AsNoTracking()
|
||||
.Where(p => p.OffenderId == clientId)
|
||||
.Where(p => p.Active)
|
||||
.Include(p => p.Offender.CurrentAlias)
|
||||
.Include(p => p.Punisher.CurrentAlias)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task RemoveActivePenalties(int aliasLinkId)
|
||||
{
|
||||
using (var context = new IW4MAdminDatabaseContext())
|
||||
using (var context = new DatabaseContext())
|
||||
{
|
||||
var now = DateTime.UtcNow;
|
||||
var penalties = await context.Penalties
|
||||
@ -104,10 +122,11 @@ namespace SharedLibrary.Services
|
||||
penalties.ForEach(async p =>
|
||||
{
|
||||
p.Active = false;
|
||||
var clients = await context.Clients.Where(cl => cl.AliasLinkId == p.LinkId).ToListAsync();
|
||||
foreach (var c in clients)
|
||||
if (c.Level == Objects.Player.Permission.Banned)
|
||||
c.Level = Objects.Player.Permission.User;
|
||||
// reset the player levels
|
||||
if (p.Type == Objects.Penalty.PenaltyType.Ban)
|
||||
await context.Clients
|
||||
.Where(c => c.AliasLinkId == p.LinkId)
|
||||
.ForEachAsync(c => c.Level = Objects.Player.Permission.User);
|
||||
});
|
||||
|
||||
await context.SaveChangesAsync();
|
||||
|
Reference in New Issue
Block a user