[244] | 1 | using System.Collections.Generic;
|
---|
| 2 | using AllocsFixes.NetConnections.Servers.Web;
|
---|
| 3 |
|
---|
[325] | 4 | namespace AllocsFixes.CustomCommands {
|
---|
[244] | 5 | public class WebPermissionsCmd : ConsoleCmdAbstract {
|
---|
| 6 | public override string[] GetCommands () {
|
---|
[325] | 7 | return new[] {"webpermission"};
|
---|
[244] | 8 | }
|
---|
[325] | 9 |
|
---|
| 10 | public override string GetDescription () {
|
---|
[244] | 11 | return "Manage web permission levels";
|
---|
| 12 | }
|
---|
[325] | 13 |
|
---|
[244] | 14 | public override string GetHelp () {
|
---|
| 15 | return "Set/get permission levels required to access a given web functionality. Default\n" +
|
---|
[325] | 16 | "level required for functions that are not explicitly specified is 0.\n" +
|
---|
| 17 | "Usage:\n" +
|
---|
| 18 | " webpermission add <webfunction> <level>\n" +
|
---|
| 19 | " webpermission remove <webfunction>\n" +
|
---|
| 20 | " webpermission list";
|
---|
[244] | 21 | }
|
---|
[325] | 22 |
|
---|
[244] | 23 | public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
|
---|
| 24 | if (_params.Count >= 1) {
|
---|
[326] | 25 | if (_params [0].EqualsCaseInsensitive ("add")) {
|
---|
| 26 | ExecuteAdd (_params);
|
---|
| 27 | } else if (_params [0].EqualsCaseInsensitive ("remove")) {
|
---|
| 28 | ExecuteRemove (_params);
|
---|
| 29 | } else if (_params [0].EqualsCaseInsensitive ("list")) {
|
---|
| 30 | ExecuteList ();
|
---|
| 31 | } else {
|
---|
| 32 | SdtdConsole.Instance.Output ("Invalid sub command \"" + _params [0] + "\".");
|
---|
[244] | 33 | }
|
---|
| 34 | } else {
|
---|
| 35 | SdtdConsole.Instance.Output ("No sub command given.");
|
---|
| 36 | }
|
---|
| 37 | }
|
---|
[325] | 38 |
|
---|
[244] | 39 | private void ExecuteAdd (List<string> _params) {
|
---|
| 40 | if (_params.Count != 3) {
|
---|
| 41 | SdtdConsole.Instance.Output ("Wrong number of arguments, expected 3, found " + _params.Count + ".");
|
---|
| 42 | return;
|
---|
| 43 | }
|
---|
[325] | 44 |
|
---|
[244] | 45 | if (!WebPermissions.Instance.IsKnownModule (_params [1])) {
|
---|
| 46 | SdtdConsole.Instance.Output ("\"" + _params [1] + "\" is not a valid web function.");
|
---|
| 47 | return;
|
---|
| 48 | }
|
---|
[325] | 49 |
|
---|
[244] | 50 | int level;
|
---|
| 51 | if (!int.TryParse (_params [2], out level)) {
|
---|
| 52 | SdtdConsole.Instance.Output ("\"" + _params [2] + "\" is not a valid integer.");
|
---|
| 53 | return;
|
---|
| 54 | }
|
---|
[325] | 55 |
|
---|
[244] | 56 | WebPermissions.Instance.AddModulePermission (_params [1], level);
|
---|
| 57 | SdtdConsole.Instance.Output (string.Format ("{0} added with permission level of {1}.", _params [1], level));
|
---|
| 58 | }
|
---|
[325] | 59 |
|
---|
[244] | 60 | private void ExecuteRemove (List<string> _params) {
|
---|
| 61 | if (_params.Count != 2) {
|
---|
| 62 | SdtdConsole.Instance.Output ("Wrong number of arguments, expected 2, found " + _params.Count + ".");
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
[325] | 65 |
|
---|
[244] | 66 | if (!WebPermissions.Instance.IsKnownModule (_params [1])) {
|
---|
| 67 | SdtdConsole.Instance.Output ("\"" + _params [1] + "\" is not a valid web function.");
|
---|
| 68 | return;
|
---|
| 69 | }
|
---|
[325] | 70 |
|
---|
[244] | 71 | WebPermissions.Instance.RemoveModulePermission (_params [1]);
|
---|
| 72 | SdtdConsole.Instance.Output (string.Format ("{0} removed from permissions list.", _params [1]));
|
---|
| 73 | }
|
---|
[325] | 74 |
|
---|
[244] | 75 | private void ExecuteList () {
|
---|
| 76 | SdtdConsole.Instance.Output ("Defined web function permissions:");
|
---|
| 77 | SdtdConsole.Instance.Output (" Level: Web function");
|
---|
| 78 | foreach (WebPermissions.WebModulePermission wmp in WebPermissions.Instance.GetModules ()) {
|
---|
| 79 | SdtdConsole.Instance.Output (string.Format (" {0,5}: {1}", wmp.permissionLevel, wmp.module));
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|