More in this section

Forums / Developing with Sitefinity / Splitting users in roles

Splitting users in roles

4 posts, 0 answered
  1. Muhammad Saqib
    Muhammad Saqib avatar
    95 posts
    Registered:
    09 Sep 2009
    15 Mar 2010
    Link to this post
    Hi Guys

    I am having a situation in which i have a role to which a number of users belong to. Now i have a requirement that i have to take some of the users and pull them out of that role and assign them a new role, but i am not intending to set all the permissions of the second role and this is why i want that the permission that were set on the first role should be cloned to the second role. Is this thing possible ? can you please give me a short code example.

    Thanks,

    Regards,
    Muhammad Saqib.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    15 Mar 2010
    Link to this post
    Hi Muhammad Saqib,

    Below is a sample code that illustrates how to clone the permissions from one role to another for modules.

    IDictionary<string, Telerik.IWebModule> dict = Telerik.ModuleManager.WebModules;
     foreach (KeyValuePair<string, Telerik.IWebModule> SitefinityModule in dict)
     {
          
         ISecuredModule module = Telerik.ModuleManager.WebModules[SitefinityModule.Key] as ISecuredModule;
         if (module != null)
         {
             //GET THE APPLICATION PERMISSIONS
             IDictionary<string, ApplicationPermission> permissions;
             ISecured secObj = module.SecurityRoot;
             if (secObj is ISecuredBase)
                 permissions = ((ISecuredBase)secObj).GetPermissions("Sitefinity");
             else
                 permissions = secObj.Permissions;
     
             //TYPE THE NAME OF YOUR CURRENT ROLE HERE
             string roleName = "test";
             bool hasViewPermission = false;
             ApplicationPermission perm;
             if (!Telerik.Security.Configuration.ConfigHelper.IsRoleUnrestricted(roleName))
             {
                 if (permissions.TryGetValue(roleName, out perm))
                 {
                     // SET PERMISSIONS FOR THE NEWLY CREATED ROLE
                     string[] cloned = { "cloned" };
                     secObj.SetPermissions(perm.Grant, perm.Deny, cloned);
                    
                 }
             }
         }
     }

    Here is the code that you should use for pages

    var cmsManager = new CmsManager();
    IList AllCmsPages = cmsManager.GetPages();
    foreach (ICmsPage singlePage in AllCmsPages)
    {
        //GET THE APPLICATION PERMISSIONS
        IDictionary<string, ApplicationPermission> permissions;
        ISecured secObj = singlePage as ISecured;
        if (secObj is ISecuredBase)
            permissions = ((ISecuredBase)secObj).GetPermissions("Sitefinity");
        else
            permissions = secObj.Permissions;
     
        //TYPE THE NAME OF YOUR ROLE HERE
        string roleName = "test";
        bool hasViewPermission = false;
        ApplicationPermission perm;
        if (!Telerik.Security.Configuration.ConfigHelper.IsRoleUnrestricted(roleName))
        {
            if (permissions.TryGetValue(roleName, out perm))
            {
                string[] cloned = { "cloned" };
                secObj.SetPermissions(perm.Grant, perm.Deny, cloned);
     
            }
        }
    }


    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. Muhammad Saqib
    Muhammad Saqib avatar
    95 posts
    Registered:
    09 Sep 2009
    16 Mar 2010
    Link to this post
    Hi all ,

    I have tried the code that you recommended but i am getting an exception in the module clone ,


    The given key was not present in the dictionary.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
     
    Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
     
    Source Error:
     
    Line 179:                    //GET THE APPLICATION PERMISSIONS
    Line 180:                    IDictionary<string, ApplicationPermission> permissions;
    Line 181:                    ISecured secObj = module.SecurityRoot;
    Line 182:                    if (secObj is ISecuredBase)
    Line 183:                        permissions = ((ISecuredBase)secObj).GetPermissions("Sitefinity");
    Exception occured on line # 181.

    inner exception is....
    StackTrace = "   at System.ThrowHelper.ThrowKeyNotFoundException()\r\n   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)\r\n   at Telerik.News.NewsModule.get_SecurityRoot()"

    Below is how i used the code:
    "ElxUsers" is the role whose permissions i want to copy into the newly creatd role(created at admin side). the newly created roles name is given through the parameter "RoleName".

    public bool CloneModulePermissions(string RoleName)
        {
     
            IDictionary<string, Telerik.IWebModule> dict = Telerik.ModuleManager.WebModules;
            foreach (KeyValuePair<string, Telerik.IWebModule> SitefinityModule in dict)
            {
     
                ISecuredModule module = Telerik.ModuleManager.WebModules[SitefinityModule.Key] as ISecuredModule;
                if (module != null)
                {
                    //GET THE APPLICATION PERMISSIONS
                    IDictionary<string, ApplicationPermission> permissions;
                    ISecured secObj = module.SecurityRoot;
                    if (secObj is ISecuredBase)
                        permissions = ((ISecuredBase)secObj).GetPermissions("Sitefinity");
                    else
                        permissions = secObj.Permissions;
     
                    //TYPE THE NAME OF YOUR CURRENT ROLE HERE
                    string roleName = "ElxUsers";
                    ApplicationPermission perm;
                    if (!Telerik.Security.Configuration.ConfigHelper.IsRoleUnrestricted(roleName))
                    {
                        if (permissions.TryGetValue(roleName, out perm))
                        {
                            // SET PERMISSIONS FOR THE NEWLY CREATED ROLE
                            string[] cloned = { RoleName };
                            secObj.SetPermissions(perm.Grant, perm.Deny, cloned);
     
                        }
                    }
                }
            }
     
            return true;
     
        }
        public bool ClonePagePermissions(string RoleName)
        {
     
            var cmsManager = new CmsManager();
            IList AllCmsPages = cmsManager.GetPages();
            foreach (ICmsPage singlePage in AllCmsPages)
            {
                //GET THE APPLICATION PERMISSIONS
                IDictionary<string, ApplicationPermission> permissions;
                ISecured secObj = singlePage as ISecured;
                if (secObj is ISecuredBase)
                    permissions = ((ISecuredBase)secObj).GetPermissions("Sitefinity");
                else
                    permissions = secObj.Permissions;
     
                //TYPE THE NAME OF YOUR ROLE HERE
                string roleName = "ElxUsers";
                ApplicationPermission perm;
                if (!Telerik.Security.Configuration.ConfigHelper.IsRoleUnrestricted(roleName))
                {
                    if (permissions.TryGetValue(roleName, out perm))
                    {
                        string[] cloned = { RoleName };
                        secObj.SetPermissions(perm.Grant, perm.Deny, cloned);
     
                    }
                }
            }
            return true;
     
     
     
     
        }

    Regards,
    Muhammad Saqib.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    16 Mar 2010
    Link to this post
    Hello Muhammad Saqib,

    You can get the error, because the defaultProvider is not set for one of your modules or you have not excluded some of the not generic content based modules. As you can see the sample gets all modules. You could create an array of the module names and use it to set the permissions instead of calling Telerik.ModuleManager.WebModules 

    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.
Register for webinar
4 posts, 0 answered