-
Notifications
You must be signed in to change notification settings - Fork 75
Fix: Cannot create templates during sync when running in production mode (Umbraco 17.3+) #938
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fa5e088
workaround the fact that you can't create templates in production mod…
KevinJump cb1c7ca
fix: remove unreachable null check in TemplateSerializer.DeserializeC…
Copilot 025f2de
fix: address all review feedback on SyncTemplateService and TemplateS…
Copilot 218c060
docs: clarify CreateAsync fallback condition in XML summary
Copilot a0107f3
clean using statements
KevinJump File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Models; | ||
| using Umbraco.Cms.Core.Services.OperationStatus; | ||
|
|
||
| namespace uSync.Core.Templates; | ||
|
|
||
| public interface ISyncTemplateService | ||
| { | ||
| Task<Attempt<ITemplate, TemplateOperationStatus>> CreateAsync(string name, string alias, string? content, Guid userKey, Guid key); | ||
| Task DeleteAsync(string alias, Guid userKey); | ||
| Task<ITemplate?> GetAsync(string alias); | ||
| Task<ITemplate?> GetAsync(Guid key); | ||
| Task<ITemplate?> GetAsync(int id); | ||
| Task<IEnumerable<ITemplate>> GetChildrenAsync(int templateId); | ||
| Task<Attempt<ITemplate, TemplateOperationStatus>> UpdateAsync(ITemplate template, Guid userKey); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| using Microsoft.Extensions.Configuration; | ||
|
|
||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Models; | ||
| using Umbraco.Cms.Core.Persistence.Repositories; | ||
| using Umbraco.Cms.Core.Scoping; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Cms.Core.Services.OperationStatus; | ||
| using Umbraco.Cms.Core.Strings; | ||
|
|
||
| using uSync.Core.Versions; | ||
|
|
||
| namespace uSync.Core.Templates; | ||
|
|
||
| /// <summary> | ||
| /// handles creation of templates, especially when running in production mode. | ||
| /// </summary> | ||
| internal class SyncTemplateService : ISyncTemplateService | ||
| { | ||
| private readonly ITemplateService _templateService; | ||
| private readonly IConfiguration _configuration; | ||
| private readonly ICoreScopeProvider _scopeProvider; | ||
|
|
||
| private readonly ITemplateRepository _templateRepository; | ||
| private readonly IShortStringHelper _shortStringHelper; | ||
|
|
||
| public SyncTemplateService(ITemplateService templateService, IConfiguration configuration, ICoreScopeProvider scopeProvider, ITemplateRepository templateRepository, IShortStringHelper shortStringHelper) | ||
| { | ||
| _templateService = templateService; | ||
| _configuration = configuration; | ||
| _scopeProvider = scopeProvider; | ||
| _templateRepository = templateRepository; | ||
| _shortStringHelper = shortStringHelper; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// creates a template, using the service when possible and falling back to the repository | ||
| /// when already in production mode or when the service returns <see cref="TemplateOperationStatus.NotAllowedInProductionMode"/>. | ||
| /// </summary> | ||
| /// <param name="name">The display name of the template.</param> | ||
| /// <param name="alias">The alias of the template.</param> | ||
| /// <param name="content">The template content.</param> | ||
| /// <param name="userKey">The key of the user performing the operation.</param> | ||
| /// <param name="key">The key to assign to the new template.</param> | ||
| /// <returns></returns> | ||
| public async Task<Attempt<ITemplate, TemplateOperationStatus>> CreateAsync(string name, string alias, string? content, Guid userKey, Guid key) | ||
| { | ||
| if (IsInProductionMode() is false) | ||
| { | ||
| var attempt = await _templateService.CreateAsync(name, alias, content, userKey, key); | ||
| if (attempt.Success) | ||
| return attempt; | ||
|
|
||
| // only fall back to the repository when blocked by production mode restrictions | ||
| if (attempt.Status is not TemplateOperationStatus.NotAllowedInProductionMode) | ||
| return attempt; | ||
| } | ||
|
|
||
| // in production mode (or blocked by it) - use the repository directly | ||
| // https://github.com/umbraco/Umbraco-CMS/pull/21600#issuecomment-4205232583 | ||
| return await CreateTemplateInternal(name, alias, content, userKey, key); | ||
| } | ||
|
|
||
| private Task<Attempt<ITemplate, TemplateOperationStatus>> CreateTemplateInternal(string name, string alias, string? content, Guid userKey, Guid key) | ||
| { | ||
| var template = new Template(_shortStringHelper, name, alias) | ||
| { | ||
| Content = content, | ||
| Key = key, | ||
| }; | ||
|
|
||
| try | ||
| { | ||
| using var scope = _scopeProvider.CreateCoreScope(autoComplete: true); | ||
| _templateRepository.Save(template); | ||
|
|
||
| return Task.FromResult(Attempt.SucceedWithStatus<ITemplate, TemplateOperationStatus>(TemplateOperationStatus.Success, template)); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| return Task.FromResult(Attempt.FailWithStatus<ITemplate, TemplateOperationStatus>(TemplateOperationStatus.NotAllowedInProductionMode, template, ex)); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public async Task<ITemplate?> GetAsync(Guid key) | ||
| => await _templateService.GetAsync(key); | ||
|
|
||
| public async Task<ITemplate?> GetAsync(string alias) | ||
| => await _templateService.GetAsync(alias); | ||
|
|
||
| public async Task<ITemplate?> GetAsync(int id) | ||
| => await _templateService.GetAsync(id); | ||
|
|
||
| public async Task<Attempt<ITemplate, TemplateOperationStatus>> UpdateAsync(ITemplate template, Guid userKey) | ||
| => await _templateService.UpdateAsync(template, userKey); | ||
|
|
||
| public async Task DeleteAsync(string alias, Guid userKey) | ||
| => await _templateService.DeleteAsync(alias, userKey); | ||
|
|
||
| public async Task<IEnumerable<ITemplate>> GetChildrenAsync(int templateId) | ||
| => await _templateService.GetChildrenAsync(templateId); | ||
|
|
||
| private bool IsInProductionMode() | ||
| => _configuration.IsUmbracoRunningInProductionMode(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.