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

Last change on this file since 396 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 2.8 KB
RevLine 
[391]1using System.Collections.Generic;
2using JetBrains.Annotations;
3
4namespace Webserver.Commands {
5 [UsedImplicitly]
6 public class WebPermissionsCmd : ConsoleCmdAbstract {
7 public override string[] GetCommands () {
8 return new[] {"webpermission"};
9 }
10
11 public override string GetDescription () {
12 return "Manage web permission levels";
13 }
14
15 public override string GetHelp () {
16 return "Set/get permission levels required to access a given web functionality. Default\n" +
17 "level required for functions that are not explicitly specified is 0.\n" +
18 "Usage:\n" +
19 " webpermission add <webfunction> <level>\n" +
20 " webpermission remove <webfunction>\n" +
21 " webpermission list";
22 }
23
24 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
25 if (_params.Count >= 1) {
26 if (_params [0].EqualsCaseInsensitive ("add")) {
27 ExecuteAdd (_params);
28 } else if (_params [0].EqualsCaseInsensitive ("remove")) {
29 ExecuteRemove (_params);
30 } else if (_params [0].EqualsCaseInsensitive ("list")) {
31 ExecuteList ();
32 } else {
33 SdtdConsole.Instance.Output ($"Invalid sub command \"{_params [0]}\".");
34 }
35 } else {
36 SdtdConsole.Instance.Output ("No sub command given.");
37 }
38 }
39
40 private void ExecuteAdd (List<string> _params) {
41 if (_params.Count != 3) {
42 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 3, found {_params.Count}.");
43 return;
44 }
45
46 if (!WebPermissions.Instance.IsKnownModule (_params [1])) {
47 SdtdConsole.Instance.Output ($"\"{_params [1]}\" is not a valid web function.");
48 return;
49 }
50
51 if (!int.TryParse (_params [2], out int 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 ($"{_params [1]} added with permission level of {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 ($"{_params [1]} removed from permissions list.");
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 ($" {wmp.permissionLevel,5}: {wmp.module}");
80 }
81 }
82 }
83}
Note: See TracBrowser for help on using the repository browser.