Skip to content

Commit

Permalink
[FIX] according to review and add return statements where needed
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander van Delft committed Jan 27, 2025
1 parent 31e8eae commit 043f78f
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 283 deletions.
360 changes: 180 additions & 180 deletions CometServer/Modules/10-25/EngineeringModelApi.cs

Large diffs are not rendered by default.

31 changes: 15 additions & 16 deletions CometServer/Modules/10-25/ExchangeFileExportApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,27 @@ public override void AddRoutes(IEndpointRouteBuilder app)
return;
}

if (!req.HttpContext.User.Identity.IsAuthenticated)
if (!(req.HttpContext.User.Identity?.IsAuthenticated ?? false))
{
res.UpdateWithNotAuthenticatedSettings();
await res.AsJson("not authenticated");
return;
}
else

try
{
await this.Authorize(this.AppConfigService, credentialsService, req.HttpContext.User.Identity.Name);
}
catch (AuthorizationException)
{
try
{
await this.Authorize(this.AppConfigService, credentialsService, req.HttpContext.User.Identity.Name);
}
catch (AuthorizationException)
{
this.logger.LogWarning("The POST REQUEST was not authorized for {Identity}", req.HttpContext.User.Identity.Name);

res.UpdateWithNotAutherizedSettings();
await res.AsJson("not authorized");
return;
}

await this.PostResponseData(req, res, requestUtils, transactionManager, credentialsService, metaInfoProvider, jsonSerializer, jsonExchangeFileWriter);
this.logger.LogWarning("The POST REQUEST was not authorized for {Identity}", req.HttpContext.User.Identity.Name);

res.UpdateWithNotAutherizedSettings();
await res.AsJson("not authorized");
return;
}

await this.PostResponseData(req, res, requestUtils, transactionManager, credentialsService, metaInfoProvider, jsonSerializer, jsonExchangeFileWriter);
});
}

Expand Down
4 changes: 4 additions & 0 deletions CometServer/Modules/10-25/ExchangeFileImportyApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ internal async Task ImportDataStore(HttpRequest httpRequest, HttpResponse respon

response.StatusCode = (int)HttpStatusCode.Forbidden;
await response.AsJson("Data store IMPORT is not allowed");
return;
}

