source: TFP-WebServer/WebServer/src/Commands/WebPermissionsCmd.cs@ 441

Last change on this file since 441 was 435, checked in by alloc, 18 months ago

Added WebModules API POST support

File size: 5.8 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 @"
18 |Set/get permission levels required to access a given web functionality. Default
19 |level required for functions that are not explicitly specified is 0.
20 |Usage:
21 | 1. webpermission add <webfunction> <method> <level>
22 | 2. webpermission remove <webfunction>
23 | 3. webpermission list [includedefaults]
24 |1. Add a new override (or replace the existing one) for the given function. Method must be a HTTP method (like 'GET', 'POST')
25 supported by the function or the keyword 'global' for a per-API permission level. Use the permission level keyword
26 'inherit' to use the per-API permission level for the specified method instead of a custom one for just the single method.
27 |2. Removes any custom overrides for the specified function.
28 |3. List all permissions. Pass in 'true' for the includedefaults argument to also show functions that do not have a custom override defined.
29 ".Unindent ();
30 }
31
32 public override void Execute (List<string> _params, CommandSenderInfo _senderInfo) {
33 if (_params.Count >= 1) {
34 if (_params [0].EqualsCaseInsensitive ("add")) {
35 ExecuteAdd (_params);
36 } else if (_params [0].EqualsCaseInsensitive ("remove")) {
37 ExecuteRemove (_params);
38 } else if (_params [0].EqualsCaseInsensitive ("list")) {
39 ExecuteList (_params);
40 } else {
41 SdtdConsole.Instance.Output ($"Invalid sub command \"{_params [0]}\".");
42 }
43 } else {
44 SdtdConsole.Instance.Output ("No sub command given.");
45 }
46 }
47
48 private void ExecuteAdd (List<string> _params) {
49 if (_params.Count != 4) {
50 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 4, found {_params.Count}.");
51 return;
52 }
53
54 string moduleString = _params [1];
55 string methodString = _params [2];
56 string permissionLevelString = _params [3];
57
58 ERequestMethod method = ERequestMethod.Count;
59 bool isGlobal = false;
60 bool isInherit = false;
61 int level;
62
63 if (!AdminWebModules.Instance.IsKnownModule (moduleString)) {
64 SdtdConsole.Instance.Output ($"\"{moduleString}\" is not a valid web function.");
65 return;
66 }
67
68 AdminWebModules.WebModule module = AdminWebModules.Instance.GetModule (moduleString);
69
70 if (methodString.EqualsCaseInsensitive ("global")) {
71 isGlobal = true;
72 } else {
73 if (!EnumUtils.TryParse (methodString, out method, true)) {
74 SdtdConsole.Instance.Output ($"\"{methodString}\" is neither a valid HTTP method nor the 'global' keyword.");
75 return;
76 }
77
78 if (module.LevelPerMethod == null || module.LevelPerMethod [(int)method] == AdminWebModules.MethodLevelNotSupported) {
79 SdtdConsole.Instance.Output ($"\"{methodString}\" is not a method supported by the \"{moduleString}\" function.");
80 return;
81 }
82 }
83
84 if (permissionLevelString.EqualsCaseInsensitive (AdminWebModules.MethodLevelInheritKeyword)) {
85 if (isGlobal) {
86 SdtdConsole.Instance.Output ($"Permission level can not use the 'inherit' keyword with the 'global' method keyword.");
87 return;
88 }
89
90 isInherit = true;
91 level = AdminWebModules.MethodLevelInheritGlobal;
92 } else {
93 if (!int.TryParse (permissionLevelString, out level)) {
94 SdtdConsole.Instance.Output ($"\"{permissionLevelString}\" is neither a valid integer nor the 'inherit' keyword.");
95 return;
96 }
97 }
98
99 module.IsDefault = false;
100 if (isGlobal) {
101 module.LevelGlobal = level;
102 } else {
103 module.LevelPerMethod [(int)method] = level;
104 }
105
106 AdminWebModules.Instance.AddModule (module);
107
108 SdtdConsole.Instance.Output (
109 $"{moduleString}, method {methodString} added {(isInherit ? ", inheriting the APIs global permission level" : "with permission level " + level)}.");
110 }
111
112 private void ExecuteRemove (List<string> _params) {
113 if (_params.Count != 2) {
114 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 2, found {_params.Count}.");
115 return;
116 }
117
118 if (!AdminWebModules.Instance.IsKnownModule (_params [1])) {
119 SdtdConsole.Instance.Output ($"\"{_params [1]}\" is not a valid web function.");
120 return;
121 }
122
123 AdminWebModules.Instance.RemoveModule (_params [1]);
124 SdtdConsole.Instance.Output ($"{_params [1]} removed from permissions list.");
125 }
126
127 private void ExecuteList (List<string> _params) {
128 bool includeDefaults = _params.Count > 1 && ConsoleHelper.ParseParamBool (_params [1], true);
129
130 SdtdConsole.Instance.Output ("Defined web function permissions:");
131
132 List<AdminWebModules.WebModule> wmps = AdminWebModules.Instance.GetModules ();
133 for (int iModule = 0; iModule < wmps.Count; iModule++) {
134 AdminWebModules.WebModule wmp = wmps [iModule];
135
136 if (!includeDefaults && wmp.IsDefault) {
137 continue;
138 }
139
140 SdtdConsole.Instance.Output ($" {wmp.Name,-25}: {wmp.LevelGlobal,4}{(wmp.IsDefault ? " (default permissions)" : "")}");
141 if (wmp.LevelPerMethod == null) {
142 continue;
143 }
144
145 for (int iMethod = 0; iMethod < wmp.LevelPerMethod.Length; iMethod++) {
146 int methodLevel = wmp.LevelPerMethod [iMethod];
147 ERequestMethod method = (ERequestMethod)iMethod;
148
149 if (methodLevel == AdminWebModules.MethodLevelNotSupported) {
150 continue;
151 }
152
153 if (methodLevel == AdminWebModules.MethodLevelInheritGlobal) {
154 SdtdConsole.Instance.Output ($" {method.ToStringCached (),25}: {wmp.LevelGlobal,4} (Using API level)");
155 } else {
156 SdtdConsole.Instance.Output ($" {method.ToStringCached (),25}: {methodLevel,4}");
157 }
158 }
159 }
160 }
161 }
162}
Note: See TracBrowser for help on using the repository browser.