Compare commits
No commits in common. "065086bfa22bb16436d2d0ccf772f4ee094a0473" and "1a809acbfbe660c45ffd0a7372edb7c8b1d3611b" have entirely different histories.
065086bfa2
...
1a809acbfb
@ -11,22 +11,21 @@ public static partial class APIHandler
|
|||||||
//============MISC=================
|
//============MISC=================
|
||||||
.Map("/updatecache",cache=>{
|
.Map("/updatecache",cache=>{
|
||||||
cache.Run(async runner=>{
|
cache.Run(async runner=>{
|
||||||
await UpdateCache();
|
_ = await UpdateCache();
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status200OK,"Cache Updated.");
|
await WriteJsonResponse(runner,StatusCodes.Status200OK,"Cache Updated.");
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
//===========UNITS=================
|
//===========UNITS=================
|
||||||
.Map("/getunits", units =>{
|
.Map("/getunits", units =>{
|
||||||
units.Run(async runner=>
|
units.Run(async runner=>{
|
||||||
{
|
if (!await RequestValidated(runner,2)) return;
|
||||||
if (!await runner.RequestValidated(2)) return;
|
await WriteJsonResponse(runner,StatusCodes.Status200OK,"Success",Deployments);
|
||||||
await runner.WriteJsonResponse( StatusCodes.Status200OK, "Success", Deployments);
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.Map("/chunit", unit =>{
|
.Map("/chunit", unit =>{
|
||||||
unit.Run(async runner=>{
|
unit.Run(async runner=>{
|
||||||
if (!await runner.RequestValidated(2, "POST", true)) return;
|
if (!await RequestValidated(runner, 2, "POST", true)) return;
|
||||||
if(await runner.TryGetBodyJsonAsync(["deplid", "unitkerja"], CTS.Token) is Dictionary<string, JsonElement> InElement)
|
if(await TryGetBodyJsonAsync(runner, ["deplid", "unitkerja"], CTS.Token) is Dictionary<string, JsonElement> InElement)
|
||||||
{
|
{
|
||||||
Deployment CorrectDeployment = new(
|
Deployment CorrectDeployment = new(
|
||||||
InElement["deplid"].GetInt16(),
|
InElement["deplid"].GetInt16(),
|
||||||
@ -34,13 +33,13 @@ public static partial class APIHandler
|
|||||||
);
|
);
|
||||||
if (CorrectDeployment.UnitKerja.Length < 1)
|
if (CorrectDeployment.UnitKerja.Length < 1)
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status400BadRequest, "Unit Kerja can't be empty string.");
|
await WriteJsonResponse(runner,StatusCodes.Status400BadRequest, "Unit Kerja can't be empty string.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int i = Deployments.FindIndex(depl=>depl.DeplID == CorrectDeployment.DeplID);
|
int i = Deployments.FindIndex(depl=>depl.DeplID == CorrectDeployment.DeplID);
|
||||||
if(i<0)
|
if(i<0)
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status404NotFound,"Deployment ID not found.");
|
await WriteJsonResponse(runner,StatusCodes.Status404NotFound,"Deployment ID not found.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_ = await RunNonQueryAsync(CS,"UPDATE deployment SET unitkerja = @uk WHERE deplid = @id",Comm=>{
|
_ = await RunNonQueryAsync(CS,"UPDATE deployment SET unitkerja = @uk WHERE deplid = @id",Comm=>{
|
||||||
@ -48,19 +47,19 @@ public static partial class APIHandler
|
|||||||
Comm.Parameters.AddWithValue("@uk", CorrectDeployment.UnitKerja);
|
Comm.Parameters.AddWithValue("@uk", CorrectDeployment.UnitKerja);
|
||||||
},CTS.Token);
|
},CTS.Token);
|
||||||
Deployments[i] = CorrectDeployment;
|
Deployments[i] = CorrectDeployment;
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status202Accepted,"Data updated.",Deployments[i]);
|
await WriteJsonResponse(runner,StatusCodes.Status202Accepted,"Data updated.",Deployments[i]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.Map("/addunit", unit =>{
|
.Map("/addunit", unit =>{
|
||||||
unit.Run(async runner=>{
|
unit.Run(async runner=>{
|
||||||
if (!await runner.RequestValidated(2, "POST", true)) return;
|
if (!await RequestValidated(runner, 2, "POST", true)) return;
|
||||||
if(await runner.TryGetBodyJsonAsync(["unitkerja"], CTS.Token) is Dictionary<string, JsonElement> InElement)
|
if(await TryGetBodyJsonAsync(runner, ["unitkerja"], CTS.Token) is Dictionary<string, JsonElement> InElement)
|
||||||
{
|
{
|
||||||
string UnitKerja = InElement["unitkerja"].GetString() ?? "";
|
string UnitKerja = InElement["unitkerja"].GetString() ?? "";
|
||||||
if (UnitKerja.Length < 1)
|
if (UnitKerja.Length < 1)
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status400BadRequest, "Unit Kerja can't be empty string.");
|
await WriteJsonResponse(runner,StatusCodes.Status400BadRequest, "Unit Kerja can't be empty string.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
short DeplID = (short)await RunScalarAsync(CS,"INSERT INTO deployment OUTPUT INSERTED.deplid VALUES (@uk)",Comm=>{
|
short DeplID = (short)await RunScalarAsync(CS,"INSERT INTO deployment OUTPUT INSERTED.deplid VALUES (@uk)",Comm=>{
|
||||||
@ -69,30 +68,30 @@ public static partial class APIHandler
|
|||||||
Deployment Inserted = new(DeplID,UnitKerja);
|
Deployment Inserted = new(DeplID,UnitKerja);
|
||||||
Deployments.Add(Inserted);
|
Deployments.Add(Inserted);
|
||||||
// EventsMarker.CacheUpdates = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString("X");
|
// EventsMarker.CacheUpdates = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString("X");
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status201Created,"Data Created.",Inserted);
|
await WriteJsonResponse(runner,StatusCodes.Status201Created,"Data Created.",Inserted);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
//============AGENTS==============
|
//============AGENTS==============
|
||||||
.Map("/getagents", agents=>{
|
.Map("/getagents", agents=>{
|
||||||
agents.Run(async runner=>{
|
agents.Run(async runner=>{
|
||||||
if (!await runner.RequestValidated(2)) return;
|
if (!await RequestValidated(runner,2)) return;
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status200OK,"Success",Agents);
|
await WriteJsonResponse(runner,StatusCodes.Status200OK,"Success",Agents);
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.Map("/addagent", agent=>{
|
.Map("/addagent", agent=>{
|
||||||
agent.Run(async runner=>{
|
agent.Run(async runner=>{
|
||||||
if (!await runner.RequestValidated(1, "POST", true)) return;
|
if (!await RequestValidated(runner, 1, "POST", true)) return;
|
||||||
if (await runner.TryGetBodyJsonAsync(["agentid", "name", "jabatan", "deplid", "skangkat", "tmt", "skper", "tgper", "vision", "mission", "photo", "createuser", "uname", "pass", "level"], CTS.Token) is Dictionary<string, JsonElement> InElement)
|
if (await TryGetBodyJsonAsync(runner, ["agentid", "name", "jabatan", "deplid", "skangkat", "tmt", "skper", "tgper", "vision", "mission", "photo", "createuser", "uname", "pass", "level"], CTS.Token) is Dictionary<string, JsonElement> InElement)
|
||||||
{
|
{
|
||||||
string AgentID = InElement["agentid"].GetString() ?? string.Empty;
|
string AgentID = InElement["agentid"].GetString() ?? string.Empty;
|
||||||
string Name = InElement["name"].GetString() ?? string.Empty;
|
string Name = InElement["nama"].GetString() ?? string.Empty;
|
||||||
string Jabatan = InElement["jabatan"].GetString() ?? string.Empty;
|
string Jabatan = InElement["jabatan"].GetString() ?? string.Empty;
|
||||||
short DeploymentID = InElement["deplid"].GetInt16();
|
short DeploymentID = InElement["deplid"].GetInt16();
|
||||||
string SKAngkat = InElement["skangkat"].GetString() ?? string.Empty;
|
string SKAngkat = InElement["skangkat"].GetString() ?? string.Empty;
|
||||||
DateOnly TMT = DateOnly.Parse(InElement["tmt"].GetString()?[..10] ?? "1970-01-01");
|
DateTime TMT = DateTime.Parse(InElement["tmt"].GetString() ?? "1970-01-01");
|
||||||
string SKPer = InElement["skper"].GetString() ?? string.Empty;
|
string SKPer = InElement["skper"].GetString() ?? string.Empty;
|
||||||
DateOnly? TGPer = InElement["tgper"].GetString()?.Length > 0 ? DateOnly.Parse(InElement["tgper"].GetString()![..10]) : null;
|
DateTime? TGPer = InElement["tgper"].GetString()?.Length > 0 ? DateTime.Parse(InElement["tgper"].GetString()!) : null;
|
||||||
string Vision = InElement["vision"].GetString() ?? "-";
|
string Vision = InElement["vision"].GetString() ?? "-";
|
||||||
string Mission = InElement["mission"].GetString() ?? "-";
|
string Mission = InElement["mission"].GetString() ?? "-";
|
||||||
string Photo = InElement["photo"].GetString() ?? string.Empty;
|
string Photo = InElement["photo"].GetString() ?? string.Empty;
|
||||||
@ -112,10 +111,9 @@ public static partial class APIHandler
|
|||||||
(CreateUser && UName.Equals(string.Empty)) ||
|
(CreateUser && UName.Equals(string.Empty)) ||
|
||||||
(CreateUser && PlainPass.Equals(string.Empty)) ||
|
(CreateUser && PlainPass.Equals(string.Empty)) ||
|
||||||
(!Photo.Equals(string.Empty) && !PhotoMatch.Success) ||
|
(!Photo.Equals(string.Empty) && !PhotoMatch.Success) ||
|
||||||
(!await runner.RequestValidated(Level, "POST"))
|
(await RequestValidated(runner, Level, "POST")))
|
||||||
)
|
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse( StatusCodes.Status400BadRequest, "One or more input(s) are not acceptable, in an unsupported format, or an attempt to create user account of a higher level than the creator is made.");
|
await WriteJsonResponse(runner, StatusCodes.Status400BadRequest, "One or more input(s) are not acceptable, in an unsupported format, or an attempt to create user account of a higher level than the creator is made.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!Photo.Equals(string.Empty))
|
if (!Photo.Equals(string.Empty))
|
||||||
@ -129,13 +127,12 @@ public static partial class APIHandler
|
|||||||
if (!File.Exists(PhotoPath)) await File.WriteAllBytesAsync(PhotoPath, ImageBytes, CTS.Token);
|
if (!File.Exists(PhotoPath)) await File.WriteAllBytesAsync(PhotoPath, ImageBytes, CTS.Token);
|
||||||
PhotoURL = Path.Combine("/assets/images/uploads", PhotoFileName);
|
PhotoURL = Path.Combine("/assets/images/uploads", PhotoFileName);
|
||||||
}
|
}
|
||||||
Agent NewAgent = new(AgentID, Name, Jabatan, DeploymentID, SKAngkat, TMT, SKPer.Length == 0 ? null : SKPer, SKPer.Length == 0 ? null : TGPer, Vision, Mission, Photo.Length == 0 ? null : PhotoURL);
|
|
||||||
await RunTransactionAsync(CS, async (Conn, Trans) =>
|
await RunTransactionAsync(CS, async (Conn, Trans) =>
|
||||||
{
|
{
|
||||||
using (SqlCommand CreateAgent = Conn.CreateCommand())
|
using (SqlCommand CreateAgent = Conn.CreateCommand())
|
||||||
{
|
{
|
||||||
CreateAgent.Transaction = Trans;
|
CreateAgent.Transaction = Trans;
|
||||||
CreateAgent.CommandText = "INSERT INTO agents VALUES(@agid, @nama, @jabt, @deid, @skng, @tmt, @skpr, @tmpr, @visi, @misi, @poto)";
|
CreateAgent.CommandText = "INSERT INTO agents VALUE(@agid, @nama, @jabt, @deid, @skng, @tmt, @skpr, @tmpr, @visi, @misi, @poto)";
|
||||||
CreateAgent.Parameters.AddWithValue("@agid", AgentID);
|
CreateAgent.Parameters.AddWithValue("@agid", AgentID);
|
||||||
CreateAgent.Parameters.AddWithValue("@nama", Name);
|
CreateAgent.Parameters.AddWithValue("@nama", Name);
|
||||||
CreateAgent.Parameters.AddWithValue("@jabt", Jabatan);
|
CreateAgent.Parameters.AddWithValue("@jabt", Jabatan);
|
||||||
@ -166,8 +163,7 @@ public static partial class APIHandler
|
|||||||
}, CTS.Token
|
}, CTS.Token
|
||||||
);
|
);
|
||||||
string OutMessage = CreateUser ? "New Agent and respective User Account created" : "New Agent created. User account creation is possible.";
|
string OutMessage = CreateUser ? "New Agent and respective User Account created" : "New Agent created. User account creation is possible.";
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status201Created, OutMessage, CreateUser?new SafeUser(UName,AgentID,Level,true):NewAgent);
|
await WriteJsonResponse(runner, StatusCodes.Status201Created, OutMessage);
|
||||||
// await runner.WriteJsonResponse(200, "OK", NewAgent);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
83
Auth.cs
83
Auth.cs
@ -30,108 +30,83 @@ public static class Auth
|
|||||||
internal static void Handle(IApplicationBuilder App)
|
internal static void Handle(IApplicationBuilder App)
|
||||||
{
|
{
|
||||||
App
|
App
|
||||||
.Map("/logout", logout =>
|
.Map("/logout", logout =>{
|
||||||
|
logout.Run(async runner=>{
|
||||||
|
if(!HttpMethods.IsGet(runner.Request.Method) || runner.Request.HasJsonContentType() || runner.Request.HasFormContentType)
|
||||||
{
|
{
|
||||||
logout.Run(async runner =>
|
await WriteJsonResponse(runner,StatusCodes.Status400BadRequest,"Improper request to log out.");
|
||||||
{
|
|
||||||
if (!HttpMethods.IsGet(runner.Request.Method) || runner.Request.HasJsonContentType() || runner.Request.HasFormContentType)
|
|
||||||
{
|
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status400BadRequest, "Improper request to log out.");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
runner.Response.Cookies.Append("session", "", Delete);
|
runner.Response.Cookies.Append("session", "", Delete);
|
||||||
if (runner.Items.ContainsKey("AuthorizedUser"))
|
if(runner.Items.ContainsKey("AuthorizedUser"))
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status200OK, "Log out successful. Authorisation token deleted.");
|
await WriteJsonResponse(runner,StatusCodes.Status200OK,"Log out successful. Authorisation token deleted.");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status200OK, "No user to log out. Authorisation token does not exists.");
|
await WriteJsonResponse(runner,StatusCodes.Status200OK,"No user to log out. Authorisation token does not exists.");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.Map("/login", login =>
|
.Map("/login", login =>{
|
||||||
|
login.Run(async runner=>{
|
||||||
|
if(runner.Items.ContainsKey("AuthorizedUser"))
|
||||||
{
|
{
|
||||||
login.Run(async runner =>
|
await WriteJsonResponse(runner,StatusCodes.Status409Conflict,"Log in not allowed when an authorized user is already logged in.");
|
||||||
{
|
|
||||||
if (runner.Items.ContainsKey("AuthorizedUser"))
|
|
||||||
{
|
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status409Conflict, "Log in not allowed when an authorized user is already logged in.");
|
|
||||||
}
|
}
|
||||||
else if (runner.Request.ContentType != "application/json")
|
else if(runner.Request.ContentType != "application/json")
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status400BadRequest, "Request Content-Type mismatch.");
|
await WriteJsonResponse(runner,StatusCodes.Status400BadRequest,"Request Content-Type mismatch.");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LoginUser LoginInfo = (await runner.Request.ReadFromJsonAsync(SGContext.Default.LoginUser))!;
|
LoginUser LoginInfo = (await runner.Request.ReadFromJsonAsync(SGContext.Default.LoginUser))!;
|
||||||
if (LoginInfo.Username is null || LoginInfo.Password is null)
|
if(LoginInfo.Username is null || LoginInfo.Password is null)
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status401Unauthorized, "No valid user information provided.");
|
await WriteJsonResponse(runner,StatusCodes.Status401Unauthorized,"No valid user information provided.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
string SHA256Pass = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(LoginInfo.Password)));
|
string SHA256Pass = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(LoginInfo.Password)));
|
||||||
if (UserAccounts.TryGetValue(LoginInfo.Username, out User? FoundUser) && FoundUser.Password.Equals(SHA256Pass, StringComparison.InvariantCultureIgnoreCase) && FoundUser.Active)
|
if (UserAccounts.TryGetValue(LoginInfo.Username, out User? FoundUser) && FoundUser.Password.Equals(SHA256Pass,StringComparison.InvariantCultureIgnoreCase) && FoundUser.Active)
|
||||||
{
|
{
|
||||||
SafeUser LoggedIn = SafeUser.FromUser(FoundUser);
|
SafeUser LoggedIn = SafeUser.FromUser(FoundUser);
|
||||||
string LoggedInBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(LoggedIn, SGContext.Default.SafeUser)));
|
string LoggedInBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(LoggedIn,SGContext.Default.SafeUser)));
|
||||||
string Signature = Convert.ToBase64String(SHA256.HashData(Encoding.UTF8.GetBytes(LoggedInBase64 + SecretKey)));
|
string Signature = Convert.ToBase64String(SHA256.HashData(Encoding.UTF8.GetBytes(LoggedInBase64+SecretKey)));
|
||||||
runner.Response.Cookies.Append("session", $"{LoggedInBase64}.{Signature}", HttpOnly);
|
runner.Response.Cookies.Append("session", $"{LoggedInBase64}.{Signature}", HttpOnly);
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status200OK, "User authorised. Authorisation token created.", LoggedIn);
|
await WriteJsonResponse(runner,StatusCodes.Status200OK,"User authorised. Authorisation token created.",LoggedIn);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status401Unauthorized, "Provided user information cannot be authorized");
|
await WriteJsonResponse(runner,StatusCodes.Status401Unauthorized,"Provided user information cannot be authorized");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.Map("/gate", auth =>
|
.Map("/gate", auth =>{
|
||||||
|
auth.Run(async runner=>{
|
||||||
|
if(runner.Request.ContentType != "application/json")
|
||||||
{
|
{
|
||||||
auth.Run(async runner =>
|
await WriteJsonResponse(runner,StatusCodes.Status400BadRequest,"Request Content-Type mismatch.");
|
||||||
{
|
|
||||||
if (runner.Request.ContentType != "application/json")
|
|
||||||
{
|
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status400BadRequest, "Request Content-Type mismatch.");
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LoginUser LoginInfo = (await runner.Request.ReadFromJsonAsync(SGContext.Default.LoginUser))!;
|
LoginUser LoginInfo = (await runner.Request.ReadFromJsonAsync(SGContext.Default.LoginUser))!;
|
||||||
if (LoginInfo.Username is null || LoginInfo.Password is null)
|
if(LoginInfo.Username is null || LoginInfo.Password is null)
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status401Unauthorized, "No valid user information provided.");
|
await WriteJsonResponse(runner,StatusCodes.Status401Unauthorized,"No valid user information provided.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
string SHA256Pass = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(LoginInfo.Password)));
|
string SHA256Pass = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(LoginInfo.Password)));
|
||||||
if (UserAccounts.TryGetValue(LoginInfo.Username, out User? FoundUser) && FoundUser.Password.Equals(SHA256Pass, StringComparison.InvariantCultureIgnoreCase) && FoundUser.Active)
|
if (UserAccounts.TryGetValue(LoginInfo.Username, out User? FoundUser) && FoundUser.Password.Equals(SHA256Pass,StringComparison.InvariantCultureIgnoreCase) && FoundUser.Active)
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status200OK, "User authorised. Operation may continue.", SafeUser.FromUser(FoundUser));
|
await WriteJsonResponse(runner,StatusCodes.Status200OK,"User authorised. Operation may continue.",SafeUser.FromUser(FoundUser));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status401Unauthorized, "Provided user information cannot be authorized");
|
await WriteJsonResponse(runner,StatusCodes.Status401Unauthorized,"Provided user information cannot be authorized");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
|
||||||
.Map("/me", me =>
|
|
||||||
{
|
|
||||||
me.Run(async runner =>
|
|
||||||
{
|
|
||||||
if (TryGetUser(runner, out SafeUser user))
|
|
||||||
{
|
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status200OK, "Success.", user);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status401Unauthorized, "No user session token is included with the request.");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
})
|
|
||||||
.Run(async runner=>
|
|
||||||
{
|
|
||||||
await runner.WriteJsonResponse(StatusCodes.Status404NotFound);
|
|
||||||
})
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
160
Commons.cs
160
Commons.cs
@ -53,7 +53,32 @@ internal static class Commons
|
|||||||
CTS.Cancel();
|
CTS.Cancel();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
internal static async Task WriteJsonResponse(HttpContext Context, int Status, string Message, object Data)
|
||||||
|
{
|
||||||
|
Context.Response.StatusCode = Status;
|
||||||
|
await Context.Response.WriteAsJsonAsync(new ApiResponse(Status, Message, Data), SGContext.Default.ApiResponse,cancellationToken: CTS.Token);
|
||||||
|
}
|
||||||
|
internal static async Task WriteJsonResponse(HttpContext Context, int Status, string Message)
|
||||||
|
{
|
||||||
|
Context.Response.StatusCode = Status;
|
||||||
|
await Context.Response.WriteAsJsonAsync(new SimpleApiResponse(Status, Message), SGContext.Default.SimpleApiResponse,cancellationToken: CTS.Token);
|
||||||
|
}
|
||||||
|
internal static async Task<bool> RequestValidated(HttpContext Context, int RequiredLevel = 0, string ValidMethod = "GET", bool CheckJson = false)
|
||||||
|
{
|
||||||
|
if (!ValidMethod.Equals(Context.Request.Method,StringComparison.OrdinalIgnoreCase) ||
|
||||||
|
(CheckJson && !Context.Request.HasJsonContentType()))
|
||||||
|
{
|
||||||
|
await WriteJsonResponse(Context, StatusCodes.Status405MethodNotAllowed, "Method Not Allowed.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Auth.IsAuthorized(Context, RequiredLevel))
|
||||||
|
{
|
||||||
|
await WriteJsonResponse(Context, StatusCodes.Status401Unauthorized, "Unauthorized.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
internal static async Task<int> RunNonQueryAsync(string ConnectionString, string SQL, Action<SqlCommand>? Configure = null, CancellationToken Token = default)
|
internal static async Task<int> RunNonQueryAsync(string ConnectionString, string SQL, Action<SqlCommand>? Configure = null, CancellationToken Token = default)
|
||||||
{
|
{
|
||||||
await using SqlConnection Conn = new(ConnectionString);
|
await using SqlConnection Conn = new(ConnectionString);
|
||||||
@ -92,7 +117,6 @@ internal static class Commons
|
|||||||
{
|
{
|
||||||
|
|
||||||
await Tran.RollbackAsync(Token);
|
await Tran.RollbackAsync(Token);
|
||||||
throw;
|
|
||||||
// if (ex.State != 255) throw; //state = 255 is custom sql error, designed just for this purpose of not rethrowing.
|
// if (ex.State != 255) throw; //state = 255 is custom sql error, designed just for this purpose of not rethrowing.
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@ -119,6 +143,23 @@ internal static class Commons
|
|||||||
uuidBytes[8] = (byte)((uuidBytes[8] & 0x3F) | 0x80); // Set variant to 10xx
|
uuidBytes[8] = (byte)((uuidBytes[8] & 0x3F) | 0x80); // Set variant to 10xx
|
||||||
return Convert.ToHexString(uuidBytes).ToLower().Insert(8, "-").Insert(13, "-").Insert(18, "-").Insert(23, "-");
|
return Convert.ToHexString(uuidBytes).ToLower().Insert(8, "-").Insert(13, "-").Insert(18, "-").Insert(23, "-");
|
||||||
}
|
}
|
||||||
|
internal static async Task<Dictionary<string, JsonElement>?> TryGetBodyJsonAsync(HttpContext Context, string[] Keys, CancellationToken Token)
|
||||||
|
{
|
||||||
|
using JsonDocument BodyDoc = await JsonDocument.ParseAsync(Context.Request.Body,cancellationToken: Token);
|
||||||
|
JsonElement Element = BodyDoc.RootElement;
|
||||||
|
Dictionary<string, JsonElement>? Result = new(StringComparer.OrdinalIgnoreCase);
|
||||||
|
foreach (string Key in Keys)
|
||||||
|
{
|
||||||
|
if (!Element.TryGetProperty(Key, out JsonElement Value))
|
||||||
|
{
|
||||||
|
await WriteJsonResponse(Context, StatusCodes.Status400BadRequest,
|
||||||
|
"Bad Request. One or more required properties were not received.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Result[Key] = Value.Clone();
|
||||||
|
}
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
internal static Dictionary<string, JsonElement>? JsonElementToDict(JsonElement Element, string[] Keys)
|
internal static Dictionary<string, JsonElement>? JsonElementToDict(JsonElement Element, string[] Keys)
|
||||||
{
|
{
|
||||||
Dictionary<string, JsonElement>? Result = new(StringComparer.OrdinalIgnoreCase);
|
Dictionary<string, JsonElement>? Result = new(StringComparer.OrdinalIgnoreCase);
|
||||||
@ -133,7 +174,7 @@ internal static class Commons
|
|||||||
return Result;
|
return Result;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static async Task UpdateCache()
|
internal static async Task<int> UpdateCache()
|
||||||
{
|
{
|
||||||
Console.WriteLine("Updating app cache.");
|
Console.WriteLine("Updating app cache.");
|
||||||
Console.Write(" Caching User Accounts... ");
|
Console.Write(" Caching User Accounts... ");
|
||||||
@ -147,8 +188,6 @@ internal static class Commons
|
|||||||
_ = UserAccounts.TryAdd((string)URead["uname"], new User((string)URead["uname"],(string)URead["name"],(string)URead["pass"],(byte)URead["level"],(bool)URead["active"]));
|
_ = UserAccounts.TryAdd((string)URead["uname"], new User((string)URead["uname"],(string)URead["name"],(string)URead["pass"],(byte)URead["level"],(bool)URead["active"]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Console.WriteLine("Done.");
|
|
||||||
Console.Write(" Caching Unit Kerja... ");
|
|
||||||
FComm.CommandText = "SELECT * FROM deployment";
|
FComm.CommandText = "SELECT * FROM deployment";
|
||||||
await using (SqlDataReader SRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
|
await using (SqlDataReader SRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
@ -157,8 +196,6 @@ internal static class Commons
|
|||||||
(string)r["unitkerja"]
|
(string)r["unitkerja"]
|
||||||
),CTS.Token);
|
),CTS.Token);
|
||||||
}
|
}
|
||||||
Console.WriteLine("Done.");
|
|
||||||
Console.Write(" Caching Agents Info... ");
|
|
||||||
FComm.CommandText = "SELECT * FROM agents";
|
FComm.CommandText = "SELECT * FROM agents";
|
||||||
await using (SqlDataReader SRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
|
await using (SqlDataReader SRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
|
||||||
{
|
{
|
||||||
@ -176,9 +213,70 @@ internal static class Commons
|
|||||||
r["photourl"] == DBNull.Value ? null : (string)r["photourl"]
|
r["photourl"] == DBNull.Value ? null : (string)r["photourl"]
|
||||||
),CTS.Token);
|
),CTS.Token);
|
||||||
}
|
}
|
||||||
|
// FComm.CommandText = "SELECT shift_sched.*, shifts.name FROM shifts LEFT JOIN shift_sched ON shifts.shiftid = shift_sched.shiftid ORDER BY shifts.shiftid, [day]";
|
||||||
|
// await using (SqlDataReader SRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
|
||||||
|
// {
|
||||||
|
// ShiftSched = await SRead.ToListAsync<ShiftSchedEntry>(r=>new(
|
||||||
|
// (byte)r["shiftid"],
|
||||||
|
// (string)r["name"],
|
||||||
|
// (byte)r["schedid"],
|
||||||
|
// (byte)r["day"],
|
||||||
|
// TimeOnly.FromTimeSpan((TimeSpan)r["start"]),
|
||||||
|
// TimeOnly.FromTimeSpan((TimeSpan)r["end"])
|
||||||
|
// ),CTS.Token);
|
||||||
|
// }
|
||||||
Console.WriteLine("Done.");
|
Console.WriteLine("Done.");
|
||||||
Console.WriteLine("App cache updated.");
|
Console.WriteLine("App cache updated.");
|
||||||
return;
|
return 0;
|
||||||
|
// FComm.CommandText = "SELECT shift_sched.*, shifts.name FROM shifts LEFT JOIN shift_sched ON shifts.shiftid = shift_sched.shiftid ORDER BY shifts.shiftid, [day]";
|
||||||
|
// await using (SqlDataReader SRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
|
||||||
|
// {
|
||||||
|
// ShiftSched = await SRead.ToListAsync<ShiftSchedEntry>(r=>new(
|
||||||
|
// (byte)r["shiftid"],
|
||||||
|
// (string)r["name"],
|
||||||
|
// (byte)r["schedid"],
|
||||||
|
// (byte)r["day"],
|
||||||
|
// TimeOnly.FromTimeSpan((TimeSpan)r["start"]),
|
||||||
|
// TimeOnly.FromTimeSpan((TimeSpan)r["end"])
|
||||||
|
// ),CTS.Token);
|
||||||
|
// }
|
||||||
|
// FComm.CommandText = "SELECT tariffs.*, tariff_sched.schedid, tariff_sched.amount FROM tariffs LEFT JOIN tariff_sched ON tariffs.tariffid = tariff_sched.tariffid ORDER BY tariffs.tariffid";
|
||||||
|
// await using (SqlDataReader TRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
|
||||||
|
// {
|
||||||
|
// TariffSched = await TRead.ToListAsync<TariffSchedEntry>(r=>new(
|
||||||
|
// (byte)r["tariffid"],
|
||||||
|
// (string)r["name"],
|
||||||
|
// (bool)r["active"],
|
||||||
|
// r["schedid"] == DBNull.Value ? null : (byte)r["schedid"],
|
||||||
|
// r["amount"] == DBNull.Value ? null : (int)r["amount"]
|
||||||
|
// ),CTS.Token);
|
||||||
|
// }
|
||||||
|
// FComm.CommandText = "SELECT * FROM charges";
|
||||||
|
// await using (SqlDataReader CRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
|
||||||
|
// {
|
||||||
|
// Charges = await CRead.ToListAsync<Charge>(r=>new(
|
||||||
|
// (byte)r["chargid"],
|
||||||
|
// (string)r["name"],
|
||||||
|
// r["percentage"] == DBNull.Value ? null : (byte)r["percentage"],
|
||||||
|
// r["amount"] == DBNull.Value ? null : (int)r["amount"]
|
||||||
|
// ),CTS.Token);
|
||||||
|
// }
|
||||||
|
// FComm.CommandText = "SELECT * FROM packview";
|
||||||
|
// await using (SqlDataReader PRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
|
||||||
|
// {
|
||||||
|
// Packages = await PRead.ToListAsync<PackItem>(r=>new(
|
||||||
|
// (byte)r["packid"],
|
||||||
|
// (string)r["name"],
|
||||||
|
// (short)r["durinmin"],
|
||||||
|
// (int)r["price"],
|
||||||
|
// (byte)r["shiftid"],
|
||||||
|
// (string)r["shift"],
|
||||||
|
// (byte)r["schedid"],
|
||||||
|
// (byte)r["day"],
|
||||||
|
// (bool)r["active"]
|
||||||
|
// ), CTS.Token);
|
||||||
|
// }
|
||||||
|
// EventsMarker.CacheUpdates = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString("X");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -236,7 +334,7 @@ internal static class DataReaderExtensions
|
|||||||
Func<SqlDataReader, T> map,
|
Func<SqlDataReader, T> map,
|
||||||
CancellationToken cancellationToken = default)
|
CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
List<T> list = [];
|
var list = new List<T>();
|
||||||
while (await reader.ReadAsync(cancellationToken))
|
while (await reader.ReadAsync(cancellationToken))
|
||||||
{
|
{
|
||||||
list.Add(map(reader));
|
list.Add(map(reader));
|
||||||
@ -244,52 +342,6 @@ internal static class DataReaderExtensions
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
internal static class HttpContextExtensions
|
|
||||||
{
|
|
||||||
internal static async Task<bool> RequestValidated(this HttpContext Context, int RequiredLevel = 0, string ValidMethod = "GET", bool CheckJson = false)
|
|
||||||
{
|
|
||||||
if (!ValidMethod.Equals(Context.Request.Method, StringComparison.OrdinalIgnoreCase) ||
|
|
||||||
(CheckJson && !Context.Request.HasJsonContentType()))
|
|
||||||
{
|
|
||||||
await Context.WriteJsonResponse(StatusCodes.Status405MethodNotAllowed, "Method Not Allowed.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Auth.IsAuthorized(Context, RequiredLevel))
|
|
||||||
{
|
|
||||||
await Context.WriteJsonResponse(StatusCodes.Status401Unauthorized, "Unauthorized.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
internal static async Task WriteJsonResponse(this HttpContext Context, int Status, string Message, object Data)
|
|
||||||
{
|
|
||||||
Context.Response.StatusCode = Status;
|
|
||||||
await Context.Response.WriteAsJsonAsync(new ApiResponse(Status, Message, Data), SGContext.Default.ApiResponse, cancellationToken: CTS.Token);
|
|
||||||
}
|
|
||||||
internal static async Task WriteJsonResponse(this HttpContext Context, int Status, string Message = "")
|
|
||||||
{
|
|
||||||
Context.Response.StatusCode = Status;
|
|
||||||
await Context.Response.WriteAsJsonAsync(new SimpleApiResponse(Status, Message), SGContext.Default.SimpleApiResponse, cancellationToken: CTS.Token);
|
|
||||||
}
|
|
||||||
internal static async Task<Dictionary<string, JsonElement>?> TryGetBodyJsonAsync(this HttpContext Context, string[] Keys, CancellationToken Token)
|
|
||||||
{
|
|
||||||
using JsonDocument BodyDoc = await JsonDocument.ParseAsync(Context.Request.Body,cancellationToken: Token);
|
|
||||||
JsonElement Element = BodyDoc.RootElement;
|
|
||||||
Dictionary<string, JsonElement>? Result = new(StringComparer.OrdinalIgnoreCase);
|
|
||||||
foreach (string Key in Keys)
|
|
||||||
{
|
|
||||||
if (!Element.TryGetProperty(Key, out JsonElement Value))
|
|
||||||
{
|
|
||||||
await Context.WriteJsonResponse(StatusCodes.Status400BadRequest,
|
|
||||||
"Bad Request. One or more required properties were not received.");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Result[Key] = Value.Clone();
|
|
||||||
}
|
|
||||||
return Result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public static class Crc32
|
public static class Crc32
|
||||||
{
|
{
|
||||||
static readonly uint[] Table;
|
static readonly uint[] Table;
|
||||||
|
@ -9,7 +9,7 @@ internal partial record ApiResponse(int Status, string Message, object Data);
|
|||||||
internal partial record Deployment(short DeplID, string UnitKerja);
|
internal partial record Deployment(short DeplID, string UnitKerja);
|
||||||
internal partial record LoginUser(string Username, string Password);
|
internal partial record LoginUser(string Username, string Password);
|
||||||
internal partial record PasswdUser(string Username, string PlainPassword);
|
internal partial record PasswdUser(string Username, string PlainPassword);
|
||||||
internal partial record SafeUser(string Username, string AgentID, byte Level, bool Active) {
|
internal partial record SafeUser(string Username, string Name, byte Level, bool Active) {
|
||||||
internal static SafeUser FromUser(User Source)
|
internal static SafeUser FromUser(User Source)
|
||||||
{
|
{
|
||||||
return new(Source.Username, Source.Name, Source.Level, Source.Active);
|
return new(Source.Username, Source.Name, Source.Level, Source.Active);
|
||||||
|
@ -26,7 +26,7 @@ try
|
|||||||
};
|
};
|
||||||
CS = CSBuilder.ConnectionString;
|
CS = CSBuilder.ConnectionString;
|
||||||
Console.WriteLine($"Done.");
|
Console.WriteLine($"Done.");
|
||||||
await UpdateCache();
|
_ = await UpdateCache();
|
||||||
Console.Write($"Configuring Kestrel Backend... ");
|
Console.Write($"Configuring Kestrel Backend... ");
|
||||||
IWebHost host = new WebHostBuilder()
|
IWebHost host = new WebHostBuilder()
|
||||||
.UseKestrel(options => {
|
.UseKestrel(options => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user