Skip to content

Commit 43230df

Browse files
Redirects: Fixes self referencing redirects (closes #20139) (#20767)
* Adding fix for self-referncing redirects for 17 * Using umbraco context on failing tests * Tests to see if self referencing redirects gets deleted * Refactoring and adding correct tests. * Expanding tests for RedirectTrackerTests.cs * Optimize by only retrieving th list of existing URLs for a content item if we have a valid route to create a redirect for. * Extract method refactoring, added explanatory comment, fixed warnings and formatting. * Resolved warnings in RedirectService. * Minor naming and formatting refactor in tests. --------- Co-authored-by: Andy Butland <[email protected]>
1 parent 01b3003 commit 43230df

File tree

4 files changed

+323
-148
lines changed

4 files changed

+323
-148
lines changed
Lines changed: 58 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Threading.Tasks;
21
using Microsoft.Extensions.Logging;
32
using Umbraco.Cms.Core.Events;
43
using Umbraco.Cms.Core.Models;
@@ -7,10 +6,16 @@
76

87
namespace Umbraco.Cms.Core.Services;
98

9+
/// <summary>
10+
/// Provides services for managing redirect URLs.
11+
/// </summary>
1012
internal sealed class RedirectUrlService : RepositoryService, IRedirectUrlService
1113
{
1214
private readonly IRedirectUrlRepository _redirectUrlRepository;
1315

16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="RedirectUrlService"/> class.
18+
/// </summary>
1419
public RedirectUrlService(
1520
ICoreScopeProvider provider,
1621
ILoggerFactory loggerFactory,
@@ -19,132 +24,119 @@ public RedirectUrlService(
1924
: base(provider, loggerFactory, eventMessagesFactory) =>
2025
_redirectUrlRepository = redirectUrlRepository;
2126

27+
/// <inheritdoc/>
2228
public void Register(string url, Guid contentKey, string? culture = null)
2329
{
24-
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
30+
using ICoreScope scope = ScopeProvider.CreateCoreScope();
31+
IRedirectUrl? redir = _redirectUrlRepository.Get(url, contentKey, culture);
32+
if (redir != null)
2533
{
26-
IRedirectUrl? redir = _redirectUrlRepository.Get(url, contentKey, culture);
27-
if (redir != null)
28-
{
29-
redir.CreateDateUtc = DateTime.UtcNow;
30-
}
31-
else
32-
{
33-
redir = new RedirectUrl { Key = Guid.NewGuid(), Url = url, ContentKey = contentKey, Culture = culture };
34-
}
35-
36-
_redirectUrlRepository.Save(redir);
37-
scope.Complete();
34+
redir.CreateDateUtc = DateTime.UtcNow;
3835
}
36+
else
37+
{
38+
redir = new RedirectUrl { Key = Guid.NewGuid(), Url = url, ContentKey = contentKey, Culture = culture };
39+
}
40+
41+
_redirectUrlRepository.Save(redir);
42+
scope.Complete();
3943
}
4044

45+
/// <inheritdoc/>
4146
public void Delete(IRedirectUrl redirectUrl)
4247
{
43-
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
44-
{
45-
_redirectUrlRepository.Delete(redirectUrl);
46-
scope.Complete();
47-
}
48+
using ICoreScope scope = ScopeProvider.CreateCoreScope();
49+
_redirectUrlRepository.Delete(redirectUrl);
50+
scope.Complete();
4851
}
4952

53+
/// <inheritdoc/>
5054
public void Delete(Guid id)
5155
{
52-
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
53-
{
54-
_redirectUrlRepository.Delete(id);
55-
scope.Complete();
56-
}
56+
using ICoreScope scope = ScopeProvider.CreateCoreScope();
57+
_redirectUrlRepository.Delete(id);
58+
scope.Complete();
5759
}
5860

61+
/// <inheritdoc/>
5962
public void DeleteContentRedirectUrls(Guid contentKey)
6063
{
61-
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
62-
{
63-
_redirectUrlRepository.DeleteContentUrls(contentKey);
64-
scope.Complete();
65-
}
64+
using ICoreScope scope = ScopeProvider.CreateCoreScope();
65+
_redirectUrlRepository.DeleteContentUrls(contentKey);
66+
scope.Complete();
6667
}
6768

69+
/// <inheritdoc/>
6870
public void DeleteAll()
6971
{
70-
using (ICoreScope scope = ScopeProvider.CreateCoreScope())
71-
{
72-
_redirectUrlRepository.DeleteAll();
73-
scope.Complete();
74-
}
72+
using ICoreScope scope = ScopeProvider.CreateCoreScope();
73+
_redirectUrlRepository.DeleteAll();
74+
scope.Complete();
7575
}
7676

77+
/// <inheritdoc/>
7778
public IRedirectUrl? GetMostRecentRedirectUrl(string url)
7879
{
79-
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
80-
{
81-
return _redirectUrlRepository.GetMostRecentUrl(url);
82-
}
80+
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
81+
return _redirectUrlRepository.GetMostRecentUrl(url);
8382
}
8483

84+
/// <inheritdoc/>
8585
public async Task<IRedirectUrl?> GetMostRecentRedirectUrlAsync(string url)
8686
{
87-
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
88-
{
89-
return await _redirectUrlRepository.GetMostRecentUrlAsync(url);
90-
}
87+
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
88+
return await _redirectUrlRepository.GetMostRecentUrlAsync(url);
9189
}
9290

91+
/// <inheritdoc/>
9392
public IEnumerable<IRedirectUrl> GetContentRedirectUrls(Guid contentKey)
9493
{
95-
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
96-
{
97-
return _redirectUrlRepository.GetContentUrls(contentKey);
98-
}
94+
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
95+
return _redirectUrlRepository.GetContentUrls(contentKey);
9996
}
10097

98+
/// <inheritdoc/>
10199
public IEnumerable<IRedirectUrl> GetAllRedirectUrls(long pageIndex, int pageSize, out long total)
102100
{
103-
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
104-
{
105-
return _redirectUrlRepository.GetAllUrls(pageIndex, pageSize, out total);
106-
}
101+
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
102+
return _redirectUrlRepository.GetAllUrls(pageIndex, pageSize, out total);
107103
}
108104

105+
/// <inheritdoc/>
109106
public IEnumerable<IRedirectUrl> GetAllRedirectUrls(int rootContentId, long pageIndex, int pageSize, out long total)
110107
{
111-
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
112-
{
113-
return _redirectUrlRepository.GetAllUrls(rootContentId, pageIndex, pageSize, out total);
114-
}
108+
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
109+
return _redirectUrlRepository.GetAllUrls(rootContentId, pageIndex, pageSize, out total);
115110
}
116111

112+
/// <inheritdoc/>
117113
public IEnumerable<IRedirectUrl> SearchRedirectUrls(string searchTerm, long pageIndex, int pageSize, out long total)
118114
{
119-
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
120-
{
121-
return _redirectUrlRepository.SearchUrls(searchTerm, pageIndex, pageSize, out total);
122-
}
115+
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
116+
return _redirectUrlRepository.SearchUrls(searchTerm, pageIndex, pageSize, out total);
123117
}
124118

119+
/// <inheritdoc/>
125120
public IRedirectUrl? GetMostRecentRedirectUrl(string url, string? culture)
126121
{
127122
if (string.IsNullOrWhiteSpace(culture))
128123
{
129124
return GetMostRecentRedirectUrl(url);
130125
}
131126

132-
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
133-
{
134-
return _redirectUrlRepository.GetMostRecentUrl(url, culture);
135-
}
127+
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
128+
return _redirectUrlRepository.GetMostRecentUrl(url, culture);
136129
}
137130

131+
/// <inheritdoc/>
138132
public async Task<IRedirectUrl?> GetMostRecentRedirectUrlAsync(string url, string? culture)
139133
{
140134
if (string.IsNullOrWhiteSpace(culture))
141135
{
142136
return await GetMostRecentRedirectUrlAsync(url);
143137
}
144138

145-
using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true))
146-
{
147-
return await _redirectUrlRepository.GetMostRecentUrlAsync(url, culture);
148-
}
139+
using ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true);
140+
return await _redirectUrlRepository.GetMostRecentUrlAsync(url, culture);
149141
}
150142
}

0 commit comments

Comments
 (0)