Skip to content

Instantly share code, notes, and snippets.

@TimGeyssens
Last active September 9, 2020 15:00
Show Gist options
  • Select an option

  • Save TimGeyssens/5e4e2a0cca5821fb69b17fc49e32e85a to your computer and use it in GitHub Desktop.

Select an option

Save TimGeyssens/5e4e2a0cca5821fb69b17fc49e32e85a to your computer and use it in GitHub Desktop.

Revisions

  1. TimGeyssens renamed this gist Sep 9, 2020. 1 changed file with 3 additions and 9 deletions.
    12 changes: 3 additions & 9 deletions CustomComponent.cs → CustomEventComponent.cs
    Original file line number Diff line number Diff line change
    @@ -17,16 +17,10 @@ namespace MySite.Custom
    {

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class CustomComposer : IUserComposer
    {
    public void Compose(Composition composition)
    {

    composition.Components().Append<CustomComponent>();
    }
    }
    public class CustomEventComposer : ComponentComposer<CustomEventComponent>
    { }

    public class CustomComponent : IComponent
    public class CustomEventComponent : IComponent
    {
    public void Initialize()
    {
  2. TimGeyssens revised this gist Sep 9, 2020. 1 changed file with 2 additions and 11 deletions.
    13 changes: 2 additions & 11 deletions CustomComponent.cs
    Original file line number Diff line number Diff line change
    @@ -32,19 +32,10 @@ public void Initialize()
    {

    EditorModelEventManager.SendingContentModel += EditorModelEventManager_SendingContentModel;
    ContentService.Deleting += ContentService_Deleting;


    }

    private void ContentService_Deleting(IContentService sender, DeleteEventArgs<Umbraco.Core.Models.IContent> e)
    {
    foreach(var entity in e.DeletedEntities)
    {
    if (entity.GetValue<bool>(AdminSettings.GetModelPropertyType(d => d.DontDelete).Alias))
    e.Cancel = true;
    }
    }


    private void EditorModelEventManager_SendingContentModel(System.Web.Http.Filters.HttpActionExecutedContext sender, EditorModelEventArgs<Umbraco.Web.Models.ContentEditing.ContentItemDisplay> e)
    {
    var identity = (UmbracoBackOfficeIdentity)System.Web.HttpContext.Current.User.Identity;
  3. TimGeyssens revised this gist Sep 9, 2020. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions CustomComponent.cs
    Original file line number Diff line number Diff line change
    @@ -47,14 +47,12 @@ private void ContentService_Deleting(IContentService sender, DeleteEventArgs<Umb

    private void EditorModelEventManager_SendingContentModel(System.Web.Http.Filters.HttpActionExecutedContext sender, EditorModelEventArgs<Umbraco.Web.Models.ContentEditing.ContentItemDisplay> e)
    {
    var identity = (UmbracoBackOfficeIdentity)System.Web.HttpContext.Current.User.Identity;
    var currentUSer = Current.Services.UserService.GetByProviderKey(identity.Id);
    var identity = (UmbracoBackOfficeIdentity)System.Web.HttpContext.Current.User.Identity;
    var currentUSer = Current.Services.UserService.GetByProviderKey(identity.Id);

    var canSeeAdminTab = currentUSer.Groups.Any(x => x.Alias == "admin");

    foreach (var variant in e.Model.Variants)
    if (!currentUSer.Groups.Any(x => x.Alias == "admin"))
    {
    if (!canSeeAdminTab)
    foreach (var variant in e.Model.Variants)
    variant.Tabs = variant.Tabs.Where(x => x.Label != "Admin");
    }
    }
  4. TimGeyssens created this gist Sep 9, 2020.
    67 changes: 67 additions & 0 deletions CustomComponent.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    using System.Web.Security;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Core.Events;
    using Umbraco.Core.Scoping;
    using Umbraco.Core.Services;
    using Umbraco.Core.Services.Implement;
    using System.Linq;
    using System;
    using Umbraco.Core.Security;
    using Umbraco.Web.Editors;
    using Umbraco.Web;
    using Umbraco.Web.Dashboards;
    using Umbraco.Web.PublishedModels;

    namespace MySite.Custom
    {

    [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
    public class CustomComposer : IUserComposer
    {
    public void Compose(Composition composition)
    {

    composition.Components().Append<CustomComponent>();
    }
    }

    public class CustomComponent : IComponent
    {
    public void Initialize()
    {

    EditorModelEventManager.SendingContentModel += EditorModelEventManager_SendingContentModel;
    ContentService.Deleting += ContentService_Deleting;

    }

    private void ContentService_Deleting(IContentService sender, DeleteEventArgs<Umbraco.Core.Models.IContent> e)
    {
    foreach(var entity in e.DeletedEntities)
    {
    if (entity.GetValue<bool>(AdminSettings.GetModelPropertyType(d => d.DontDelete).Alias))
    e.Cancel = true;
    }
    }

    private void EditorModelEventManager_SendingContentModel(System.Web.Http.Filters.HttpActionExecutedContext sender, EditorModelEventArgs<Umbraco.Web.Models.ContentEditing.ContentItemDisplay> e)
    {
    var identity = (UmbracoBackOfficeIdentity)System.Web.HttpContext.Current.User.Identity;
    var currentUSer = Current.Services.UserService.GetByProviderKey(identity.Id);

    var canSeeAdminTab = currentUSer.Groups.Any(x => x.Alias == "admin");

    foreach (var variant in e.Model.Variants)
    {
    if (!canSeeAdminTab)
    variant.Tabs = variant.Tabs.Where(x => x.Label != "Admin");
    }
    }

    public void Terminate()
    {

    }
    }
    }