Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of publish/unpublish operations #17441

Conversation

MichaelPetrinolis
Copy link
Contributor

@MichaelPetrinolis MichaelPetrinolis commented Feb 1, 2025

This pull request includes several changes to the AdminController.cs and related files to improve error handling, authorization checks, and the publishing process for content items. The most important changes include adding detailed error handling, updating methods to return boolean values indicating success, and refactoring the controller methods to use these new return values.

Improvements to error handling:

Authorization and publishing checks:

Refactoring methods to return success status:

Adding support for error notifications:

Additional changes:

Updated AdminController to handle success/failure of publish/unpublish operations more robustly. Modified IContentManager interface to return Task<bool> for PublishAsync and UnpublishAsync methods. Implemented these changes in DefaultContentManager, including handling operation cancellations. Enhanced user feedback based on operation results.
Copy link
Member

@Piedone Piedone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nicely prepared PR, thanks for the helpful description.

I'd wait for the triage, because I'm really unsure about the IContentManager changes.

Copy link
Member

@hishamco hishamco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will leave the final approval to @Piedone after addressing his feedback

Copy link
Contributor

github-actions bot commented Feb 6, 2025

This pull request has merge conflicts. Please resolve those before requesting a review.

@hishamco
Copy link
Member

hishamco commented Feb 8, 2025

@MichaelPetrinolis could you please address @Piedone feedback, so that we can merge this PR

Introduce `authorized` variable in `AdminController` to track authorization status during bulk actions, redirecting to list view or returning forbidden response as needed. Update `conditionallyPublish` to return `Task<bool>` for better control flow in `CreateInternalAsync` and `EditInternalAsync`. Enhance `DefaultContentManager` with `INotifier` and `IHtmlLocalizer` dependencies to provide user notifications when publishing or unpublishing is canceled, including content type and display text in messages.
Updated AdminController to handle ModelState errors across redirects using TempData. Added RedirectToListActionWithModelState method. Updated List.cshtml to display validation errors. Replaced INotifier and IHtmlLocalizer with IUpdateModelAccessor and IStringLocalizer in DefaultContentManager. Ensured ModelUpdater is checked before adding ModelState errors.
Copy link
Member

@Piedone Piedone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, looks good now!

While I'm not a fan of TempData in the controller, I don't have a better suggestion, and it's localized anyway. Otherwise, only nitpicks.

Anybody else?

MichaelPetrinolis and others added 3 commits February 10, 2025 10:01
Co-authored-by: Hisham Bin Ateya <[email protected]>
Co-authored-by: Zoltán Lehóczky <[email protected]>
Refactor error handling in AdminController to iterate through error keys and messages before adding them to ModelState. Rename variable 'authorized' to 'isAuthorized' for better readability and reformat return statement for clarity.
@hishamco
Copy link
Member

@Piedone feel free to merge once you are fine with the latest changes

@Piedone
Copy link
Member

Piedone commented Feb 11, 2025

I am fine with the latest changes, hence the approve :). But @MichaelPetrinolis please address Sebastien's, and re-request review.

@hishamco
Copy link
Member

Here is a C# syntax that could help for the future

Yes, but when I saw "for the future," I thought it was not necessary to be addressed right now

The code refactor removes the `isAuthorized` variable and replaces it with an inline variable declaration using the `is var` pattern within the `if` statements. This change is applied to both the `PublishNow` and `Unpublish` cases within the `switch` statement handling `options.BulkAction`.

The refactor simplifies the authorization and content management checks by combining them into a single `if` statement, improving readability.

The return statements are updated to use the new `authorized` variable instead of the removed `isAuthorized` variable.
@Piedone Piedone merged commit 444852a into OrchardCMS:main Feb 12, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants