Skip to content

Commit

Permalink
Added support for disabling the content app through appsettings.json
Browse files Browse the repository at this point in the history
Implements #160
  • Loading branch information
abjerner committed Nov 5, 2022
1 parent 10610e7 commit ab3127f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/Skybrud.Umbraco.Redirects/Composers/RedirectsComposer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Skybrud.Umbraco.Redirects.Config;
using Skybrud.Umbraco.Redirects.ContentApps;
using Skybrud.Umbraco.Redirects.Factories.References;
using Skybrud.Umbraco.Redirects.Helpers;
Expand All @@ -21,6 +22,10 @@ public class RedirectsComposer : IComposer {

public void Compose(IUmbracoBuilder builder) {

builder.Services.AddOptions<RedirectsSettings>()
.Bind(builder.Config.GetSection("Skybrud:Redirects"), o => o.BindNonPublicProperties = true)
.ValidateDataAnnotations();

builder.Services.AddSingleton<RedirectsServiceDependencies>();
builder.Services.AddSingleton<RedirectsBackOfficeHelperDependencies>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Skybrud.Umbraco.Redirects.Config {

/// <summary>
/// Class with settings for the redirects content app.
/// </summary>
public class RedirectsContentAppSettings {

/// <summary>
/// Gets or sets whether the content app should be enabled. Default is <see langword="true"/>.
/// </summary>
public bool Enabled { get; set; } = true;

}

}
15 changes: 15 additions & 0 deletions src/Skybrud.Umbraco.Redirects/Config/RedirectsSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Skybrud.Umbraco.Redirects.Config {

/// <summary>
/// Class with settings for the redirects package.
/// </summary>
public class RedirectsSettings {

/// <summary>
/// Gets settings for the redirects content app.
/// </summary>
public RedirectsContentAppSettings ContentApp { get; private set; } = new();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Skybrud.Essentials.Reflection;
using Skybrud.Umbraco.Redirects.Config;
using Skybrud.Umbraco.Redirects.Dashboards;
using Skybrud.Umbraco.Redirects.Models;
using Skybrud.Umbraco.Redirects.Models.Api;
Expand Down Expand Up @@ -32,6 +33,11 @@ public class RedirectsBackOfficeHelper {
/// </summary>
public string BackOfficeUrl => Dependencies.GlobalSettings.GetBackOfficePath(Dependencies.HostingEnvironment);

/// <summary>
/// Gets a reference to the redirects settings.
/// </summary>
public RedirectsSettings Settings => Dependencies.RedirectsSettings.Value;

#endregion

#region Constructors
Expand Down Expand Up @@ -240,7 +246,10 @@ public class RedirectsBackOfficeHelper {
/// <param name="source">The source - eg. an instance <see cref="IContent"/>.</param>
/// <param name="userGroups">The user groups of the current user.</param>
/// <returns>An instance of <see cref="ContentApp"/>, or <c>null</c> if no content app should be shown.</returns>
public virtual ContentApp GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups) {
public virtual ContentApp GetContentAppFor(object source, IEnumerable<IReadOnlyUserGroup> userGroups) {

// Return null if the content app is disabled via appsettings.json
if (!Settings.ContentApp.Enabled) return null;

switch (source) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Options;
using Skybrud.Umbraco.Redirects.Config;
using Umbraco.Cms.Core.Configuration.Models;
using Umbraco.Cms.Core.Hosting;
using Umbraco.Cms.Core.Services;
Expand Down Expand Up @@ -47,6 +48,11 @@ public class RedirectsBackOfficeHelperDependencies {
/// </summary>
public ILocalizedTextService TextService { get; }

/// <summary>
/// Gets a reference to the options for the redirects page.
/// </summary>
public IOptions<RedirectsSettings> RedirectsSettings { get; }

#endregion

#region Constructors
Expand All @@ -61,16 +67,18 @@ public class RedirectsBackOfficeHelperDependencies {
/// <param name="mediaService"></param>
/// <param name="textService"></param>
/// <param name="globalSettings"></param>
/// <param name="redirectsSettings"></param>
public RedirectsBackOfficeHelperDependencies(IOptions<GlobalSettings> globalSettings, IHostingEnvironment hostingEnvironment,
IRuntimeState runtimeState, IDomainService domainService, IContentService contentService,
IMediaService mediaService, ILocalizedTextService textService) {
IMediaService mediaService, ILocalizedTextService textService, IOptions<RedirectsSettings> redirectsSettings) {
GlobalSettings = globalSettings.Value;
HostingEnvironment = hostingEnvironment;
RuntimeState = runtimeState;
DomainService = domainService;
ContentService = contentService;
MediaService = mediaService;
TextService = textService;
RedirectsSettings = redirectsSettings;
}

#endregion
Expand Down

0 comments on commit ab3127f

Please sign in to comment.