using System.Collections.Generic; using JetBrains.Annotations; using Webserver.Permissions; namespace Webserver.Commands { [UsedImplicitly] public class WebPermissionsCmd : ConsoleCmdAbstract { protected override string[] getCommands () { return new[] {"webpermission"}; } protected override string getDescription () { return "Manage web permission levels"; } protected override string getHelp () { return "Set/get permission levels required to access a given web functionality. Default\n" + "level required for functions that are not explicitly specified is 0.\n" + "Usage:\n" + " webpermission add \n" + " webpermission remove \n" + " webpermission list [includedefaults]"; } public override void Execute (List _params, CommandSenderInfo _senderInfo) { if (_params.Count >= 1) { if (_params [0].EqualsCaseInsensitive ("add")) { ExecuteAdd (_params); } else if (_params [0].EqualsCaseInsensitive ("remove")) { ExecuteRemove (_params); } else if (_params [0].EqualsCaseInsensitive ("list")) { ExecuteList (_params); } else { SdtdConsole.Instance.Output ($"Invalid sub command \"{_params [0]}\"."); } } else { SdtdConsole.Instance.Output ("No sub command given."); } } private void ExecuteAdd (List _params) { if (_params.Count != 3) { SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 3, found {_params.Count}."); return; } if (!AdminWebModules.Instance.IsKnownModule (_params [1])) { SdtdConsole.Instance.Output ($"\"{_params [1]}\" is not a valid web function."); return; } if (!int.TryParse (_params [2], out int level)) { SdtdConsole.Instance.Output ($"\"{_params [2]}\" is not a valid integer."); return; } AdminWebModules.Instance.AddModule (_params [1], level); SdtdConsole.Instance.Output ($"{_params [1]} added with permission level of {level}."); } private void ExecuteRemove (List _params) { if (_params.Count != 2) { SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 2, found {_params.Count}."); return; } if (!AdminWebModules.Instance.IsKnownModule (_params [1])) { SdtdConsole.Instance.Output ($"\"{_params [1]}\" is not a valid web function."); return; } AdminWebModules.Instance.RemoveModule (_params [1]); SdtdConsole.Instance.Output ($"{_params [1]} removed from permissions list."); } private void ExecuteList (List _params) { bool includeDefaults = _params.Count > 1 && ConsoleHelper.ParseParamBool (_params [1], true); SdtdConsole.Instance.Output ("Defined web function permissions:"); SdtdConsole.Instance.Output (" Level: Web function"); List wmps = AdminWebModules.Instance.GetModules (); for (int i = 0; i < wmps.Count; i++) { AdminWebModules.WebModule wmp = wmps [i]; if (!includeDefaults && wmp.IsDefault) { continue; } if (wmp.IsDefault) { if (wmp.PermissionLevel == int.MinValue) { SdtdConsole.Instance.Output ($" - : {wmp.Name} (default permission)"); } else { SdtdConsole.Instance.Output ($" {wmp.PermissionLevel,5}: {wmp.Name} (default permission)"); } } else { SdtdConsole.Instance.Output ($" {wmp.PermissionLevel,5}: {wmp.Name}"); } } } } }