More in this section

Forums / Developing with Sitefinity / How to add new permission?

How to add new permission?

4 posts, 0 answered
  1. bemara57
    bemara57 avatar
    135 posts
    Registered:
    27 Mar 2008
    31 Mar 2010
    Link to this post
    Currently the permissions support "View", "Create", "Modify", "Delete", and "ChangePermissions". I want to add a new permission called "View Unauthorized" with the usual allow/deny options. Can this be done? I want to extend the news module for this (or any Generic Content based module).
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    01 Apr 2010
    Link to this post
    Hi bemara57,

    You could take a look at Programming Security: TOC.
    The News module is generic content based module and it it gets key/value information for provider/global permissions using ContentManager class and GlobalPermission and GlobalPermissions from Telerik.Cms.Engine.Security.GlobalPermission.

    1. You need to implement custom class that inherits from using Telerik.Security.Permissions.Rights.

    sample code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Security.Permissions;
     
    /// <summary>
    /// Summary description for TelerikRights
    /// </summary>
    public class TelerikRights : Rights
    {
     
        //adding custom permissions
     
        [LocalRight("View")]
        public static int View
        {
            get
            {
                return 1;
            }
        }
     
     
        [LocalRight("Modify")]
        public static int Modify
        {
            get
            {
                return 2;
            }
        }
     
        [LocalRight("ManagePermissions")]
        public static int ManagePermissions
        {
            get
            {
                return 4;
            }
        }
     
        [LocalRight("SomethingElse")]
        public static int SomethingElse
        {
            get
            {
                return 8;
     
            }
        }
     
     
        public static TelerikRights Instance
        {
            get
            {
                return instance;
            }
        }
     
     
        private static TelerikRights instance = new TelerikRights();
     
    }

    2. You have to implement custom GlobalPermission and GlobalPermissions classes and use the custom rights class - TelerikRights

    sample code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Security.Permissions;
     
    /// <summary>
    /// Summary description for GlobalPermissions
    /// </summary>
    public class GlobalPermissions : SecuredBase
    {
        public GlobalPermissions(string providerName)
        {
            // THIS WILL WORK IN CASE YOU HAVE ONE PROVIDER
            this.rootId = new Guid("E48BA2BE-B022-4949-BBE7-E70C7A14462E");
            this.securityManager = new Telerik.Security.SecurityManager();
            this.rights = TelerikRights.Instance;
        }
     
        public override Guid ID
        {
            get
            {
                return this.rootId;
            }
        }
     
        private Guid rootId;
     
    }


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Security.Permissions;
    using Telerik.Security;
     
     
    public class GlobalPermission : ApplicationPermission
    {
     
        public GlobalPermission(string providerName)
            : this(new GlobalPermissions(providerName))
        {
        }
     
     
        public GlobalPermission(GlobalPermissions secObj)
            : base(secObj, 0)
        {
        }
     
        public GlobalPermission(string providerName, int requestedRights)
            : this(new GlobalPermissions(providerName), requestedRights)
        {
        }
     
     
        public GlobalPermission(GlobalPermissions secObj, int requestedRights)
            : base(secObj, requestedRights)
        {
        }
     
     
        public override bool CheckDemand()
        {
            if (SecurityManager.IsCurrentUserUnrestricted())
                return true;
     
            return base.CheckDemand();
        }
     
    }


    3. Implement a custom manager that uses your GlobalPermission class

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using Telerik.Security.Permissions;
    using Telerik.Cms.Engine.Security;
    using System.Configuration;
    using Telerik;
     
    public class TelerikManager : ManagerBase<BaseProvider>
     {
           public TelerikManager()
           {
           }
            
            #region Constructors
     
           public TelerikManager(string providerName)
                : base(providerName)
            {
     
            }
     
            #endregion
     
            #region Base overrides
     
            public override string DefaultProvider
            {
                get
                {
                  
                    if (!string.IsNullOrEmpty(ConfigHelper.Handler.DefaultProvider))
                        return ConfigHelper.Handler.DefaultProvider;
                    return String.Empty;
                }
            }
     
            public override string ModuleName
            {
                get
                {
     
                    return "CustomTelerikModule";
                }
            }
     
     
            public GlobalPermissions Permissions
            {
                get
                {
                    return (GlobalPermissions)TelerikManager.SecurityRoots[base.Provider.Name];
                }
            }
     
     
            public static Dictionary<String, ISecured> SecurityRoots
            {
                get
                {
                    if (securityRoots == null)
                    {
                        securityRoots = new Dictionary<String, ISecured>(Providers.Count);
                        foreach (string name in Providers.Keys)
                            securityRoots.Add(name, new GlobalPermissions(name));
                    }
                    return securityRoots;
                }
            }
     
     
            protected override ProviderSettingsCollection ProvidersSettings
            {
                get
                {
                    return ConfigHelper.Handler.Providers;
                }
            }
     
            public static IDictionary<String, BaseProvider> Providers
            {
                get
                {
                    if (providers == null)
                        providers = new TelerikManager().GetProviders();
                    return providers;
                }
            }
     
     
            #endregion
     
            #region Security Methods
     
     
            public GlobalPermission GetPermission()
            {
                return new GlobalPermission(this.Permissions);
            }
     
            public GlobalPermission GetPermission(int requestRights)
            {
                return new GlobalPermission(this.Permissions, requestRights);
            }
     
     
            #endregion
     
            #region Private fields
     
            string providerName;
            private static IDictionary<String, BaseProvider> providers;
            private static Dictionary<String, ISecured> securityRoots;
     
            #endregion
    }


    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. bemara57
    bemara57 avatar
    135 posts
    Registered:
    27 Mar 2008
    01 Apr 2010
    Link to this post
    Thanks for the code. I have a question about the GUID you highlighted in your code for "GlobalPermissions : SecuredBase". Does that GUID correspond to something, or is that any random GUID I make up for the module? I already have an extended module, so I should I get this GUID from an existing location?
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    01 Apr 2010
    Link to this post
    Hello bemara57,

    The Guid is random. It is used for persisting the permissions and SecurityRootID. You could see the value saved into [sf_GC_Variables] table.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Register for webinar
4 posts, 0 answered