Cache Sitefinity ISecuredObject models with permissions using SecuredProxy

Permissions for Sitefinity content types are stored in the database. 99% of projects are relying on the out-of-the-box functionality when it comes to permissions. So when each user requests a page, it will query the DB to filter the content items on the page by the user’s permissions. There’s nothing wrong with this, but some projects may be able to benefit from another approach. This approach makes use of the SecuredProxy class. The idea with this approach is that you cache each content item in memory with its permissions. The benefit of course, is increased performance. To do this, you will have to customize your widgets so they use your own lookup class.

Obviously, if you have 100,000 products with permissions set for 100 different roles, you wouldn’t want to cache these in memory – so use your best judgement with this. I recently used this in very large project for caching permissions-based categories (not taxonomies – these were custom content types built in code, not module builder). Clicking through to our “detail pages” is extremely fast, and tailored to the user’s permissions.

Here’s how to use the SecuredProxy class…

    public class SecuredEventProxy : ISecuredObject
    {
        public SecuredEventProxy(Event e) : base()
        {
            this.Id = e.Id;
            this.Title = e.Title;

            // Add any other fields here that you want to add to the cached model

            this.securedProxy = new SecuredProxy(e);
        }

        public Guid Id { get; private set; }
        public string Title { get; private set; }

        private readonly SecuredProxy securedProxy;

        #region ISecuredObject

        public bool InheritsPermissions
        {
            get
            {
                return this.securedProxy.InheritsPermissions;
            }
            set
            {
                this.securedProxy.InheritsPermissions = value;
            }
        }

        public bool CanInheritPermissions
        {
            get
            {
                return this.securedProxy.CanInheritPermissions;
            }
            set
            {
                this.securedProxy.CanInheritPermissions = value;
            }
        }

        public IList<Telerik.Sitefinity.Security.Model.Permission> Permissions
        {
            get
            {
                return securedProxy.Permissions;
            }
        }

        public string[] SupportedPermissionSets
        {
            get
            {
                return this.securedProxy.SupportedPermissionSets;
            }
            set
            {
                this.securedProxy.SupportedPermissionSets = value;
            }
        }

        public IDictionary<string, string> PermissionsetObjectTitleResKeys
        {
            get
            {
                return this.securedProxy.PermissionsetObjectTitleResKeys;
            }
            set
            {
                this.securedProxy.PermissionsetObjectTitleResKeys = value;
            }
        }

        #endregion

If you want to see this in action within Sitefinity, use JustDecompile to dig deeper in one of these classes:

  • Telerik.Sitefinity.DynamicModules.Builder.Proxy.DynamicModuleProxy
  • Telerik.Sitefinity.Multisite.MultisiteContext.SiteProxy

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *