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

Forums / Developing with Sitefinity / Set page permissions programmatically

Set page permissions programmatically

6 posts, 2 answered
  1. Jenn Bohm
    Jenn Bohm avatar
    17 posts
    Registered:
    19 Aug 2009
    12 Mar 2010
    Link to this post
    I am trying to use SecurityManager.SetPermissions to set permissions on a page, and I'm not sure how to use the parameters "grant" and "deny". I see they are integers, but what kind of values should they have? I'm looking for an example of the use of this method, and can't find anything.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    14 Mar 2010
    Link to this post
    Hello Jenn Bohm,

    Below is a sample code that illustrates how to set permissions for a given page.

    var manager = new CmsManager();
    ICmsPage page = (ICmsPage)manager.GetPage(new Guid("8f48d010-9e87-4201-9a89-06b65ddded8a"), true);
    ISecured secured = page as ISecured;
    string[] rolenames = {"test"};
     secured.SetPermissions(CrudRights.View, CrudRights.Delete, rolenames);


    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.
    Answered
  3. Jenn Bohm
    Jenn Bohm avatar
    17 posts
    Registered:
    19 Aug 2009
    23 Mar 2010
    Link to this post
    Thank you!
  4. Jenn Bohm
    Jenn Bohm avatar
    17 posts
    Registered:
    19 Aug 2009
    27 Mar 2010
    Link to this post
    So I have more questions about this. In a call to SetPermissions, I can pass one CrudRights to grant them permission on, and one to deny them permission on. I must be missing something here, but I don't see how to do a couple simple-seeming things:
    1. deny ALL rights to one set of roles
    2. grant view rights and deny ALL OTHER rights to another set of roles

    If I don't want to grant ANY rights, and want to deny ALL rights to a particular role, what would I pass as the grant argument? I had hoped there would be a "CrudRights.All" or some such thing, but only saw values for individual rights (ChangePermissions, Create, Delete, Modify, and View)
    When I used SetPermissions(CrudRights.View, CrudRights.Delete, roles), then viewed the page in the cms dashboard, I saw that in the Permissions section for the page in question, View had "grant" checked, and Delete had "deny" checked, and neither "grant" nor "deny" was checked for any of the other rights, and for all roles other than that for which I wanted to grant view rights only had nothing checked for any of the rights.

    I know this shouldn't be that hard to do, but somehow I'm just not getting it.

    edit: it looks like perhaps to combine multiple rights, I should bitwise "or" them? that's what I'm about to try next. not sure what I should be putting in for the "grant" argument if there's nothing I want to grant, though. trying zero...
  5. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    28 Mar 2010
    Link to this post
    Hello Jenn Bohm,

    You can deny all permissions to a role/s if you pass "0" to the first and second parameter of SerPermissions method

    secured.SetPermissions(0, 0, rolenames);

    You could also use conditions as shown below

    secured.SetPermissions(0, CrudRights.Delete | CrudRights.View, rolenames);

    To grant only view pass CrudRights.View to the first parameter of SetPermissions and "0" for the second.

    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.
    Answered
  6. Jenn Bohm
    Jenn Bohm avatar
    17 posts
    Registered:
    19 Aug 2009
    30 Mar 2010
    Link to this post
    It looks like this worked, thanks! ^_^
Register for webinar
6 posts, 2 answered