Skip to content

Instantly share code, notes, and snippets.

@dannydrogt
Last active June 24, 2020 06:57
Show Gist options
  • Save dannydrogt/7450807e81e552704d71 to your computer and use it in GitHub Desktop.
Save dannydrogt/7450807e81e552704d71 to your computer and use it in GitHub Desktop.
Filter Allowed Document Types and Templates in Umbraco
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using Umbraco.Core;
using Umbraco.Web;
using Umbraco.Web.Models.ContentEditing;
namespace WebApplication2.App_Code
{
public class MyApplication : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
System.Web.Http.GlobalConfiguration.Configuration.MessageHandlers.Add(new MyApiHandler());
}
}
public class MyApiHandler : System.Net.Http.DelegatingHandler
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var user = UmbracoContext.Current.Security.CurrentUser;
switch (request.RequestUri.AbsolutePath.ToLower())
{
case "/umbraco/backoffice/umbracoapi/contenttype/getallowedchildren":
return FilterAllowedContentTypes(request, cancellationToken, user);
case "/umbraco/backoffice/umbracoapi/content/getbyid":
return FilterAllowedTemplates(request, cancellationToken, user);
default:
return base.SendAsync(request, cancellationToken);
}
}
private Task<HttpResponseMessage> FilterAllowedTemplates(HttpRequestMessage request, CancellationToken cancellationToken, Umbraco.Core.Models.Membership.IUser user)
{
return base.SendAsync(request, cancellationToken)
.ContinueWith(task =>
{
var response = task.Result;
try
{
var data = response.Content;
var content = ((ObjectContent)(data)).Value as ContentItemDisplay;
var templates = content.Properties.FirstOrDefault(x => x.Alias == "_umb_template");
if (templates != null && templates.Config.ContainsKey("items"))
{
var allowedTemplates = ((Dictionary<string, string>)templates.Config["items"]);
// Replace this with your filtering logic
if (user.Name == "Danny Drogt")
{
if (allowedTemplates.ContainsKey("umbTextPage"))
{
allowedTemplates.Remove("umbTextPage");
}
}
}
}
catch (Exception ex)
{
// Log
}
return response;
});
}
private Task<HttpResponseMessage> FilterAllowedContentTypes(HttpRequestMessage request, CancellationToken cancellationToken, Umbraco.Core.Models.Membership.IUser user)
{
return base.SendAsync(request, cancellationToken)
.ContinueWith(task =>
{
var response = task.Result;
try
{
var data = response.Content;
var allowedTemplates = ((ObjectContent)(data)).Value as IList<ContentTypeBasic>;
if (allowedTemplates != null)
{
// Replace this with your filtering logic
if (user.Name == "Danny Drogt")
{
allowedTemplates.Clear();
}
}
}
catch (Exception ex)
{
// Log
}
return response;
});
}
}
}
@StephenPAdams
Copy link

This is downright amazing. So happy that I stumbled on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment