[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) {
|
---|
| 25 | switch (_params [0].ToLower ()) {
|
---|
| 26 | case "add":
|
---|
| 27 | ExecuteAdd (_params);
|
---|
| 28 | break;
|
---|
| 29 | case "remove":
|
---|
| 30 | ExecuteRemove (_params);
|
---|
| 31 | break;
|
---|
| 32 | case "list":
|
---|
| 33 | ExecuteList ();
|
---|
| 34 | break;
|
---|
| 35 | default:
|
---|
| 36 | SdtdConsole.Instance.Output ("Invalid sub command \"" + _params [0] + "\".");
|
---|
| 37 | return;
|
---|
| 38 | }
|
---|
| 39 | } else {
|
---|
| 40 | SdtdConsole.Instance.Output ("No sub command given.");
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
[325] | 43 |
|
---|
[244] | 44 | private void ExecuteAdd (List<string> _params) {
|
---|
| 45 | if (_params.Count != 3) {
|
---|
| 46 | SdtdConsole.Instance.Output ("Wrong number of arguments, expected 3, found " + _params.Count + ".");
|
---|
| 47 | return;
|
---|
| 48 | }
|
---|
[325] | 49 |
|
---|
[244] | 50 | if (!WebPermissions.Instance.IsKnownModule (_params [1])) {
|
---|
| 51 | SdtdConsole.Instance.Output ("\"" + _params [1] + "\" is not a valid web function.");
|
---|
| 52 | return;
|
---|
| 53 | }
|
---|
[325] | 54 |
|
---|
[244] | 55 | int level;
|
---|
| 56 | if (!int.TryParse (_params [2], out level)) {
|
---|
| 57 | SdtdConsole.Instance.Output ("\"" + _params [2] + "\" is not a valid integer.");
|
---|
| 58 | return;
|
---|
| 59 | }
|
---|
[325] | 60 |
|
---|
[244] | 61 | WebPermissions.Instance.AddModulePermission (_params [1], level);
|
---|
| 62 | SdtdConsole.Instance.Output (string.Format ("{0} added with permission level of {1}.", _params [1], level));
|
---|
| 63 | }
|
---|
[325] | 64 |
|
---|
[244] | 65 | private void ExecuteRemove (List<string> _params) {
|
---|
| 66 | if (_params.Count != 2) {
|
---|
| 67 | SdtdConsole.Instance.Output ("Wrong number of arguments, expected 2, found " + _params.Count + ".");
|
---|
| 68 | return;
|
---|
| 69 | }
|
---|
[325] | 70 |
|
---|
[244] | 71 | if (!WebPermissions.Instance.IsKnownModule (_params [1])) {
|
---|
| 72 | SdtdConsole.Instance.Output ("\"" + _params [1] + "\" is not a valid web function.");
|
---|
| 73 | return;
|
---|
| 74 | }
|
---|
[325] | 75 |
|
---|
[244] | 76 | WebPermissions.Instance.RemoveModulePermission (_params [1]);
|
---|
| 77 | SdtdConsole.Instance.Output (string.Format ("{0} removed from permissions list.", _params [1]));
|
---|
| 78 | }
|
---|
[325] | 79 |
|
---|
[244] | 80 | private void ExecuteList () {
|
---|
| 81 | SdtdConsole.Instance.Output ("Defined web function permissions:");
|
---|
| 82 | SdtdConsole.Instance.Output (" Level: Web function");
|
---|
| 83 | foreach (WebPermissions.WebModulePermission wmp in WebPermissions.Instance.GetModules ()) {
|
---|
| 84 | SdtdConsole.Instance.Output (string.Format (" {0,5}: {1}", wmp.permissionLevel, wmp.module));
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|