IW4M-Admin/WebfrontCore/Controllers/ActionController.cs

468 lines
17 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Data.Models.Client;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
2018-04-08 02:44:42 -04:00
using SharedLibraryCore;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Configuration;
move all the deployment setup into 2.4 pr (#85) * don't run build commands in release * fix test file * Set up CI with Azure Pipelines [skip ci] * Include fonts and fix automessage hidden command * more project changes * migration from bower to libman * more lib man changes * project update for sneaky commands * add missing canvas.js dep update projects not to have stupid extra dlls include in previous * update pipeline file * update post publish script and pipeline definition * fix broken yaml * move encoding conversion to seperate script * remove extra uneeded rank icons remove garbage language files being created remove frontend lib when done * fix publish script path * grab localizations through powershell * fix broken batch :shrug: * actually fixed * only include runtime compilation in debug mode for webfront * don't deploy un minified css use full jquery version * add step to download the scss for open iconic change the font path * update mkdir for iconic path * don't include old iconic css * correct font path for real now * copy script plugins * lots of changes for deployment * build the projects * use projectdir instead of solution dir * nerf script commands plugin fix live radar left over command * actually kill script command post build * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * fix the font file copy (I think) * maybe fix delete folder issue * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines
2019-12-02 16:52:36 -05:00
using SharedLibraryCore.Interfaces;
using WebfrontCore.Permissions;
using WebfrontCore.ViewModels;
namespace WebfrontCore.Controllers
{
public class ActionController : BaseController
{
private readonly ApplicationConfiguration _appConfig;
private readonly string _banCommandName;
private readonly string _tempbanCommandName;
private readonly string _unbanCommandName;
private readonly string _sayCommandName;
private readonly string _kickCommandName;
private readonly string _flagCommandName;
private readonly string _unflagCommandName;
private readonly string _setLevelCommandName;
public ActionController(IManager manager, IEnumerable<IManagerCommand> registeredCommands,
ApplicationConfiguration appConfig) : base(manager)
move all the deployment setup into 2.4 pr (#85) * don't run build commands in release * fix test file * Set up CI with Azure Pipelines [skip ci] * Include fonts and fix automessage hidden command * more project changes * migration from bower to libman * more lib man changes * project update for sneaky commands * add missing canvas.js dep update projects not to have stupid extra dlls include in previous * update pipeline file * update post publish script and pipeline definition * fix broken yaml * move encoding conversion to seperate script * remove extra uneeded rank icons remove garbage language files being created remove frontend lib when done * fix publish script path * grab localizations through powershell * fix broken batch :shrug: * actually fixed * only include runtime compilation in debug mode for webfront * don't deploy un minified css use full jquery version * add step to download the scss for open iconic change the font path * update mkdir for iconic path * don't include old iconic css * correct font path for real now * copy script plugins * lots of changes for deployment * build the projects * use projectdir instead of solution dir * nerf script commands plugin fix live radar left over command * actually kill script command post build * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * fix the font file copy (I think) * maybe fix delete folder issue * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines
2019-12-02 16:52:36 -05:00
{
_appConfig = appConfig;
foreach (var cmd in registeredCommands)
{
var type = cmd.GetType().Name;
switch (type)
{
case nameof(BanCommand):
_banCommandName = cmd.Name;
break;
case nameof(TempBanCommand):
_tempbanCommandName = cmd.Name;
break;
case nameof(UnbanCommand):
_unbanCommandName = cmd.Name;
break;
// todo: this should be flag driven
case "SayCommand":
_sayCommandName = cmd.Name;
break;
case nameof(KickCommand):
_kickCommandName = cmd.Name;
break;
case nameof(FlagClientCommand):
_flagCommandName = cmd.Name;
break;
case nameof(UnflagClientCommand):
_unflagCommandName = cmd.Name;
break;
case nameof(SetLevelCommand):
_setLevelCommandName = cmd.Name;
break;
}
}
move all the deployment setup into 2.4 pr (#85) * don't run build commands in release * fix test file * Set up CI with Azure Pipelines [skip ci] * Include fonts and fix automessage hidden command * more project changes * migration from bower to libman * more lib man changes * project update for sneaky commands * add missing canvas.js dep update projects not to have stupid extra dlls include in previous * update pipeline file * update post publish script and pipeline definition * fix broken yaml * move encoding conversion to seperate script * remove extra uneeded rank icons remove garbage language files being created remove frontend lib when done * fix publish script path * grab localizations through powershell * fix broken batch :shrug: * actually fixed * only include runtime compilation in debug mode for webfront * don't deploy un minified css use full jquery version * add step to download the scss for open iconic change the font path * update mkdir for iconic path * don't include old iconic css * correct font path for real now * copy script plugins * lots of changes for deployment * build the projects * use projectdir instead of solution dir * nerf script commands plugin fix live radar left over command * actually kill script command post build * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * fix the font file copy (I think) * maybe fix delete folder issue * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines * Update azure-pipelines.yml for Azure Pipelines
2019-12-02 16:52:36 -05:00
}
public IActionResult BanForm()
{
2022-04-19 19:43:58 -04:00
var info = new ActionInfo
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_BAN_NAME"],
Name = "Ban",
2022-04-19 19:43:58 -04:00
Inputs = new List<InputInfo>
{
2022-04-19 19:43:58 -04:00
new()
{
Name = "Reason",
Label = Localization["WEBFRONT_ACTION_LABEL_REASON"],
},
2022-04-19 19:43:58 -04:00
new()
{
Name = "PresetReason",
Type = "select",
Label = Localization["WEBFRONT_ACTION_LABEL_PRESET_REASON"],
Values = GetPresetPenaltyReasons()
},
2022-04-19 19:43:58 -04:00
new()
{
Name = "Duration",
Label = Localization["WEBFRONT_ACTION_LABEL_DURATION"],
Type = "select",
Values = _appConfig.BanDurations
.Select((item, index) => new
{
Id = (index + 1).ToString(),
Value = item.HumanizeForCurrentCulture()
}
)
.Append(new
{
Id = (_appConfig.BanDurations.Length + 1).ToString(),
Value = Localization["WEBFRONT_ACTION_SELECTION_PERMANENT"]
}).ToDictionary(duration => duration.Id, duration => duration.Value),
}
},
2019-08-04 18:06:07 -04:00
Action = "BanAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
public async Task<IActionResult> BanAsync(int targetId, string reason, int duration, string presetReason = null)
{
var fallthroughReason = presetReason ?? reason;
string command;
// permanent
if (duration > _appConfig.BanDurations.Length)
{
command = $"{_appConfig.CommandPrefix}{_banCommandName} @{targetId} {fallthroughReason}";
}
// temporary ban
else
{
var durationSpan = _appConfig.BanDurations[duration - 1];
var durationValue = durationSpan.TotalHours.ToString(CultureInfo.InvariantCulture) +
Localization["GLOBAL_TIME_HOURS"][0];
command =
$"{_appConfig.CommandPrefix}{_tempbanCommandName} @{targetId} {durationValue} {fallthroughReason}";
}
var server = Manager.GetServers().First();
2022-04-19 19:43:58 -04:00
return await Task.FromResult(RedirectToAction("Execute", "Console", new
{
serverId = server.EndPoint,
command
}));
}
public IActionResult UnbanForm()
{
2022-04-19 19:43:58 -04:00
var info = new ActionInfo
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_UNBAN_NAME"],
Name = "Unban",
2022-04-19 19:43:58 -04:00
Inputs = new List<InputInfo>
{
2022-04-19 19:43:58 -04:00
new()
{
Name = "Reason",
Label = Localization["WEBFRONT_ACTION_LABEL_REASON"],
}
},
2019-08-04 18:06:07 -04:00
Action = "UnbanAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
2022-04-19 19:43:58 -04:00
public async Task<IActionResult> UnbanAsync(int targetId, string reason)
{
var server = Manager.GetServers().First();
2022-04-19 19:43:58 -04:00
return await Task.FromResult(RedirectToAction("Execute", "Console", new
{
serverId = server.EndPoint,
2022-04-19 19:43:58 -04:00
command = $"{_appConfig.CommandPrefix}{_unbanCommandName} @{targetId} {reason}"
}));
}
public IActionResult LoginForm()
{
2022-04-19 19:43:58 -04:00
var login = new ActionInfo
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_LOGIN_NAME"],
Name = "Login",
2022-04-19 19:43:58 -04:00
Inputs = new List<InputInfo>
{
2022-04-19 19:43:58 -04:00
new()
{
Name = "clientId",
2022-04-19 19:43:58 -04:00
Label = Localization["WEBFRONT_ACTION_LABEL_ID"],
Required = true
},
2022-04-19 19:43:58 -04:00
new()
{
Name = "Password",
Label = Localization["WEBFRONT_ACTION_LABEL_PASSWORD"],
Type = "password",
2022-04-19 19:43:58 -04:00
Required = true
}
},
2022-04-19 19:43:58 -04:00
Action = "Login"
};
return View("_ActionForm", login);
}
2022-04-19 19:43:58 -04:00
public async Task<IActionResult> Login(int clientId, string password)
{
2022-04-19 19:43:58 -04:00
return await Task.FromResult(RedirectToAction("Login", "Account", new {clientId, password}));
}
public IActionResult EditForm()
{
2022-04-19 19:43:58 -04:00
var info = new ActionInfo
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_LABEL_EDIT"],
Name = "Edit",
2022-04-19 19:43:58 -04:00
Inputs = new List<InputInfo>
{
2022-04-19 19:43:58 -04:00
new()
{
Name = "level",
Label = Localization["WEBFRONT_PROFILE_LEVEL"],
Type = "select",
Values = Enum.GetValues(typeof(EFClient.Permission)).OfType<EFClient.Permission>()
.Where(p => p <= Client.Level)
.Where(p => p != EFClient.Permission.Banned)
.Where(p => p != EFClient.Permission.Flagged)
.ToDictionary(p => p.ToString(), p => p.ToLocalizedLevelName())
},
},
2019-08-04 18:06:07 -04:00
Action = "EditAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
public async Task<IActionResult> EditAsync(int targetId, string level)
{
var server = Manager.GetServers().First();
2022-04-19 19:43:58 -04:00
return await Task.FromResult(RedirectToAction("Execute", "Console", new
{
serverId = server.EndPoint,
command = $"{_appConfig.CommandPrefix}{_setLevelCommandName} @{targetId} {level}"
}));
}
public IActionResult GenerateLoginTokenForm()
{
2022-04-19 19:43:58 -04:00
var info = new ActionInfo
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_LABEL_GENERATE_TOKEN"],
Name = "GenerateLoginToken",
Action = "GenerateLoginTokenAsync",
Inputs = new List<InputInfo>()
};
return View("_ActionForm", info);
}
[Authorize]
public string GenerateLoginTokenAsync()
{
var state = Manager.TokenAuthenticator.GenerateNextToken(Client.NetworkId);
return string.Format(Utilities.CurrentLocalization.LocalizationIndex["COMMANDS_GENERATETOKEN_SUCCESS"],
state.Token,
$"{state.RemainingTime} {Utilities.CurrentLocalization.LocalizationIndex["GLOBAL_MINUTES"]}",
Client.ClientId);
}
public IActionResult ChatForm(long id)
{
2022-04-19 19:43:58 -04:00
var info = new ActionInfo
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_LABEL_SUBMIT_MESSAGE"],
Name = "Chat",
Inputs = new List<InputInfo>
{
2022-04-19 19:43:58 -04:00
new()
{
Name = "message",
Type = "text",
Label = Localization["WEBFRONT_ACTION_LABEL_MESSAGE"]
},
2022-04-19 19:43:58 -04:00
new()
{
Name = "id",
Value = id.ToString(),
Type = "hidden"
}
},
Action = "ChatAsync"
};
return View("_ActionForm", info);
}
public async Task<IActionResult> ChatAsync(long id, string message)
{
2022-04-19 19:43:58 -04:00
var server = Manager.GetServers().First(server => server.EndPoint == id);
server.ChatHistory.Add(new SharedLibraryCore.Dtos.ChatInfo()
{
ClientId = Client.ClientId,
Message = message,
Name = Client.Name,
ServerGame = server.GameName,
Time = DateTime.Now
});
2022-04-19 19:43:58 -04:00
return await Task.FromResult(RedirectToAction("Execute", "Console", new
{
serverId = server.EndPoint,
command = $"{_appConfig.CommandPrefix}{_sayCommandName} {message}"
}));
}
public async Task<IActionResult> RecentClientsForm()
{
var clients = await Manager.GetClientService().GetRecentClients();
foreach (var client in clients)
{
client.IPAddress =
2022-04-19 19:43:58 -04:00
_appConfig.HasPermission(Client.Level, WebfrontEntity.ClientIPAddress, WebfrontPermission.Read)
? client.IPAddress
: null;
}
return View("~/Views/Shared/Components/Client/_RecentClients.cshtml", clients);
}
2019-08-04 18:06:07 -04:00
public IActionResult FlagForm()
{
2022-04-19 19:43:58 -04:00
var info = new ActionInfo
2019-08-04 18:06:07 -04:00
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_FLAG_NAME"],
Name = "Flag",
2022-04-19 19:43:58 -04:00
Inputs = new List<InputInfo>
2019-08-04 18:06:07 -04:00
{
2022-04-19 19:43:58 -04:00
new()
2019-08-04 18:06:07 -04:00
{
Name = "reason",
Label = Localization["WEBFRONT_ACTION_LABEL_REASON"],
},
2022-04-19 19:43:58 -04:00
new()
{
Name = "PresetReason",
Type = "select",
Label = Localization["WEBFRONT_ACTION_LABEL_PRESET_REASON"],
Values = GetPresetPenaltyReasons()
},
2019-08-04 18:06:07 -04:00
},
Action = "FlagAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
public async Task<IActionResult> FlagAsync(int targetId, string reason, string presetReason = null)
2019-08-04 18:06:07 -04:00
{
var server = Manager.GetServers().First();
2022-04-19 19:43:58 -04:00
return await Task.FromResult(RedirectToAction("Execute", "Console", new
2019-08-04 18:06:07 -04:00
{
serverId = server.EndPoint,
command = $"{_appConfig.CommandPrefix}{_flagCommandName} @{targetId} {presetReason ?? reason}"
2019-08-04 18:06:07 -04:00
}));
}
public IActionResult UnflagForm()
{
2022-04-19 19:43:58 -04:00
var info = new ActionInfo
2019-08-04 18:06:07 -04:00
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_UNFLAG_NAME"],
Name = "Unflag",
2022-04-19 19:43:58 -04:00
Inputs = new List<InputInfo>
2019-08-04 18:06:07 -04:00
{
2022-04-19 19:43:58 -04:00
new()
2019-08-04 18:06:07 -04:00
{
Name = "reason",
Label = Localization["WEBFRONT_ACTION_LABEL_REASON"],
2019-08-04 18:06:07 -04:00
}
},
Action = "UnflagAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
public async Task<IActionResult> UnflagAsync(int targetId, string reason)
{
var server = Manager.GetServers().First();
2022-04-19 19:43:58 -04:00
return await Task.FromResult(RedirectToAction("Execute", "Console", new
2019-08-04 18:06:07 -04:00
{
serverId = server.EndPoint,
command = $"{_appConfig.CommandPrefix}{_unflagCommandName} @{targetId} {reason}"
2019-08-04 18:06:07 -04:00
}));
}
public IActionResult KickForm(int id)
{
2022-04-19 19:43:58 -04:00
var info = new ActionInfo
{
ActionButtonLabel = Localization["WEBFRONT_ACTION_KICK_NAME"],
Name = "Kick",
2022-04-19 19:43:58 -04:00
Inputs = new List<InputInfo>
{
2022-04-19 19:43:58 -04:00
new()
{
Name = "reason",
Label = Localization["WEBFRONT_ACTION_LABEL_REASON"],
},
2022-04-19 19:43:58 -04:00
new()
{
Name = "PresetReason",
Type = "select",
Label = Localization["WEBFRONT_ACTION_LABEL_PRESET_REASON"],
Values = GetPresetPenaltyReasons()
},
2022-04-19 19:43:58 -04:00
new()
{
Name = "targetId",
Type = "hidden",
Value = id.ToString()
}
},
Action = "KickAsync",
ShouldRefresh = true
};
return View("_ActionForm", info);
}
public async Task<IActionResult> KickAsync(int targetId, string reason, string presetReason = null)
{
2022-04-19 19:43:58 -04:00
var client = Manager.GetActiveClients().FirstOrDefault(client => client.ClientId == targetId);
if (client == null)
{
return BadRequest(Localization["WEBFRONT_ACTION_KICK_DISCONNECT"]);
}
2022-04-19 19:43:58 -04:00
return await Task.FromResult(RedirectToAction("Execute", "Console", new
{
serverId = client.CurrentServer.EndPoint,
command = $"{_appConfig.CommandPrefix}{_kickCommandName} {client.ClientNumber} {presetReason ?? reason}"
}));
}
private Dictionary<string, string> GetPresetPenaltyReasons() => _appConfig.PresetPenaltyReasons.Values
.Concat(_appConfig.GlobalRules)
2022-04-19 19:43:58 -04:00
.Concat(_appConfig.Servers.SelectMany(server => server.Rules ?? Array.Empty<string>()))
.Distinct()
2022-04-19 19:43:58 -04:00
.Select((value, _) => new
{
Value = value
})
// this is used for the default empty optional value
.Prepend(new
{
Value = ""
})
.ToDictionary(item => item.Value, item => item.Value);
}
}