source: binary-improvements2/WebServer/src/Commands/WebPermissionsCmd.cs@ 425

Last change on this file since 425 was 419, checked in by alloc, 21 months ago

Updated WebPermissions command to only list manually defined levels by default

File size: 3.4 KB
Line 
1using System.Collections.Generic;
2using JetBrains.Annotations;
3using Webserver.Permissions;
4
5namespace Webserver.Commands {
6 [UsedImplicitly]
7 public class WebPermissionsCmd : ConsoleCmdAbstract {
8 protected override string[] getCommands () {
9 return new[] {"webpermission"};
10 }
11
12 protected override string getDescription () {
13 return "Manage web permission levels";
14 }
15
16 protected override string getHelp () {
17 return "Set/get permission levels required to access a given web functionality. Default\n" +
18 "level required for functions that are not explicitly specified is 0.\n" +
19 "Usage:\n" +
20 " webpermission add <webfunction> <level>\n" +
21 " webpermission remove <webfunction>\n" +
22 " webpermission list [includedefaults]";
23 }
24
25 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
26 if (_params.Count >= 1) {
27 if (_params [0].EqualsCaseInsensitive ("add")) {
28 ExecuteAdd (_params);
29 } else if (_params [0].EqualsCaseInsensitive ("remove")) {
30 ExecuteRemove (_params);
31 } else if (_params [0].EqualsCaseInsensitive ("list")) {
32 ExecuteList (_params);
33 } else {
34 SdtdConsole.Instance.Output ($"Invalid sub command \"{_params [0]}\".");
35 }
36 } else {
37 SdtdConsole.Instance.Output ("No sub command given.");
38 }
39 }
40
41 private void ExecuteAdd (List<string> _params) {
42 if (_params.Count != 3) {
43 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 3, found {_params.Count}.");
44 return;
45 }
46
47 if (!AdminWebModules.Instance.IsKnownModule (_params [1])) {
48 SdtdConsole.Instance.Output ($"\"{_params [1]}\" is not a valid web function.");
49 return;
50 }
51
52 if (!int.TryParse (_params [2], out int level)) {
53 SdtdConsole.Instance.Output ($"\"{_params [2]}\" is not a valid integer.");
54 return;
55 }
56
57 AdminWebModules.Instance.AddModule (_params [1], level);
58 SdtdConsole.Instance.Output ($"{_params [1]} added with permission level of {level}.");
59 }
60
61 private void ExecuteRemove (List<string> _params) {
62 if (_params.Count != 2) {
63 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 2, found {_params.Count}.");
64 return;
65 }
66
67 if (!AdminWebModules.Instance.IsKnownModule (_params [1])) {
68 SdtdConsole.Instance.Output ($"\"{_params [1]}\" is not a valid web function.");
69 return;
70 }
71
72 AdminWebModules.Instance.RemoveModule (_params [1]);
73 SdtdConsole.Instance.Output ($"{_params [1]} removed from permissions list.");
74 }
75
76 private void ExecuteList (List<string> _params) {
77 bool includeDefaults = _params.Count > 1 && ConsoleHelper.ParseParamBool (_params [1], true);
78
79 SdtdConsole.Instance.Output ("Defined web function permissions:");
80 SdtdConsole.Instance.Output (" Level: Web function");
81
82 List<AdminWebModules.WebModule> wmps = AdminWebModules.Instance.GetModules ();
83 for (int i = 0; i < wmps.Count; i++) {
84 AdminWebModules.WebModule wmp = wmps [i];
85
86 if (!includeDefaults && wmp.IsDefault) {
87 continue;
88 }
89
90 if (wmp.IsDefault) {
91 if (wmp.PermissionLevel == int.MinValue) {
92 SdtdConsole.Instance.Output ($" - : {wmp.Name} (default permission)");
93 } else {
94 SdtdConsole.Instance.Output ($" {wmp.PermissionLevel,5}: {wmp.Name} (default permission)");
95 }
96 } else {
97 SdtdConsole.Instance.Output ($" {wmp.PermissionLevel,5}: {wmp.Name}");
98 }
99 }
100 }
101 }
102}
Note: See TracBrowser for help on using the repository browser.