Last active
June 24, 2020 06:57
-
-
Save dannydrogt/7450807e81e552704d71 to your computer and use it in GitHub Desktop.
Filter Allowed Document Types and Templates in Umbraco
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is downright amazing. So happy that I stumbled on this!