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

Last change on this file since 296 was 244, checked in by alloc, 9 years ago

Fixes intermediate state

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