+1-888-365-2779
Try Now
More in this section

Forums / Security / Adding Permissions to a role

Adding Permissions to a role

2 posts, 1 answered
  1. Chris
    Chris avatar
    1 posts
    Registered:
    27 Oct 2004
    16 Mar 2010
    Link to this post

    I have project where I am creating users and roles programmatically. I have everything working except for adding the cmsaccess permission to the role.  The code below is what I am using to create the role.

    public static bool CreateRole(string roleName)
       {
           bool retVal = false;
     
           CmsManager cmsManager = new CmsManager();
     
           UserManager membership = new UserManager(cmsManager.SecurityManager.Provider.Name);
     
           membership.CreateRole(roleName);
     
           string[] roleRoles = { roleName };
     
           //get permissions for role
           ApplicationPermission perm = GlobalPermissions.Instance.GetPermission(UserManager.Default.RoleProvider.Name, roleRoles);
     
           //make union of old permissions, and new permissions and set them
           perm.Union(new GlobalPermission(GlobalRights.CmsAccess));
            
           GlobalPermissions.Instance.SetPermissions(perm.Grant, perm.Deny, UserManager.Default.RoleProvider.Name, roleName);
     
           retVal = true;
     
           return retVal;
       }

    I get the following error on the union() call.

    System.ArgumentException: Target and source types must be the same Parameter name: Telerik.Security.Permissions.ApplicationPermission at Telerik.Security.Permissions.ApplicationPermission.Union(ApplicationPermission target) 

    I don’t fully understand the union but in the documentation it says it is combining the current and new permissions.  I wrote it to get the permission of the role I just created but that might be wrong.  Maybe it’s supposed to get it from currently logged in role?  I guess it could also be that the role I just created doesn’t have permissions yet and that’s causing a problem with the union of permissions. I am at a loss and any help would be appreciated.

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    17 Mar 2010
    Link to this post
    Hi Chris,

    The problem with your code is that  you are trying to set GlobalPermissions like CmsAccess . Below is a sample code that illustrates how to perform permission Union for ISecured object

    var cmsManager = new CmsManager();
    Guid pageID = new Guid("40b75e9d-4205-45aa-89c6-db43373ce41e");
    ICmsPage cmsPage = (ICmsPage)cmsManager.GetPage(pageID, true);
    ISecured secObject = cmsPage as ISecured;
    IDictionary<string, ApplicationPermission> permissions;
      
    // get the current permissions
    permissions = secObject.Permissions;
      
    ApplicationPermission perm = null;
    string[] selectedRole = {"test"};
    if (permissions.TryGetValue(selectedRole[0].ToLower(), out perm))
    {
        // create a new application permission to make the union
        ApplicationPermission p = new ApplicationPermission(CrudRights.Create, 0);
        perm = perm.Union(p);
        secObject.SetPermissions(perm.Grant, perm.Deny, selectedRole);
    }


    Kind regards,
    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.
    Answered
2 posts, 1 answered