-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
272 lines (243 loc) · 8.74 KB
/
Program.cs
File metadata and controls
272 lines (243 loc) · 8.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Program.cs (ASP.NET Core 8/9 Minimal API)
using Microsoft.Data.SqlClient;
using System.Data;
var builder = WebApplication.CreateBuilder(args);
// Enforce HTTPS and HSTS
builder.Services.AddHsts(options =>
{
options.Preload = true;
options.IncludeSubDomains = true;
options.MaxAge = TimeSpan.FromDays(60);
});
var app = builder.Build();
app.UseHsts();
app.UseHttpsRedirection();
// Global exception handler middleware (production safe)
app.Use(async (context, next) =>
{
try
{
await next();
}
catch (Exception ex)
{
// Log the error (replace with your logging framework as needed)
app.Logger.LogError(ex, "Unhandled exception");
context.Response.StatusCode = 500;
context.Response.ContentType = "application/json";
var errorJson = System.Text.Json.JsonSerializer.Serialize(new
{
error = "An unexpected error occurred."
});
await context.Response.WriteAsync(errorJson);
}
});
app.MapGet("/health", () => Results.Ok(new { status = "ok" }));
app.MapPost("/ExecProc", async (HttpContext context, IConfiguration config) =>
{
var req = context.Request;
// Get username from SCALE's custom header (sent by SCALE web UI)
var rawUsername = req.Headers["UserName"].FirstOrDefault() ?? "Anonymous";
// Clean up username: extract just the username part
// Handles formats: "DOMAIN\\username" -> "username", "username@domain.com" -> "username"
var windowsIdentity = rawUsername;
if (rawUsername.Contains('\\'))
{
// Extract username from DOMAIN\username
windowsIdentity = rawUsername.Split('\\').Last();
}
else if (rawUsername.Contains('@'))
{
// Extract username from username@domain.com
windowsIdentity = rawUsername.Split('@').First();
}
// Get 'action' from query string
var action = req.Query["action"].ToString();
if (string.IsNullOrWhiteSpace(action))
{
return Results.BadRequest(new
{
ErrorCode = "MissingAction",
ErrorType = 1,
Message = "Missing required query parameter 'action'.",
AdditionalErrors = Array.Empty<string>(),
Data = (object?)null
});
}
// Limit request body size (e.g., 10 KB)
if (req.ContentLength is > 10_240)
{
return Results.BadRequest(new
{
ErrorCode = "PayloadTooLarge",
ErrorType = 1,
Message = "Request payload too large.",
AdditionalErrors = Array.Empty<string>(),
Data = (object?)null
});
}
using var reader = new StreamReader(req.Body);
var raw = await reader.ReadToEndAsync();
List<Dictionary<string, object>> items = [];
try
{
items = System.Text.Json.JsonSerializer.Deserialize<List<Dictionary<string, object>>>(raw)
?? [];
}
catch
{
try
{
var obj = System.Text.Json.JsonSerializer.Deserialize<Dictionary<string, object>>(raw);
if (obj != null)
items.Add(obj);
}
catch
{
return Results.BadRequest(new
{
ErrorCode = "InvalidPayload",
ErrorType = 1,
Message = "Invalid payload.",
AdditionalErrors = Array.Empty<string>(),
Data = (object?)null
});
}
}
if (items.Count == 0)
{
return Results.BadRequest(new
{
ErrorCode = "InvalidPayload",
ErrorType = 1,
Message = "Invalid payload.",
AdditionalErrors = Array.Empty<string>(),
Data = (object?)null
});
}
var connStr = config.GetConnectionString("DefaultConnection");
if (string.IsNullOrWhiteSpace(connStr))
return Results.Problem("Connection string not configured.", statusCode: 500);
await using var conn = new SqlConnection(connStr);
try
{
await conn.OpenAsync();
}
catch
{
return Results.Problem("Database connection failed.", statusCode: 500);
}
// Verify all items have required fields first
foreach (var item in items)
{
if (!item.TryGetValue("internalID", out var _) || !item.TryGetValue("changeValue", out var _))
{
return Results.BadRequest(new Dictionary<string, object?>
{
["ErrorCode"] = "MissingParams",
["ErrorType"] = 1,
["Message"] = "Missing required params 'internalID' and/or 'changeValue'.",
["AdditionalErrors"] = Array.Empty<string>(),
["Data"] = null
});
}
}
// Build comma-separated list of internal IDs (for single or multiple items)
var internalIDs = string.Join(",", items.Select(item =>
{
var id = item["internalID"];
if (id is System.Text.Json.JsonElement je && je.ValueKind == System.Text.Json.JsonValueKind.Number)
return je.GetInt32().ToString();
if (id is System.Text.Json.JsonElement jes && jes.ValueKind == System.Text.Json.JsonValueKind.String)
return jes.GetString() ?? "";
return id?.ToString() ?? "";
}));
// Use first item's changeValue (all rows share same value)
var changeValueRaw = items[0]["changeValue"];
object? changeValueObj = changeValueRaw is System.Text.Json.JsonElement jeCV && jeCV.ValueKind == System.Text.Json.JsonValueKind.Number
? jeCV.GetInt32()
: changeValueRaw is System.Text.Json.JsonElement jeCVs && jeCVs.ValueKind == System.Text.Json.JsonValueKind.String
? jeCVs.GetString()
: changeValueRaw;
// Call stored procedure ONCE with comma-separated IDs and Windows username
await using var cmd = new SqlCommand("usp_UserAction", conn) { CommandType = CommandType.StoredProcedure };
cmd.Parameters.AddWithValue("@action", action ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@internalID", internalIDs); // CSV list
cmd.Parameters.AddWithValue("@changeValue", changeValueObj ?? DBNull.Value);
cmd.Parameters.AddWithValue("@userName", windowsIdentity); // Windows authenticated user
// Collect all result messages from stored procedure and combine into single message
var successMessages = new List<string>();
var errorMessages = new List<string>();
string? finalCode = null;
try
{
await using var procReader = await cmd.ExecuteReaderAsync();
while (await procReader.ReadAsync())
{
var messageCode = procReader.GetString(procReader.GetOrdinal("MessageCode"));
var message = procReader.GetString(procReader.GetOrdinal("Message"));
if (messageCode.StartsWith("ERR_", StringComparison.OrdinalIgnoreCase))
{
errorMessages.Add(message);
finalCode ??= messageCode; // Use first error code
}
else
{
successMessages.Add(message);
finalCode ??= messageCode; // Use first success code
}
}
procReader.Close();
}
catch (Exception ex)
{
return Results.BadRequest(new Dictionary<string, object?>
{
["ErrorCode"] = "SqlError",
["ErrorType"] = 1,
["Message"] = ex.Message,
["AdditionalErrors"] = new[] { ex.ToString() },
["Data"] = new Dictionary<string, object?>
{
["action"] = action,
["internalID"] = internalIDs,
["changeValue"] = changeValueObj
}
});
}
// If no results returned, return error
if (finalCode == null)
{
return Results.BadRequest(new Dictionary<string, object?>
{
["ErrorCode"] = "NoResults",
["ErrorType"] = 1,
["Message"] = "Stored procedure returned no results.",
["AdditionalErrors"] = Array.Empty<string>(),
["Data"] = null
});
}
// Combine all messages into a single message
var combinedMessage = string.Join(" ", successMessages.Concat(errorMessages));
// If there are any errors, return BadRequest
if (errorMessages.Count > 0)
{
return Results.BadRequest(new Dictionary<string, object?>
{
["ErrorCode"] = finalCode,
["ErrorType"] = 1,
["Message"] = combinedMessage,
["AdditionalErrors"] = Array.Empty<string>(),
["Data"] = null
});
}
// All success - return OK
return Results.Ok(new Dictionary<string, object?>
{
["ConfirmationMessageCode"] = null,
["ConfirmationMessage"] = null,
["MessageCode"] = finalCode,
["Message"] = combinedMessage
});
});
app.Run();