source: binary-improvements/MapRendering/Commands/WebTokens.cs@ 418

Last change on this file since 418 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

File size: 3.6 KB
Line 
1using System.Collections.Generic;
2using System.Text.RegularExpressions;
3using AllocsFixes.NetConnections.Servers.Web;
4
5namespace AllocsFixes.CustomCommands {
6 public class WebTokens : ConsoleCmdAbstract {
7 private static readonly Regex validNameTokenMatcher = new Regex (@"^\w+$");
8
9 public override string[] GetCommands () {
10 return new[] {"webtokens"};
11 }
12
13 public override string GetDescription () {
14 return "Manage web tokens";
15 }
16
17 public override string GetHelp () {
18 return "Set/get webtoken permission levels. A level of 0 is maximum permission.\n" +
19 "Usage:\n" +
20 " webtokens add <username> <usertoken> <level>\n" +
21 " webtokens remove <username>\n" +
22 " webtokens 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 != 4) {
43 SdtdConsole.Instance.Output ("Wrong number of arguments, expected 4, found " + _params.Count + ".");
44 return;
45 }
46
47 if (string.IsNullOrEmpty (_params [1])) {
48 SdtdConsole.Instance.Output ("Argument 'username' is empty.");
49 return;
50 }
51
52 if (!validNameTokenMatcher.IsMatch (_params [1])) {
53 SdtdConsole.Instance.Output (
54 "Argument 'username' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
55 return;
56 }
57
58 if (string.IsNullOrEmpty (_params [2])) {
59 SdtdConsole.Instance.Output ("Argument 'usertoken' is empty.");
60 return;
61 }
62
63 if (!validNameTokenMatcher.IsMatch (_params [2])) {
64 SdtdConsole.Instance.Output (
65 "Argument 'usertoken' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
66 return;
67 }
68
69 int level;
70 if (!int.TryParse (_params [3], out level)) {
71 SdtdConsole.Instance.Output ("Argument 'level' is not a valid integer.");
72 return;
73 }
74
75 WebPermissions.Instance.AddAdmin (_params [1], _params [2], level);
76 SdtdConsole.Instance.Output (string.Format (
77 "Web user with name={0} and password={1} added with permission level of {2}.", _params [1], _params [2],
78 level));
79 }
80
81 private void ExecuteRemove (List<string> _params) {
82 if (_params.Count != 2) {
83 SdtdConsole.Instance.Output ("Wrong number of arguments, expected 2, found " + _params.Count + ".");
84 return;
85 }
86
87 if (string.IsNullOrEmpty (_params [1])) {
88 SdtdConsole.Instance.Output ("Argument 'username' is empty.");
89 return;
90 }
91
92 if (!validNameTokenMatcher.IsMatch (_params [1])) {
93 SdtdConsole.Instance.Output (
94 "Argument 'username' may only contain characters (A-Z, a-z), digits (0-9) and underscores (_).");
95 return;
96 }
97
98 WebPermissions.Instance.RemoveAdmin (_params [1]);
99 SdtdConsole.Instance.Output (string.Format ("{0} removed from web user permissions list.", _params [1]));
100 }
101
102 private void ExecuteList () {
103 SdtdConsole.Instance.Output ("Defined webuser permissions:");
104 SdtdConsole.Instance.Output (" Level: Name / Token");
105 foreach (WebPermissions.AdminToken at in WebPermissions.Instance.GetAdmins ()) {
106 SdtdConsole.Instance.Output (
107 string.Format (" {0,5}: {1} / {2}", at.permissionLevel, at.name, at.token));
108 }
109 }
110 }
111}
Note: See TracBrowser for help on using the repository browser.