From 8ae86c70aa832ce2fd1f3abab5b017e3ac2fa83e Mon Sep 17 00:00:00 2001 From: vnugent Date: Tue, 24 Oct 2023 12:01:09 -0400 Subject: package updates, upstream ui updates, pki multi-key support --- back-end/src/Endpoints/ChannelEndpoint.cs | 51 +++++++------------ back-end/src/Endpoints/ContentEndpoint.cs | 84 ++++++++++--------------------- back-end/src/Endpoints/PostsEndpoint.cs | 77 +++++++++------------------- 3 files changed, 66 insertions(+), 146 deletions(-) (limited to 'back-end/src/Endpoints') diff --git a/back-end/src/Endpoints/ChannelEndpoint.cs b/back-end/src/Endpoints/ChannelEndpoint.cs index d51ad3a..700a19d 100644 --- a/back-end/src/Endpoints/ChannelEndpoint.cs +++ b/back-end/src/Endpoints/ChannelEndpoint.cs @@ -70,8 +70,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints object[] contexts = await ContentManager.GetAllContextsAsync(entity.EventCancellation); //Return the list to the client - entity.CloseResponseJson(HttpStatusCode.OK, contexts); - return VfReturnType.VirtualSkip; + return VirtualOkJson(entity, contexts); } protected override async ValueTask PostAsync(HttpEntity entity) @@ -81,8 +80,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints //Check user write-permissions if (webm.Assert(entity.Session.CanWrite() == true, "You do not have permission to add channels")) { - entity.CloseResponseJson(HttpStatusCode.Forbidden, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.Forbidden); } //Get the blog context from the request body @@ -90,22 +88,19 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(channel != null, "You must specify a new blog channel")) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //Validate the blog context if (!ChannelValidator.Validate(channel, webm)) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.UnprocessableEntity); } //Validate the feed if its defined if (channel.Feed != null && !FeedValidator.Validate(channel.Feed, webm)) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.UnprocessableEntity); } //Add the blog context to the manager @@ -113,13 +108,11 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(result, "A blog with the given name already exists")) { - entity.CloseResponseJson(HttpStatusCode.Conflict, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.Conflict); } //Return the new blog context to the client - entity.CloseResponse(HttpStatusCode.Created); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, HttpStatusCode.Created); } protected override async ValueTask PatchAsync(HttpEntity entity) @@ -129,8 +122,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints //Check user write-permissions if (webm.Assert(entity.Session.CanWrite() == true, "You do not have permission to add channels")) { - entity.CloseResponseJson(HttpStatusCode.Forbidden, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.Forbidden); } //Get the blog context from the request body @@ -138,22 +130,19 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(channel?.Id != null, "You must specify a new blog channel")) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //Validate the blog context if (!ChannelValidator.Validate(channel, webm)) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.UnprocessableEntity); } //Validate the feed if its defined if (channel.Feed != null && !FeedValidator.Validate(channel.Feed, webm)) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //Make sure the blog context exists @@ -161,8 +150,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(context != null, "The specified blog channel does not exist")) { - entity.CloseResponseJson(HttpStatusCode.NotFound, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.NotFound); } //Update the context @@ -170,16 +158,13 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(result, "Failed to update the channel setting")) { - entity.CloseResponseJson(HttpStatusCode.Conflict, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.Conflict); } //Update post feeds await PostManager.UpdateFeedForChannelAsync(channel, entity.EventCancellation); - - //Return the new blog context to the client - entity.CloseResponse(HttpStatusCode.Created); - return VfReturnType.VirtualSkip; + + return VirtualClose(entity, HttpStatusCode.Created); } protected override async ValueTask DeleteAsync(HttpEntity entity) @@ -205,10 +190,8 @@ namespace Content.Publishing.Blog.Admin.Endpoints //Delete the blog context await ContentManager.DeleteChannelAsync(context, entity.EventCancellation); - - //Return the new blog context to the client - entity.CloseResponse(HttpStatusCode.NoContent); - return VfReturnType.VirtualSkip; + + return VirtualClose(entity, HttpStatusCode.NoContent); } private sealed class ChannelRequest : BlogChannel, IJsonOnDeserialized diff --git a/back-end/src/Endpoints/ContentEndpoint.cs b/back-end/src/Endpoints/ContentEndpoint.cs index d362eed..2427cf0 100644 --- a/back-end/src/Endpoints/ContentEndpoint.cs +++ b/back-end/src/Endpoints/ContentEndpoint.cs @@ -75,8 +75,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints //Get the channel id if (!entity.QueryArgs.TryGetNonEmptyValue("channel", out string? channelId)) { - entity.CloseResponse(HttpStatusCode.BadRequest); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, HttpStatusCode.BadRequest); } //Get the channel @@ -84,8 +83,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (channel == null) { - entity.CloseResponse(HttpStatusCode.NotFound); - return VfReturnType.VirtualSkip; + return VfReturnType.NotFound; } //Get the content id, if not set get all content meta items @@ -95,9 +93,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints ContentMeta[] items = await _content.GetAllContentItemsAsync(channel, entity.EventCancellation); //Return the items - entity.CloseResponseJson(HttpStatusCode.OK, items); - return VfReturnType.VirtualSkip; - + return VirtualCloseJson(entity, items, HttpStatusCode.OK); } //See if the user wants to get a link to the content @@ -113,9 +109,8 @@ namespace Content.Publishing.Blog.Admin.Endpoints webm.Success = webm.Result != null; webm.Result ??= "The requested content item was not found in the database"; - //Return the link - entity.CloseResponse(webm); - return VfReturnType.VirtualSkip; + //Return the link in webmessage result + return VirtualOk(entity, webm); } else { @@ -130,17 +125,15 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (meta?.ContentType == null) { vms.Dispose(); - entity.CloseResponse(HttpStatusCode.NotFound); - return VfReturnType.VirtualSkip; + return VfReturnType.NotFound; } else { //rewind the stream vms.Seek(0, SeekOrigin.Begin); - //Return the content - entity.CloseResponse(HttpStatusCode.OK, HttpHelpers.GetContentType(meta.ContentType), vms); - return VfReturnType.VirtualSkip; + //Return the content stream + return VirtualClose(entity, HttpStatusCode.OK, HttpHelpers.GetContentType(meta.ContentType), vms); } } catch @@ -167,15 +160,13 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(entity.Session.CanWrite(), "You do not have permissions to update content")) { - entity.CloseResponseJson(HttpStatusCode.Forbidden, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.Forbidden); } //Make sure there is content attached if (webm.Assert(entity.Files.Count > 0, "No content was attached to the entity body")) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //Get the channel @@ -183,8 +174,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(channel != null, "The channel does not exist")) { - entity.CloseResponseJson(HttpStatusCode.NotFound, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.NotFound); } //Read meta from request @@ -192,23 +182,20 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(requestedMeta?.Id != null, "You must supply a content id")) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //Validate the meta if (!MetaValidator.Validate(requestedMeta, webm)) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.UnprocessableEntity); } //Get the original content meta ContentMeta? meta = await _content.GetMetaAsync(channel, requestedMeta.Id, entity.EventCancellation); if (webm.Assert(meta != null, "The requested content item does not exist")) { - entity.CloseResponseJson(HttpStatusCode.NotFound, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.NotFound); } //Currently only allow chaning the file name @@ -221,8 +208,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints webm.Result = meta; webm.Success = true; - entity.CloseResponse(webm); - return VfReturnType.VirtualSkip; + return VirtualOk(entity, webm); } /* @@ -240,15 +226,13 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(entity.Session.CanWrite(), "You do not have permissions to update content")) { - entity.CloseResponseJson(HttpStatusCode.Forbidden, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.Forbidden); } //Make sure there is content attached if (webm.Assert(entity.Files.Count > 0, "No content was attached to the entity body")) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //Get the first file @@ -257,15 +241,13 @@ namespace Content.Publishing.Blog.Admin.Endpoints //Check content length if (webm.Assert(file.FileData.Length <= MaxContentLength, $"The content length is too long, max length is {MaxContentLength} bytes")) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //the http layer should protect from this but just in case if(webm.Assert(file.ContentType != ContentType.NonSupported, "The uploaded file is not a supported system content type")) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //Get the channel @@ -273,8 +255,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(channel != null, "The channel does not exist")) { - entity.CloseResponseJson(HttpStatusCode.NotFound, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.NotFound); } ContentMeta? meta; @@ -287,8 +268,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(meta != null, "The request item does not exist")) { - entity.CloseResponseJson(HttpStatusCode.NotFound, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.NotFound); } //May want to change the content name @@ -306,8 +286,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints //Validate the meta after updating file name if (!MetaValidator.Validate(meta, webm)) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.UnprocessableEntity); } //Add or update the content @@ -317,8 +296,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints webm.Result = meta; webm.Success = true; - entity.CloseResponse(webm); - return VfReturnType.VirtualSkip; + return VirtualOk(entity, webm); } protected override async ValueTask DeleteAsync(HttpEntity entity) @@ -331,15 +309,13 @@ namespace Content.Publishing.Blog.Admin.Endpoints //Get the channel id if (!entity.QueryArgs.TryGetNonEmptyValue("channel", out string? channelId)) { - entity.CloseResponse(HttpStatusCode.BadRequest); - return VfReturnType.VirtualSkip; + return VfReturnType.BadRequest; } //get the content id if (!entity.QueryArgs.TryGetNonEmptyValue("id", out string? contentId)) { - entity.CloseResponse(HttpStatusCode.BadRequest); - return VfReturnType.VirtualSkip; + return VfReturnType.BadRequest; } //Get channel @@ -352,15 +328,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints //Try to delete the content bool deleted = await _content.DeleteContentAsync(channel, contentId, entity.EventCancellation); - if (deleted) - { - entity.CloseResponse(HttpStatusCode.OK); - return VfReturnType.VirtualSkip; - } - else - { - return VfReturnType.NotFound; - } + return deleted ? VirtualOk(entity) : VfReturnType.NotFound; } } diff --git a/back-end/src/Endpoints/PostsEndpoint.cs b/back-end/src/Endpoints/PostsEndpoint.cs index fe7a310..152b95a 100644 --- a/back-end/src/Endpoints/PostsEndpoint.cs +++ b/back-end/src/Endpoints/PostsEndpoint.cs @@ -65,23 +65,20 @@ namespace Content.Publishing.Blog.Admin.Endpoints { Result = "You do not have permission to read content" }; - entity.CloseResponseJson(HttpStatusCode.Forbidden, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.Forbidden); } //Try to get the blog id from the query if (!entity.QueryArgs.TryGetNonEmptyValue("channel", out string? contextId)) { - entity.CloseResponse(HttpStatusCode.BadRequest); - return VfReturnType.VirtualSkip; + return VfReturnType.BadRequest; } //Try to get the blog context from the id IChannelContext? context = await ContentManager.GetChannelAsync(contextId, entity.EventCancellation); if (context == null) { - entity.CloseResponse(HttpStatusCode.NotFound); - return VfReturnType.VirtualSkip; + return VfReturnType.NotFound; } //Try to get the post id from the query @@ -90,22 +87,12 @@ namespace Content.Publishing.Blog.Admin.Endpoints //Try to get single post PostMeta? post = await PostManager.GetPostAsync(context, postId, entity.EventCancellation); - if (post != null) - { - entity.CloseResponseJson(HttpStatusCode.OK, post); - } - else - { - entity.CloseResponse(HttpStatusCode.NotFound); - } - - return VfReturnType.VirtualSkip; + return post != null ? VirtualOkJson(entity, post) : VfReturnType.NotFound; } //Get the post meta list PostMeta[] posts = await PostManager.GetPostsAsync(context, entity.EventCancellation); - entity.CloseResponseJson(HttpStatusCode.OK, posts); - return VfReturnType.VirtualSkip; + return VirtualOkJson(entity, posts); } protected override async ValueTask PostAsync(HttpEntity entity) @@ -115,15 +102,13 @@ namespace Content.Publishing.Blog.Admin.Endpoints //Check for write permissions if (webm.Assert(entity.Session.CanWrite() == true, "You do not have permission to publish posts")) { - entity.CloseResponseJson(HttpStatusCode.Forbidden, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.Forbidden); } if (!entity.QueryArgs.TryGetNonEmptyValue("channel", out string? contextId)) { webm.Result = "No blog channel was selected"; - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //Try to get the blog context from the id @@ -131,8 +116,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(context != null, "A blog with the given id does not exist")) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.NotFound); } //Get the post from the request body @@ -140,15 +124,13 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(post != null, "Message body was empty")) { - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //Validate post if (!PostValidator.Validate(post, webm)) { - entity.CloseResponse(webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.UnprocessableEntity); } //Publish post to the blog @@ -159,8 +141,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints webm.Success = true; //Return updated post to client - entity.CloseResponse(webm); - return VfReturnType.VirtualSkip; + return VirtualOk(entity, webm); } protected override async ValueTask PatchAsync(HttpEntity entity) @@ -170,16 +151,14 @@ namespace Content.Publishing.Blog.Admin.Endpoints //Check for write permissions if (webm.Assert(entity.Session.CanWrite() == true, "You do not have permissions to update posts")) { - entity.CloseResponseJson(HttpStatusCode.Forbidden, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.Forbidden); } //Try to get the blog id from the query if (!entity.QueryArgs.TryGetNonEmptyValue("channel", out string? contextId)) { webm.Result = "You must select a blog channel to update posts"; - entity.CloseResponseJson(HttpStatusCode.BadRequest, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //Try to get the blog context from the id @@ -187,8 +166,7 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(channel != null, "The channel you selected does not exist")) { - entity.CloseResponseJson(HttpStatusCode.NotFound, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.NotFound); } //Get the blog post object @@ -196,15 +174,13 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(post != null, "Message body was empty")) { - entity.CloseResponse(webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.BadRequest); } //Validate post if (!PostValidator.Validate(post, webm)) { - entity.CloseResponse(webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.UnprocessableEntity); } //Update post against manager @@ -212,16 +188,14 @@ namespace Content.Publishing.Blog.Admin.Endpoints if (webm.Assert(result, "Failed to update post because it does not exist or the blog channel was not found")) { - entity.CloseResponse(webm); - return VfReturnType.VirtualSkip; + return VirtualOk(entity, webm); } //Success webm.Result = post; webm.Success = true; - entity.CloseResponse(webm); - return VfReturnType.VirtualSkip; + return VirtualOk(entity, webm); } protected override async ValueTask DeleteAsync(HttpEntity entity) @@ -233,38 +207,33 @@ namespace Content.Publishing.Blog.Admin.Endpoints { Result = "You do not have permission to delete content" }; - entity.CloseResponseJson(HttpStatusCode.Forbidden, webm); - return VfReturnType.VirtualSkip; + return VirtualClose(entity, webm, HttpStatusCode.Forbidden); } //Try to get the blog id from the query if (!entity.QueryArgs.TryGetNonEmptyValue("channel", out string? contextId)) { - entity.CloseResponse(HttpStatusCode.BadRequest); - return VfReturnType.VirtualSkip; + return VfReturnType.BadRequest; } //Try to get the blog context from the id IChannelContext? context = await ContentManager.GetChannelAsync(contextId, entity.EventCancellation); if (context == null) { - entity.CloseResponse(HttpStatusCode.NotFound); - return VfReturnType.VirtualSkip; + return VfReturnType.NotFound; } //Try to get the post id from the query if (!entity.QueryArgs.TryGetNonEmptyValue("post", out string? postId)) { - entity.CloseResponse(HttpStatusCode.NotFound); - return VfReturnType.VirtualSkip; + return VfReturnType.NotFound; } //Delete post await PostManager.DeletePostAsync(context, postId, entity.EventCancellation); //Success - entity.CloseResponse(HttpStatusCode.OK); - return VfReturnType.VirtualSkip; + return VirtualOk(entity); } } -- cgit