Attaching elements

Step 2: Implementing Security for New Root Element

Now we need to fix the security for our new root element. Because of the NoAncestorSecurityAncestorProvider value we need to make a hook from the parent element of our root element to our root element. This takes two steps.

First, we should implement the IAuxiliarySecurityAncestorProvider interface.

public sealed class MyRootEntityTokenAuxiliarySecurityAncestorProvider :
  IAuxiliarySecurityAncestorProvider
{
  public Dictionary<EntityToken, IEnumerable<EntityToken>> 
    GetParents(IEnumerable<EntityToken> entityTokens)
  {
    Dictionary<EntityToken, IEnumerable<EntityToken>> result = 
      new Dictionary<EntityToken, IEnumerable<EntityToken>>();
    foreach (EntityToken entityToken in entityTokens)
    {
      if (entityToken.GetType() == typeof(MyRootEntityToken))
      {
        // Here we specify that the Content perspective element is the parent of our root element
        result.Add(entityToken, new EntityToken[] 
        {
          AttachingPoint.ContentPerspective.EntityToken 
        });
      }
    }
    return result;
  }
}

When C1 CMS runs its security algorithm, it will call SecurityAncestorProvider to get these hooks. In the code above we can see that we return a hook from our root entity token to the Content perspective’s entity token.

Next, we need to register our new MyRootEntityTokenAuxiliarySecurityAncestorProvider class. This can be easily done with an ApplicationStartup handler like this:

[ApplicationStartup]
public static class MyApplicationStartup
{
  public static void OnBeforeInitialize()
  {
  }
  public static void OnInitialized()
  {      
AuxiliarySecurityAncestorFacade.AddAuxiliaryAncestorProvider<MyRootEntityToken>(new MyRootEntityTokenAuxiliarySecurityAncestorProvider());
  }
}

The ApplicationStartup attribute will make C1 CMS call two methods - OnBeforeInitialize and OnInitialized - on its startup. We want to register our new AuxiliarySecurityAncestorProvider after C1 CMS has been initialized. So we put the registering code inside the OnInitialized method.