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

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

Code style cleanup (mostly whitespace changes, enforcing braces, using cleanup)

File size: 2.8 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 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 }
43
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 }
49
50 if (!WebPermissions.Instance.IsKnownModule (_params [1])) {
51 SdtdConsole.Instance.Output ("\"" + _params [1] + "\" is not a valid web function.");
52 return;
53 }
54
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 }
60
61 WebPermissions.Instance.AddModulePermission (_params [1], level);
62 SdtdConsole.Instance.Output (string.Format ("{0} added with permission level of {1}.", _params [1], level));
63 }
64
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 }
70
71 if (!WebPermissions.Instance.IsKnownModule (_params [1])) {
72 SdtdConsole.Instance.Output ("\"" + _params [1] + "\" is not a valid web function.");
73 return;
74 }
75
76 WebPermissions.Instance.RemoveModulePermission (_params [1]);
77 SdtdConsole.Instance.Output (string.Format ("{0} removed from permissions list.", _params [1]));
78 }
79
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}
Note: See TracBrowser for help on using the repository browser.