Skip to content

Commit

Permalink
Improved the logic around content apps
Browse files Browse the repository at this point in the history
See #161
  • Loading branch information
abjerner committed Nov 10, 2022
1 parent d4a53c8 commit f8274c7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Skybrud.Umbraco.Redirects.Config {
using System.Collections.Generic;

namespace Skybrud.Umbraco.Redirects.Config {

/// <summary>
/// Class with settings for the redirects content app.
Expand All @@ -10,6 +12,15 @@ public class RedirectsContentAppSettings {
/// </summary>
public bool Enabled { get; set; } = true;

/// <summary>
/// Gets or sets a list of content types and media types where the content app should or should not be shown.
/// The format follows Umbraco's <c>show</c> option - eg. <c>+content/*</c> enables the content app for all
/// content.
///
/// If empty, the content app will be enabled for all content and media.
/// </summary>
public HashSet<string> Show { get; set; } = new();

}

}
66 changes: 58 additions & 8 deletions src/Skybrud.Umbraco.Redirects/Helpers/RedirectsBackOfficeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,18 +256,68 @@ public class RedirectsBackOfficeHelper {
// Return null if the content app is disabled via appsettings.json
if (!Settings.ContentApp.Enabled) return null;

switch (source) {
return source switch {
IContent content => ShowContentApp(content, userGroups) ? GetDefaultContentApp() : null,
IMedia media => ShowContentApp(media, userGroups) ? GetDefaultContentApp() : null,
_ => null
};

case IContent content:
return content.ContentType.IsElement ? null : GetDefaultContentApp();
}

case IMedia media:
return media.ContentType.Alias == Constants.Conventions.MediaTypes.Folder ? null : GetDefaultContentApp();
/// <summary>
/// Returns whether to show the redirects content app for the specified <paramref name="content"/>.
/// </summary>
/// <param name="content">An instance of <see cref="IContent"/>.</param>
/// <param name="userGroups">The user groups of the current user.</param>
/// <returns><see langword="true"/> if the content app should be shown; otherwise, <see langword="false"/>.</returns>
protected virtual bool ShowContentApp(IContent content, IEnumerable<IReadOnlyUserGroup> userGroups) {

default:
return null;
if (content.ContentType.IsElement) return false;
if (content.TemplateId == null) return false;

}
IReadOnlySet<string> show = Settings.ContentApp.Show;

// ReSharper disable PossibleMultipleEnumeration
// The underlying type is likely always HashSet<IReadOnlyUserGroup>, so we should care too much about multiple enumerations
if (userGroups.Any(group => show.Contains("+role/" + group.Alias))) return true;
if (userGroups.Any(group => show.Contains("-role/" + group.Alias))) return false;
// ReSharper restore PossibleMultipleEnumeration

if (show.Contains("-content/" + content.ContentType.Alias)) return false;
if (show.Contains("+content/" + content.ContentType.Alias)) return true;

if (show.Contains("-content/*")) return false;
if (show.Contains("+content/*")) return true;

return true;

}

/// <summary>
/// Returns whether to show the redirects content app for the specified <paramref name="media"/>.
/// </summary>
/// <param name="media">An instance of <see cref="IMedia"/>.</param>
/// <param name="userGroups">The user groups of the current user.</param>
/// <returns><see langword="true"/> if the content app should be shown; otherwise, <see langword="false"/>.</returns>
protected virtual bool ShowContentApp(IMedia media, IEnumerable<IReadOnlyUserGroup> userGroups) {

if (media.ContentType.Alias == Constants.Conventions.MediaTypes.Folder) return false;

IReadOnlySet<string> show = Settings.ContentApp.Show;

// ReSharper disable PossibleMultipleEnumeration
// The underlying type is likely always HashSet<IReadOnlyUserGroup>, so we should care too much about multiple enumerations
if (userGroups.Any(group => show.Contains("+role/" + group.Alias))) return true;
if (userGroups.Any(group => show.Contains("-role/" + group.Alias))) return false;
// ReSharper restore PossibleMultipleEnumeration

if (show.Contains("-media/" + media.ContentType.Alias)) return false;
if (show.Contains("+media/" + media.ContentType.Alias)) return true;

if (show.Contains("-media/*")) return false;
if (show.Contains("+media/*")) return true;

return true;

}

Expand Down

0 comments on commit f8274c7

Please sign in to comment.