source: binary-improvements2/WebServer/src/WebAPI/APIs/Permissions/WebModules.cs@ 434

Last change on this file since 434 was 434, checked in by alloc, 19 months ago

Added permission management APIs

File size: 4.7 KB
Line 
1using System.Collections.Generic;
2using System.Net;
3using JetBrains.Annotations;
4using Utf8Json;
5using Webserver.Permissions;
6
7namespace Webserver.WebAPI.APIs.Permissions {
8 [UsedImplicitly]
9 public class WebModules : AbsRestApi {
10 private const string propertyModule = "module";
11 private const string propertyPermissionLevelGlobal = "permissionLevelGlobal";
12 private const string propertyPermissionLevelPerMethod = "permissionLevelPerMethod";
13 private const string propertyIsDefault = "isDefault";
14
15
16 private static readonly byte[] jsonKeyModule = JsonWriter.GetEncodedPropertyNameWithBeginObject (propertyModule);
17 private static readonly byte[] jsonKeyPermissionLevelGlobal = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyPermissionLevelGlobal);
18 private static readonly byte[] jsonKeyPermissionLevelPerMethod = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyPermissionLevelPerMethod);
19 private static readonly byte[] jsonKeyIsDefault = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator (propertyIsDefault);
20
21 private static readonly byte[][] jsonMethodNameKeys;
22
23 static WebModules () {
24 jsonMethodNameKeys = new byte[(int)ERequestMethod.Count][];
25 for (int i = 0; i < jsonMethodNameKeys.Length; i++) {
26 ERequestMethod method = (ERequestMethod)i;
27 jsonMethodNameKeys [i] = JsonWriter.GetEncodedPropertyName (method.ToStringCached ());
28 }
29 }
30
31 private static AdminWebModules ModulesInstance => AdminWebModules.Instance;
32
33 protected override void HandleRestGet (RequestContext _context) {
34 string id = _context.RequestPath;
35
36 PrepareEnvelopedResult (out JsonWriter writer);
37
38 if (string.IsNullOrEmpty (id)) {
39
40 writer.WriteBeginArray ();
41
42 bool first = true;
43 foreach (AdminWebModules.WebModule module in ModulesInstance.GetModules ()) {
44 if (!first) {
45 writer.WriteValueSeparator ();
46 }
47
48 first = false;
49
50 writeModuleJson (ref writer, module);
51 }
52
53 writer.WriteEndArray ();
54
55 SendEnvelopedResult (_context, ref writer);
56 return;
57 }
58
59 writer.WriteRaw (WebUtils.JsonEmptyData);
60 SendEnvelopedResult (_context, ref writer, HttpStatusCode.BadRequest);
61 }
62
63 private void writeModuleJson (ref JsonWriter _writer, AdminWebModules.WebModule _module) {
64 _writer.WriteRaw (jsonKeyModule);
65 _writer.WriteString (_module.Name);
66 _writer.WriteRaw (jsonKeyPermissionLevelGlobal);
67 _writer.WriteInt32 (_module.LevelGlobal);
68 _writer.WriteRaw (jsonKeyPermissionLevelPerMethod);
69
70 _writer.WriteBeginObject ();
71
72 if (_module.LevelPerMethod != null) {
73 bool first = true;
74 for (int iMethod = 0; iMethod < _module.LevelPerMethod.Length; iMethod++) {
75 int methodLevel = _module.LevelPerMethod [iMethod];
76
77 if (methodLevel == AdminWebModules.MethodLevelNotSupported) {
78 continue;
79 }
80
81 if (!first) {
82 _writer.WriteValueSeparator ();
83 }
84
85 first = false;
86
87 _writer.WriteRaw (jsonMethodNameKeys [iMethod]);
88 if (methodLevel == AdminWebModules.MethodLevelInheritGlobal) {
89 _writer.WriteString ("inherit");
90 } else {
91 _writer.WriteInt32 (methodLevel);
92 }
93 }
94 }
95
96 _writer.WriteEndObject ();
97
98 _writer.WriteRaw (jsonKeyIsDefault);
99 _writer.WriteBoolean (_module.IsDefault);
100
101 _writer.WriteEndObject ();
102 }
103
104 protected override void HandleRestPost (RequestContext _context, IDictionary<string, object> _jsonInput, byte[] _jsonInputData) {
105 string id = _context.RequestPath;
106
107 // TODO
108
109 if (string.IsNullOrEmpty (id)) {
110 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_MODULE");
111 return;
112 }
113
114 if (!JsonCommons.TryGetJsonField (_jsonInput, propertyPermissionLevelGlobal, out int permissionLevel)) {
115 SendEmptyResponse (_context, HttpStatusCode.BadRequest, _jsonInputData, "NO_OR_INVALID_PERMISSION_LEVEL");
116 return;
117 }
118
119 // ModulesInstance.AddModule (id, permissionLevel);
120
121 SendEmptyResponse (_context, HttpStatusCode.Created);
122 }
123
124 protected override void HandleRestDelete (RequestContext _context) {
125 string id = _context.RequestPath;
126
127 bool removed = ModulesInstance.RemoveModule (id);
128
129 SendEmptyResponse (_context, removed ? HttpStatusCode.NoContent : HttpStatusCode.NotFound);
130 }
131
132 protected override bool AllowPostWithId => true;
133
134 public override int[] DefaultMethodPermissionLevels () => new[] {
135 AdminWebModules.MethodLevelNotSupported,
136 AdminWebModules.MethodLevelInheritGlobal,
137 AdminWebModules.MethodLevelInheritGlobal,
138 AdminWebModules.MethodLevelNotSupported,
139 AdminWebModules.MethodLevelInheritGlobal
140 };
141 }
142}
Note: See TracBrowser for help on using the repository browser.