1 | using System.Collections.Generic;
|
---|
2 | using JetBrains.Annotations;
|
---|
3 | using Webserver.Permissions;
|
---|
4 |
|
---|
5 | namespace 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";
|
---|
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 ();
|
---|
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 () {
|
---|
77 | SdtdConsole.Instance.Output ("Defined web function permissions:");
|
---|
78 | SdtdConsole.Instance.Output (" Level: Web function");
|
---|
79 |
|
---|
80 | List<AdminWebModules.WebModule> wmps = AdminWebModules.Instance.GetModules ();
|
---|
81 | for (int i = 0; i < wmps.Count; i++) {
|
---|
82 | AdminWebModules.WebModule wmp = wmps [i];
|
---|
83 |
|
---|
84 | SdtdConsole.Instance.Output ($" {wmp.PermissionLevel,5}: {wmp.Name}");
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|