filtering out tours.

This commit is contained in:
Claus
2018-01-09 15:49:19 +01:00
parent ea70e18a91
commit a5ec5f46ac
6 changed files with 111 additions and 13 deletions

View File

@@ -548,6 +548,7 @@
<DependentUpon>HealthChecks.config</DependentUpon>
<SubType>Designer</SubType>
</None>
<Content Include="Config\BackOfficeTours\my-tours.json" />
<None Include="Config\umbracoSettings.Release.config">
<DependentUpon>umbracoSettings.config</DependentUpon>
<SubType>Designer</SubType>

View File

@@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.IO;
using Umbraco.Core.Logging;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
using Umbraco.Web.WebApi.Filters;
using Constants = Umbraco.Core.Constants;
@@ -11,29 +15,46 @@ using Constants = Umbraco.Core.Constants;
namespace Umbraco.Web.Editors
{
[PluginController("UmbracoApi")]
[UmbracoApplicationAuthorize(Constants.Applications.Content)]
[UmbracoApplicationAuthorize(Constants.Applications.Content)]
public class TourController : UmbracoAuthorizedJsonController
{
//TODO: Strongly type this for final release!
public JArray GetTours()
public IEnumerable<Tour[]> GetTours()
{
//TODO: Add error checking to this for final release!
var result = new JArray();
var tours = new List<Tour[]>();
if (UmbracoConfig.For.UmbracoSettings().BackOffice.Tours.EnableTours == false)
return result;
return tours;
var tourFiles = Directory.GetFiles(
Path.Combine(IOHelper.MapPath(SystemDirectories.Config), "BackOfficeTours"), "*.json")
var toursPath = Path.Combine(IOHelper.MapPath(SystemDirectories.Config), "BackOfficeTours");
if (Directory.Exists(toursPath) == false)
return tours;
var tourFiles = Directory.GetFiles(toursPath, "*.json")
.OrderBy(x => x, StringComparer.InvariantCultureIgnoreCase);
var disabledTours = TourFilterResolver.Current.DisabledTours;
foreach (var tourFile in tourFiles)
{
var contents = File.ReadAllText(tourFile);
result.Add(JArray.Parse(contents));
try
{
var contents = File.ReadAllText(tourFile);
var tourArray = JsonConvert.DeserializeObject<Tour[]>(contents);
tours.Add(tourArray.Where(x =>
disabledTours.Contains(x.Alias, StringComparer.InvariantCultureIgnoreCase) == false).ToArray());
}
catch (IOException e)
{
Logger.Error<TourController>("Error while trying to read file: " + tourFile, e);
throw new IOException("Error while trying to read file: " + tourFile, e);
}
catch (JsonReaderException e)
{
Logger.Error<TourController>("Error while trying to parse content as tour data: " + tourFile, e);
throw new JsonReaderException("Error while trying to parse content as tour data: " + tourFile, e);
}
}
return result;
return tours;
}
}
}

View File

@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.Linq;
namespace Umbraco.Web.Editors
{
//TODO: find out where this should live
public class TourFilterResolver
{
private static TourFilterResolver _current;
private readonly HashSet<string> _disabledTours;
public TourFilterResolver()
{
_disabledTours = new HashSet<string>();
}
public static TourFilterResolver Current
{
get { return _current ?? (_current = new TourFilterResolver()); }
}
public void Disable(string tour)
{
_disabledTours.Add(tour);
}
public IEnumerable<string> DisabledTours
{
get { return _disabledTours.ToArray(); }
}
}
}

View File

@@ -0,0 +1,22 @@
using System.Runtime.Serialization;
namespace Umbraco.Web.Models
{
/// <summary>
/// A model representing a tour.
/// </summary>
[DataContract(Name = "tour", Namespace = "")]
public class Tour
{
[DataMember(Name = "name")]
public string Name { get; set; }
[DataMember(Name = "alias")]
public string Alias { get; set; }
[DataMember(Name = "group")]
public string Group { get; set; }
[DataMember(Name = "groupOrder")]
public int GroupOrder { get; set; }
[DataMember(Name = "steps")]
public TourStep[] Steps { get; set; }
}
}

View File

@@ -0,0 +1,18 @@
using System.Runtime.Serialization;
namespace Umbraco.Web.Models
{
/// <summary>
/// A model representing a step in a tour.
/// </summary>
[DataContract(Name = "step", Namespace = "")]
public class TourStep
{
[DataMember(Name = "title")]
public string Title { get; set; }
[DataMember(Name = "content")]
public string Content { get; set; }
[DataMember(Name = "type")]
public string Type { get; set; }
}
}

View File

@@ -328,6 +328,7 @@
<Compile Include="Editors\ParameterSwapControllerActionSelector.cs" />
<Compile Include="Editors\CodeFileController.cs" />
<Compile Include="Editors\TourController.cs" />
<Compile Include="Editors\TourFilterResolver.cs" />
<Compile Include="Editors\UserEditorAuthorizationHelper.cs" />
<Compile Include="Editors\UserGroupAuthorizationAttribute.cs" />
<Compile Include="Editors\UserGroupEditorAuthorizationHelper.cs" />
@@ -428,6 +429,8 @@
<Compile Include="Models\SetPasswordModel.cs" />
<Compile Include="Models\RequestPasswordResetModel.cs" />
<Compile Include="Models\PublishedContentWithKeyBase.cs" />
<Compile Include="Models\Tour.cs" />
<Compile Include="Models\TourStep.cs" />
<Compile Include="Models\UserTourStatus.cs" />
<Compile Include="Models\ValidatePasswordResetCodeModel.cs" />
<Compile Include="Mvc\ControllerContextExtensions.cs" />