this.logger.LogInformation("{request}:{requestToken} - Starting data store IMPORT", httpRequest.QueryNameMethodPath(), requestToken);
Expand Down Expand Up @@ -456,6 +457,7 @@ private bool InsertModelData(IRequestUtils requestUtils, ICdp4TransactionManager
try
{
var sw = new Stopwatch();

if (seed)
{
// clear database schemas if seeding
Expand Down Expand Up @@ -544,6 +546,7 @@ private bool InsertModelData(IRequestUtils requestUtils, ICdp4TransactionManager
foreach (var person in items.OfType<Person>())
{
var credential = migrationCredentials.FirstOrDefault(mc => mc.Iid == person.Iid);

if (credential != null)
{
personService.UpdateCredentials(transaction, "SiteDirectory", person, credential);
Expand All @@ -558,6 +561,7 @@ private bool InsertModelData(IRequestUtils requestUtils, ICdp4TransactionManager
var engineeringModelSetups =
items.OfType<EngineeringModelSetup>()
.ToList();

var engineeringModelService = serviceProvider.MapToPersitableService<EngineeringModelService>("EngineeringModel");

var iterationService = serviceProvider.MapToPersitableService<IterationService>("Iteration");
Expand Down
165 changes: 82 additions & 83 deletions CometServer/Modules/10-25/SiteDirectoryApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,27 @@ public override void AddRoutes(IEndpointRouteBuilder app)
return;
}

if (!req.HttpContext.User.Identity.IsAuthenticated)
if (!(req.HttpContext.User.Identity?.IsAuthenticated ?? false))
{
res.UpdateWithNotAuthenticatedSettings();
await res.AsJson("not authenticated");
return;
}
else
{
try
{
await this.Authorize(this.AppConfigService, credentialsService, req.HttpContext.User.Identity.Name);
}
catch (AuthorizationException)
{
this.logger.LogWarning("The GET REQUEST was not authorized for {Identity}", req.HttpContext.User.Identity.Name);

res.UpdateWithNotAutherizedSettings();
await res.AsJson("not authorized");
return;
}
try
{
await this.Authorize(this.AppConfigService, credentialsService, req.HttpContext.User.Identity.Name);
}
catch (AuthorizationException)
{
this.logger.LogWarning("The GET REQUEST was not authorized for {Identity}", req.HttpContext.User.Identity.Name);

await this.GetResponseData(req, res, requestUtils, transactionManager, credentialsService, headerInfoProvider, serviceProvider, metaInfoProvider, revisionService, revisionResolver, jsonSerializer, messagePackSerializer, permissionInstanceFilterService);
res.UpdateWithNotAutherizedSettings();
await res.AsJson("not authorized");
return;
}

await this.GetResponseData(req, res, requestUtils, transactionManager, credentialsService, headerInfoProvider, serviceProvider, metaInfoProvider, revisionService, revisionResolver, jsonSerializer, messagePackSerializer, permissionInstanceFilterService);
});

app.MapGet("SiteDirectory/{*path}",
Expand All @@ -183,28 +182,27 @@ public override void AddRoutes(IEndpointRouteBuilder app)
return;
}

if (!req.HttpContext.User.Identity.IsAuthenticated)
if (!(req.HttpContext.User.Identity?.IsAuthenticated ?? false))
{
res.UpdateWithNotAuthenticatedSettings();
await res.AsJson("not authenticated");
return;
}
else
{
try
{
await this.Authorize(this.AppConfigService, credentialsService, req.HttpContext.User.Identity.Name);
}
catch (AuthorizationException)
{
this.logger.LogWarning("The GET REQUEST was not authorized for {Identity}", req.HttpContext.User.Identity.Name);

res.UpdateWithNotAutherizedSettings();
await res.AsJson("not authorized");
return;
}
try
{
await this.Authorize(this.AppConfigService, credentialsService, req.HttpContext.User.Identity.Name);
}
catch (AuthorizationException)
{
this.logger.LogWarning("The GET REQUEST was not authorized for {Identity}", req.HttpContext.User.Identity.Name);

await this.GetResponseData(req, res, requestUtils, transactionManager, credentialsService, headerInfoProvider, serviceProvider, metaInfoProvider, revisionService, revisionResolver, jsonSerializer, messagePackSerializer, permissionInstanceFilterService);
res.UpdateWithNotAutherizedSettings();
await res.AsJson("not authorized");
return;
}

await this.GetResponseData(req, res, requestUtils, transactionManager, credentialsService, headerInfoProvider, serviceProvider, metaInfoProvider, revisionService, revisionResolver, jsonSerializer, messagePackSerializer, permissionInstanceFilterService);
});

app.MapPost("SiteDirectory/{iid:guid}",
Expand All @@ -215,74 +213,75 @@ public override void AddRoutes(IEndpointRouteBuilder app)
return;
}

if (!req.HttpContext.User.Identity.IsAuthenticated)
if (!(req.HttpContext.User.Identity?.IsAuthenticated ?? false))
{
res.UpdateWithNotAuthenticatedSettings();
await res.AsJson("not authenticated");
return;
}
else
{
var requestToken = this.TokenGeneratorService.GenerateRandomToken();

try
{
await this.Authorize(this.AppConfigService, credentialsService, req.HttpContext.User.Identity.Name);
}
catch (AuthorizationException)
{
this.logger.LogWarning("The {requestToken} POST REQUEST was not authorized for {Identity}", requestToken, req.HttpContext.User.Identity.Name);
var requestToken = this.TokenGeneratorService.GenerateRandomToken();

res.UpdateWithNotAutherizedSettings();
await res.AsJson("not authorized");
return;
}
try
{
await this.Authorize(this.AppConfigService, credentialsService, req.HttpContext.User.Identity.Name);
}
catch (AuthorizationException)
{
this.logger.LogWarning("The {requestToken} POST REQUEST was not authorized for {Identity}", requestToken, req.HttpContext.User.Identity.Name);

var cometTask = this.CreateAndRegisterCometTask(credentialsService.Credentials.Person.Iid, TopContainer, requestToken);

PostRequestData postRequestData = null;
res.UpdateWithNotAutherizedSettings();
await res.AsJson("not authorized");
return;
}

try
{
postRequestData = this.ProcessPostRequest(req, requestUtils, metaInfoProvider, jsonSerializer);
}
catch (BadRequestException ex)
{
this.logger.LogWarning("Request {requestToken} failed as BadRequest \n {ErrorMessage}", requestToken, ex.Message);
var cometTask = this.CreateAndRegisterCometTask(credentialsService.Credentials.Person.Iid, TopContainer, requestToken);

PostRequestData postRequestData = null;

this.CometTaskService.AddOrUpdateTask(cometTask, finishedAt: DateTime.Now, statusKind: StatusKind.FAILED, error: $"BAD REQUEST - {ex.Message}");
try
{
postRequestData = this.ProcessPostRequest(req, requestUtils, metaInfoProvider, jsonSerializer);
}
catch (BadRequestException ex)
{
this.logger.LogWarning("Request {requestToken} failed as BadRequest \n {ErrorMessage}", requestToken, ex.Message);

res.StatusCode = (int)HttpStatusCode.BadRequest;
await res.AsJson($"exception:{ex.Message}");
}
catch (Exception ex)
{
this.logger.LogError("Request {requestToken} failed as InternalServerError \n {ErrorMessage}", requestToken, ex.Message);
this.CometTaskService.AddOrUpdateTask(cometTask, finishedAt: DateTime.Now, statusKind: StatusKind.FAILED, error: $"BAD REQUEST - {ex.Message}");

this.CometTaskService.AddOrUpdateTask(cometTask, finishedAt: DateTime.Now, statusKind: StatusKind.FAILED, error: $"INTERNAL SERVER ERROR - {ex.Message}");
res.StatusCode = (int)HttpStatusCode.BadRequest;
await res.AsJson($"exception:{ex.Message}");
return;
}
catch (Exception ex)
{
this.logger.LogError("Request {requestToken} failed as InternalServerError \n {ErrorMessage}", requestToken, ex.Message);

res.StatusCode = (int)HttpStatusCode.InternalServerError;
await res.AsJson($"exception:{ex.Message}");
return;
}
this.CometTaskService.AddOrUpdateTask(cometTask, finishedAt: DateTime.Now, statusKind: StatusKind.FAILED, error: $"INTERNAL SERVER ERROR - {ex.Message}");

if (postRequestData.IsMultiPart )
{
this.logger.LogWarning("Request {requestToken} failed as BadRequest: The SiteDirectory does not support MultiPart POST request", requestToken);
res.StatusCode = (int)HttpStatusCode.InternalServerError;
await res.AsJson($"exception:{ex.Message}");
return;
}

this.CometTaskService.AddOrUpdateTask(cometTask, finishedAt: DateTime.Now, statusKind: StatusKind.FAILED, error: "BAD REQUEST - The SiteDirectory does not support MultiPart POST request");
if (postRequestData.IsMultiPart )
{
this.logger.LogWarning("Request {requestToken} failed as BadRequest: The SiteDirectory does not support MultiPart POST request", requestToken);

res.StatusCode = (int)HttpStatusCode.BadRequest;
await res.AsJson("The SiteDirectory does not support MultiPart POST request");
}
this.CometTaskService.AddOrUpdateTask(cometTask, finishedAt: DateTime.Now, statusKind: StatusKind.FAILED, error: "BAD REQUEST - The SiteDirectory does not support MultiPart POST request");

if (requestUtils.QueryParameters.WaitTime > 0)
{
await this.EnqueCometTaskForPostRequest(postRequestData, requestToken, res, cometTask, requestUtils, transactionManager, credentialsService, headerInfoProvider, metaInfoProvider, operationProcessor, revisionService, jsonSerializer, messagePackSerializer, permissionInstanceFilterService, modelCreatorManager);
}
else
{
await this.PostResponseData(postRequestData, requestToken, res, cometTask, requestUtils, transactionManager, credentialsService, headerInfoProvider, metaInfoProvider, operationProcessor, revisionService, jsonSerializer, messagePackSerializer, permissionInstanceFilterService, modelCreatorManager);
}
res.StatusCode = (int)HttpStatusCode.BadRequest;
await res.AsJson("The SiteDirectory does not support MultiPart POST request");
return;
}

if (requestUtils.QueryParameters.WaitTime > 0)
{
await this.EnqueueCometTaskForPostRequest(postRequestData, requestToken, res, cometTask, requestUtils, transactionManager, credentialsService, headerInfoProvider, metaInfoProvider, operationProcessor, revisionService, jsonSerializer, messagePackSerializer, permissionInstanceFilterService, modelCreatorManager);
}
else
{
await this.PostResponseData(postRequestData, requestToken, res, cometTask, requestUtils, transactionManager, credentialsService, headerInfoProvider, metaInfoProvider, operationProcessor, revisionService, jsonSerializer, messagePackSerializer, permissionInstanceFilterService, modelCreatorManager);
}
});
}
Expand Down Expand Up @@ -558,7 +557,7 @@ protected PostRequestData ProcessPostRequest(HttpRequest httpRequest, IRequestUt
/// <returns>
/// an awaitable <see cref="Task"/>
/// </returns>
protected async Task EnqueCometTaskForPostRequest(PostRequestData postRequestData, string requestToken, HttpResponse httpResponse, CometTask cometTask, IRequestUtils requestUtils, ICdp4TransactionManager transactionManager, ICredentialsService credentialsService, IHeaderInfoProvider headerInfoProvider, IMetaInfoProvider metaInfoProvider, IOperationProcessor operationProcessor, IRevisionService revisionService, ICdp4JsonSerializer jsonSerializer, IMessagePackSerializer messagePackSerializer, IPermissionInstanceFilterService permissionInstanceFilterService, IModelCreatorManager modelCreatorManager)
protected async Task EnqueueCometTaskForPostRequest(PostRequestData postRequestData, string requestToken, HttpResponse httpResponse, CometTask cometTask, IRequestUtils requestUtils, ICdp4TransactionManager transactionManager, ICredentialsService credentialsService, IHeaderInfoProvider headerInfoProvider, IMetaInfoProvider metaInfoProvider, IOperationProcessor operationProcessor, IRevisionService revisionService, ICdp4JsonSerializer jsonSerializer, IMessagePackSerializer messagePackSerializer, IPermissionInstanceFilterService permissionInstanceFilterService, IModelCreatorManager modelCreatorManager)
{
var longRunningCometTask = Task.Run(() =>
{
Expand Down
6 changes: 3 additions & 3 deletions CometServer/Modules/Tasks/CometTasksModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public async Task QueryTasks(HttpRequest req, HttpResponse res, ICredentialsServ
return;
}

if (!req.HttpContext.User.Identity.IsAuthenticated)
if (!(req.HttpContext.User.Identity?.IsAuthenticated ?? false))
{
res.UpdateWithNotAuthenticatedSettings();
await res.AsJson("not authenticated");
Expand Down Expand Up @@ -185,7 +185,7 @@ public async Task QueryTask(HttpRequest req, HttpResponse res, ICredentialsServi
return;
}

if (!req.HttpContext.User.Identity.IsAuthenticated)
if (!(req.HttpContext.User.Identity?.IsAuthenticated ?? false))
{
res.UpdateWithNotAuthenticatedSettings();
await res.AsJson("not authenticated");
Expand Down Expand Up @@ -241,7 +241,7 @@ public async Task CancelTask(HttpRequest req, HttpResponse res, ICredentialsServ
return;
}

if (!req.HttpContext.User.Identity.IsAuthenticated)
if (!(req.HttpContext.User.Identity?.IsAuthenticated ?? false))
{
res.UpdateWithNotAuthenticatedSettings();
await res.AsJson("not authenticated");
Expand Down
2 changes: 1 addition & 1 deletion CometServer/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.Grafana.Loki" ],
"MinimumLevel": {
"Default": "Information",
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
Expand Down

0 comments on commit 043f78f

Please sign in to comment.