Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Johan Björnfot
Mar 10, 2011
  6382
(2 votes)

Include ACL during Import, Mirroring and Copy

The standard behavior for Copy, Import and Mirroring is that the AccessControlList (ACL) is inherited from destination page. However the original ACL that the page had at source is included in the export package. In this post I will present a module that will set the ACL to the original value it had instead of inheriting from  destination.

The implementation does not distinguish between the type of importing (Copy, Import or Mirroring). It is possible to distinguish on type if you for example only want ACLs to be preserved during mirroring but not for copy and import.

In this implementation I call importedAcl.Save(SecuritySaveType.Replace); which will replace the inherited ACL. Another option is to call importedAcl.Save(SecuritySaveType.Modify); which will be a merge of the inherited and the imported ACL.

In CMS6 and prior versions the type of entity in the ACE (User or Role) was not included in the package. In that case on import side a check is made if there exist a Role with given name and if so the type is set to Role else it is considered a User. This has been changed in CMS6R2 so the type of entity (now User, Role or VisitorGroup) is included in the package.

The code holds the standard “works on my machine”, that is it has not gone through any solid testing matrix. You are free to use it at own risk without support, however feedback is appreciated.

   [InitializableModule]
    public class AclImporterModule : IInitializableModule
    {
        //The module will be a singleton so it needs to be thread safe
        private object _lockObject = new object();
        private Dictionary<DataImporter, AclImporter> _aclImporters = 
            new Dictionary<DataImporter, AclImporter>();

        public void Initialize(InitializationEngine context)
        {
            DataImporter.Importing += new EventHandler(DataImporter_Importing);
            DataImporter.Imported += new EventHandler(DataImporter_Imported);
            DataImporter.PageImporting += 
                new PageImportingEventHandler(DataImporter_PageImporting);
            DataImporter.PageImported += 
                new PageImportedEventHandler(DataImporter_PageImported);
        }

        void DataImporter_Importing(object sender, EventArgs e)
        {
            lock (_lockObject)
            {
                _aclImporters.Add(sender as DataImporter, 
                    new AclImporter(sender as ITransferContext, 
                        new PermanentLinkMapper()));
            }
        }

        void DataImporter_Imported(object sender, EventArgs e)
        {
            lock (_lockObject)
            {
                _aclImporters.Remove(sender as DataImporter);
            }
        }

        void DataImporter_PageImporting(DataImporter dataImporter, 
            PageImportingEventArgs e)
        {
            AclImporter aclImporter;
            lock (_lockObject)
            {
                aclImporter = _aclImporters[dataImporter];
            }
            aclImporter.ImportingPage(e.RawPage);
            
        }

        void DataImporter_PageImported(DataImporter dataImporter, 
            PageImportedEventArgs e)
        {
            AclImporter aclImporter;
            lock (_lockObject)
            {
                aclImporter = _aclImporters[dataImporter];
            }
            aclImporter.ImportedPage(e.PageData);
        }

        public void Preload(string[] parameters)
        {
        }

        public void Uninitialize(InitializationEngine context)
        {
            DataImporter.Importing -= new EventHandler(DataImporter_Importing);
            DataImporter.Imported -= new EventHandler(DataImporter_Imported);
            DataImporter.PageImporting -= 
                new PageImportingEventHandler(DataImporter_PageImporting);
            DataImporter.PageImported -= 
                new PageImportedEventHandler(DataImporter_PageImported);
        }
    }

    public class AclImporter
    {
        private Dictionary<Guid, RawACE[]> _importedAce = new Dictionary<Guid, RawACE[]>();
        private ITransferContext _context;
        private IPermanentLinkMapper _mapper;

        public AclImporter(ITransferContext context, IPermanentLinkMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        internal void ImportingPage(EPiServer.Core.RawPage rawPage)
        {
            _importedAce[GetPageGuid(rawPage)] = rawPage.ACL;
        }

        internal void ImportedPage(EPiServer.Core.PageData pageData)
        {
            //Only set ACL for "local" pages
            if (!pageData.PageLink.IsRemote())
            {
                //Note: We can not use pageData.PageGuid here since 
                //PageData instance is not complete
                PermanentPageLinkMap pageMap = 
                    _mapper.Find(pageData.PageLink) as PermanentPageLinkMap;
                Guid oldPageGuid = 
                    _context.LinkGuidMap.First(pair => pair.Value == pageMap.Guid).Key;
                RawACE[] aces = _importedAce[oldPageGuid];
                PageAccessControlList importedAcl = 
                    new PageAccessControlList(aces){PageLink = pageData.PageLink};
                importedAcl.Save(SecuritySaveType.Replace);
            }
            
        }

        private Guid GetPageGuid(EPiServer.Core.RawPage rawPage)
        {
            return new Guid(rawPage.Property.Where
                (prop => prop.Name == "PageGUID").Single().Value);
        }
    }
Mar 10, 2011

Comments

Clif Render
Clif Render Jan 16, 2015 06:18 PM

Any chance you have an EPiServer 7 version of this handy?

Please login to comment.
Latest blogs
Optimizely CMS easy RSS feed integration library

As I've mentioned in my  previous blog post , while I was developing the Optimizely version of my blog, I tried to look for a library that could...

David Drouin-Prince | Jan 25, 2025 | Syndicated blog

Decimal numbers in Optimizely Graph

Storing prices as decimal numbers on a commerce website and planning to expose them through Optimizely Graph? It might not be as straightforward as...

Damian Smutek | Jan 23, 2025 | Syndicated blog

Find and delete non used media and blocks

On my new quest to play around with Blazor and MudBlazor I'm going back memory lane and porting some previously plugins. So this time up is my plug...

Per Nergård (MVP) | Jan 21, 2025

Optimizely Content Graph on mobile application

CG everywhere! I pull schema from our default index https://cg.optimizely.com/app/graphiql?auth=eBrGunULiC5TziTCtiOLEmov2LijBf30obh0KmhcBlyTktGZ in...

Cuong Nguyen Dinh | Jan 20, 2025

Image Analyzer with AI Assistant for Optimizely

The Smart Image Analyzer is a new feature in the Epicweb AI Assistant for Optimizely CMS that automates the management of image metadata, such as...

Luc Gosso (MVP) | Jan 16, 2025 | Syndicated blog

How to: create Decimal metafield with custom precision

If you are using catalog system, the way of creating metafields are easy – in fact, you can forget about “metafields”, all you should be using is t...

Quan Mai | Jan 16, 2025 | Syndicated blog