Changeset 426 for binary-improvements2/WebServer/src/Commands
- Timestamp:
- Apr 24, 2023, 2:40:34 PM (21 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/Commands/WebPermissionsCmd.cs
r419 r426 15 15 16 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 [includedefaults]"; 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 (); 23 30 } 24 31 … … 40 47 41 48 private void ExecuteAdd (List<string> _params) { 42 if (_params.Count != 3) {43 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 3, found {_params.Count}.");49 if (_params.Count != 4) { 50 SdtdConsole.Instance.Output ($"Wrong number of arguments, expected 4, found {_params.Count}."); 44 51 return; 45 52 } 46 53 47 if (!AdminWebModules.Instance.IsKnownModule (_params [1])) { 48 SdtdConsole.Instance.Output ($"\"{_params [1]}\" is not a valid web function."); 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."); 49 65 return; 50 66 } 51 67 52 if (!int.TryParse (_params [2], out int level)) { 53 SdtdConsole.Instance.Output ($"\"{_params [2]}\" is not a valid integer."); 54 return; 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 } 55 82 } 56 83 57 AdminWebModules.Instance.AddModule (_params [1], level); 58 SdtdConsole.Instance.Output ($"{_params [1]} added with permission level of {level}."); 84 if (permissionLevelString.EqualsCaseInsensitive ("inherit")) { 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)}."); 59 110 } 60 111 … … 78 129 79 130 SdtdConsole.Instance.Output ("Defined web function permissions:"); 80 SdtdConsole.Instance.Output (" Level: Web function");81 131 82 132 List<AdminWebModules.WebModule> wmps = AdminWebModules.Instance.GetModules (); 83 for (int i = 0; i < wmps.Count; i++) {84 AdminWebModules.WebModule wmp = wmps [i ];133 for (int iModule = 0; iModule < wmps.Count; iModule++) { 134 AdminWebModules.WebModule wmp = wmps [iModule]; 85 135 86 136 if (!includeDefaults && wmp.IsDefault) { … … 88 138 } 89 139 90 if (wmp.IsDefault) { 91 if (wmp.PermissionLevel == int.MinValue) { 92 SdtdConsole.Instance.Output ($" - : {wmp.Name} (default permission)"); 93 } else { 94 SdtdConsole.Instance.Output ($" {wmp.PermissionLevel,5}: {wmp.Name} (default permission)"); 140 SdtdConsole.Instance.Output ($" {wmp.Name,-25}: {wmp.LevelGlobal,4}{(wmp.IsDefault ? " (default permissions)" : "")}"); 141 if (wmp.LevelPerMethod != null) { 142 for (int iMethod = 0; iMethod < wmp.LevelPerMethod.Length; iMethod++) { 143 int methodLevel = wmp.LevelPerMethod [iMethod]; 144 ERequestMethod method = (ERequestMethod)iMethod; 145 146 if (methodLevel == AdminWebModules.MethodLevelNotSupported) { 147 continue; 148 } 149 150 if (methodLevel == AdminWebModules.MethodLevelInheritGlobal) { 151 SdtdConsole.Instance.Output ($" {method.ToStringCached (),25}: {wmp.LevelGlobal,4} (Using API level)"); 152 } else { 153 SdtdConsole.Instance.Output ($" {method.ToStringCached (),25}: {methodLevel,4}"); 154 } 95 155 } 96 } else {97 SdtdConsole.Instance.Output ($" {wmp.PermissionLevel,5}: {wmp.Name}");98 156 } 99 157 }
Note:
See TracChangeset
for help on using the changeset viewer.