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

Last change on this file since 499 was 487, checked in by alloc, 5 months ago

1.1.0.1 Release for V 1.0

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 public override string[] getCommands () {
9 return new[] {"webpermission"};
10 }
11
12 public override string getDescription () {
13 return "Manage web permission levels";
14 }
15
16 public 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 if (isGlobal) {
100 module = module.SetLevelGlobal (level);
101 } else {
102 module = module.SetLevelForMethod (method, level);
103 }
104
105 AdminWebModules.Instance.AddModule (module);
106
107 SdtdConsole.Instance.Output (
108 $"{moduleString}, method {methodString} added {(isInherit ? ", inheriting the APIs global permission level" : "with permission level " + level)}.");
109 }
110
111 private void ExecuteRemove (List<string> _params) {
112 if (_params.Count != 2) {
113 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 2, found {_params.Count}.");
114 return;
115 }
116
117 if (!AdminWebModules.Instance.IsKnownModule (_params [1])) {
118 SdtdConsole.Instance.Output ($"\"{_params [1]}\" is not a valid web function.");
119 return;
120 }
121
122 AdminWebModules.Instance.RemoveModule (_params [1]);
123 SdtdConsole.Instance.Output ($"{_params [1]} removed from permissions list.");
124 }
125
126 private void ExecuteList (List<string> _params) {
127 bool includeDefaults = _params.Count > 1 && ConsoleHelper.ParseParamBool (_params [1], true);
128
129 SdtdConsole.Instance.Output ("Defined web function permissions:");
130
131 List<AdminWebModules.WebModule> wmps = AdminWebModules.Instance.GetModules ();
132 for (int iModule = 0; iModule < wmps.Count; iModule++) {
133 AdminWebModules.WebModule wmp = wmps [iModule];
134
135 if (!includeDefaults && wmp.IsDefault) {
136 continue;
137 }
138
139 SdtdConsole.Instance.Output ($" {wmp.Name,-25}: {wmp.LevelGlobal,4}{(wmp.IsDefault ? " (default permissions)" : "")}");
140 if (wmp.LevelPerMethod == null) {
141 continue;
142 }
143
144 for (int iMethod = 0; iMethod < wmp.LevelPerMethod.Length; iMethod++) {
145 int methodLevel = wmp.LevelPerMethod [iMethod];
146 ERequestMethod method = (ERequestMethod)iMethod;
147
148 if (methodLevel == AdminWebModules.MethodLevelNotSupported) {
149 continue;
150 }
151
152 if (methodLevel == AdminWebModules.MethodLevelInheritGlobal) {
153 SdtdConsole.Instance.Output ($" {method.ToStringCached (),25}: {wmp.LevelGlobal,4} (Using API level)");
154 } else {
155 SdtdConsole.Instance.Output ($" {method.ToStringCached (),25}: {methodLevel,4}");
156 }
157 }
158 }
159 }
160 }
161}
Note: See TracBrowser for help on using the repository browser.