2018-12-07 14:05:47 +01:00
|
|
|
|
using System.Collections.Generic;
|
2018-09-20 17:22:39 +02:00
|
|
|
|
using System.Linq;
|
2020-09-21 13:04:57 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.Composing;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models.ContentEditing;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models.Membership;
|
|
|
|
|
|
using Umbraco.Cms.Core.Security;
|
|
|
|
|
|
using Umbraco.Extensions;
|
2018-09-20 17:22:39 +02:00
|
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
|
namespace Umbraco.Cms.Core.ContentApps
|
2018-09-20 17:22:39 +02:00
|
|
|
|
{
|
2018-12-07 14:05:47 +01:00
|
|
|
|
public class ContentAppFactoryCollection : BuilderCollectionBase<IContentAppFactory>
|
2018-09-20 17:22:39 +02:00
|
|
|
|
{
|
2020-09-21 13:04:57 +02:00
|
|
|
|
private readonly ILogger<ContentAppFactoryCollection> _logger;
|
2020-11-24 12:52:48 +01:00
|
|
|
|
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
|
2018-09-24 13:50:42 +10:00
|
|
|
|
|
2020-11-24 12:52:48 +01:00
|
|
|
|
public ContentAppFactoryCollection(IEnumerable<IContentAppFactory> items, ILogger<ContentAppFactoryCollection> logger, IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
|
2018-09-20 17:22:39 +02:00
|
|
|
|
: base(items)
|
2018-09-24 13:50:42 +10:00
|
|
|
|
{
|
2018-09-24 09:34:25 +02:00
|
|
|
|
_logger = logger;
|
2020-11-24 12:52:48 +01:00
|
|
|
|
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
|
2018-09-24 13:50:42 +10:00
|
|
|
|
}
|
2018-09-20 17:22:39 +02:00
|
|
|
|
|
2018-10-16 15:05:17 +02:00
|
|
|
|
private IEnumerable<IReadOnlyUserGroup> GetCurrentUserGroups()
|
|
|
|
|
|
{
|
2020-11-24 12:52:48 +01:00
|
|
|
|
var currentUser = _backOfficeSecurityAccessor?.BackOfficeSecurity?.CurrentUser;
|
2018-10-16 15:05:17 +02:00
|
|
|
|
return currentUser == null
|
|
|
|
|
|
? Enumerable.Empty<IReadOnlyUserGroup>()
|
|
|
|
|
|
: currentUser.Groups;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-14 15:56:22 +01:00
|
|
|
|
public IEnumerable<ContentApp> GetContentAppsFor(object o, IEnumerable<IReadOnlyUserGroup> userGroups=null)
|
2018-09-20 17:22:39 +02:00
|
|
|
|
{
|
2018-10-16 15:05:17 +02:00
|
|
|
|
var roles = GetCurrentUserGroups();
|
2018-12-06 10:02:23 +01:00
|
|
|
|
|
2018-12-06 11:02:35 +01:00
|
|
|
|
var apps = this.Select(x => x.GetContentAppFor(o, roles)).WhereNotNull().OrderBy(x => x.Weight).ToList();
|
2018-12-06 10:02:23 +01:00
|
|
|
|
|
2018-09-24 09:34:25 +02:00
|
|
|
|
var aliases = new HashSet<string>();
|
2018-09-24 13:50:42 +10:00
|
|
|
|
List<string> dups = null;
|
2018-09-24 09:34:25 +02:00
|
|
|
|
|
|
|
|
|
|
foreach (var app in apps)
|
2018-09-24 13:50:42 +10:00
|
|
|
|
{
|
2018-09-24 09:34:25 +02:00
|
|
|
|
if (aliases.Contains(app.Alias))
|
|
|
|
|
|
(dups ?? (dups = new List<string>())).Add(app.Alias);
|
2018-09-24 13:50:42 +10:00
|
|
|
|
else
|
2018-09-24 09:34:25 +02:00
|
|
|
|
aliases.Add(app.Alias);
|
2018-09-24 13:50:42 +10:00
|
|
|
|
}
|
2018-09-24 09:34:25 +02:00
|
|
|
|
|
2018-09-24 13:50:42 +10:00
|
|
|
|
if (dups != null)
|
|
|
|
|
|
{
|
2018-09-24 09:34:25 +02:00
|
|
|
|
// dying is not user-friendly, so let's write to log instead, and wish people read logs...
|
|
|
|
|
|
|
|
|
|
|
|
//throw new InvalidOperationException($"Duplicate content app aliases found: {string.Join(",", dups)}");
|
2020-09-16 09:58:07 +02:00
|
|
|
|
_logger.LogWarning("Duplicate content app aliases found: {DuplicateAliases}", string.Join(",", dups));
|
2018-09-24 13:50:42 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-09-24 09:34:25 +02:00
|
|
|
|
return apps;
|
2018-09-20 17:22:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|