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

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

1.1.0.1 Release for V 1.0

File size: 5.8 KB
RevLine 
[391]1using System.Collections.Generic;
2using JetBrains.Annotations;
[404]3using Webserver.Permissions;
[391]4
5namespace Webserver.Commands {
6 [UsedImplicitly]
7 public class WebPermissionsCmd : ConsoleCmdAbstract {
[487]8 public override string[] getCommands () {
[391]9 return new[] {"webpermission"};
10 }
11
[487]12 public override string getDescription () {
[391]13 return "Manage web permission levels";
14 }
15
[487]16 public override string getHelp () {
[426]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 ();
[391]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")) {
[419]39 ExecuteList (_params);
[391]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) {
[426]49 if (_params.Count != 4) {
50 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 4, found {_params.Count}.");
[391]51 return;
52 }
53
[426]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.");
[391]65 return;
66 }
67
[426]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 }
[391]82 }
83
[435]84 if (permissionLevelString.EqualsCaseInsensitive (AdminWebModules.MethodLevelInheritKeyword)) {
[426]85 if (isGlobal) {
[487]86 SdtdConsole.Instance.Output ("Permission level can not use the 'inherit' keyword with the 'global' method keyword.");
[426]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) {
[487]100 module = module.SetLevelGlobal (level);
[426]101 } else {
[487]102 module = module.SetLevelForMethod (method, level);
[426]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)}.");
[391]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
[404]117 if (!AdminWebModules.Instance.IsKnownModule (_params [1])) {
[391]118 SdtdConsole.Instance.Output ($"\"{_params [1]}\" is not a valid web function.");
119 return;
120 }
121
[404]122 AdminWebModules.Instance.RemoveModule (_params [1]);
[391]123 SdtdConsole.Instance.Output ($"{_params [1]} removed from permissions list.");
124 }
125
[419]126 private void ExecuteList (List<string> _params) {
127 bool includeDefaults = _params.Count > 1 && ConsoleHelper.ParseParamBool (_params [1], true);
128
[391]129 SdtdConsole.Instance.Output ("Defined web function permissions:");
[404]130
131 List<AdminWebModules.WebModule> wmps = AdminWebModules.Instance.GetModules ();
[426]132 for (int iModule = 0; iModule < wmps.Count; iModule++) {
133 AdminWebModules.WebModule wmp = wmps [iModule];
[419]134
135 if (!includeDefaults && wmp.IsDefault) {
136 continue;
137 }
138
[426]139 SdtdConsole.Instance.Output ($" {wmp.Name,-25}: {wmp.LevelGlobal,4}{(wmp.IsDefault ? " (default permissions)" : "")}");
[434]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;
[426]147
[434]148 if (methodLevel == AdminWebModules.MethodLevelNotSupported) {
149 continue;
150 }
[426]151
[434]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}");
[419]156 }
157 }
[391]158 }
159 }
160 }
161}
Note: See TracBrowser for help on using the repository browser.