Attaching elements

Step 8 - Adding Elements That Represent Data

This time we will add elements that represent data (pages), and in C1 CMS all data items have their own entity token (DataEntityToken), so we don’t need an extra class for that. So the only thing we need to do in order to get security in place is to add hooks from our two child elements to the pages that are going to be children of those two elements. We do that with a new IAuxiliarySecurityAncestorProvider implementation

public sealed class MyDataEntityTokenAuxiliarySecurityAncestorProvider : 
  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)
    {
      DataEntityToken dataEntityToken = entityToken as DataEntityToken;
      if (dataEntityToken.Data == null) continue;
      Type interfaceType = TypeManager.GetType(dataEntityToken.Type);
      if (interfaceType != typeof(IPage)) continue;
      IPage page = dataEntityToken.Data as IPage;
      if (page.PublicationStatus == "awaitingApproval")
      {
        result.Add(entityToken, new MyEntityToken[] { new MyEntityToken("Approval") });
      }
      else if (page.PublicationStatus == "awaitingPublication")
      {
        result.Add(entityToken, new MyEntityToken[] { new MyEntityToken("Publication") });
      }
    }
    return result;
  }
}

Some notes on this implementation. First, we do some checks to see that it is actually a DataEntityToken for a page that we have. Then we get the page and check the publication status for that given page. If it is awaiting approval we say that it has a hook from our child element with the entity token with the constructed value of “Approval” as mentioned in Step 7. The same goes for pages awaiting publication. If the page is in draft or published we do nothing, i.e. no hook exists. With this code we have told C1 CMS that all the pages in the “awaiting approval” state are a hook from our child element (“Pages awaiting approval”) with the constructed value of “Approval”. So are the pages awaiting publication.