source: binary-improvements/MapRendering/Commands/WebPermissionsCmd.cs@ 343

Last change on this file since 343 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

File size: 2.9 KB
Line 
1using System.Collections.Generic;
2using AllocsFixes.NetConnections.Servers.Web;
3
4namespace AllocsFixes.CustomCommands {
5 public class WebPermissionsCmd : ConsoleCmdAbstract {
6 public override string[] GetCommands () {
7 return new[] {"webpermission"};
8 }
9
10 public override string GetDescription () {
11 return "Manage web permission levels";
12 }
13
14 public override string GetHelp () {
15 return "Set/get permission levels required to access a given web functionality. Default\n" +
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";
21 }
22
23 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
24 if (_params.Count >= 1) {
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] + "\".");
33 }
34 } else {
35 SdtdConsole.Instance.Output ("No sub command given.");
36 }
37 }
38
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 }
44
45 if (!WebPermissions.Instance.IsKnownModule (_params [1])) {
46 SdtdConsole.Instance.Output ("\"" + _params [1] + "\" is not a valid web function.");
47 return;
48 }
49
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 }
55
56 WebPermissions.Instance.AddModulePermission (_params [1], level);
57 SdtdConsole.Instance.Output (string.Format ("{0} added with permission level of {1}.", _params [1], level));
58 }
59
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 }
65
66 if (!WebPermissions.Instance.IsKnownModule (_params [1])) {
67 SdtdConsole.Instance.Output ("\"" + _params [1] + "\" is not a valid web function.");
68 return;
69 }
70
71 WebPermissions.Instance.RemoveModulePermission (_params [1]);
72 SdtdConsole.Instance.Output (string.Format ("{0} removed from permissions list.", _params [1]));
73 }
74
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}
Note: See TracBrowser for help on using the repository browser.