Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Shaun 248 posts 475 karma points
    Dec 03, 2009 @ 17:00
    Shaun
    0

    Setting Public Access on a node programatically.

    Hi All.

    I'm writing nodes to umbraco dynamically and need to set all these nodes to have role based protection, allowing only one group to view them.

    I also need to set the login page and error page dynamically as well. I can't see anything on the web about this. Is it possible?

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Dec 03, 2009 @ 17:21
    Thomas Höhler
    0

    Take a look into the api: umbraco.cms.Permission method MakeNew

    hth, Thomas

  • Tim 225 posts 690 karma points
    Dec 03, 2009 @ 17:36
    Tim
    0

    @Thomas: I think that only applies to Users rather than Members???

    @Shaun: I'm assuming you mean you only want site members to access various pages (like an extranet)?

    T

  • Tim 225 posts 690 karma points
    Dec 03, 2009 @ 17:43
    Tim
    0

    Hi Shaun I think you are looking for this API call:

    umbraco.cms.businesslogic.web.Access.AddMemberGroupToDocument(int DocumentId, int MemberGroupId);

    T

  • Shaun 248 posts 475 karma points
    Dec 03, 2009 @ 18:09
    Shaun
    0

    Thanks Thomas!

    I've had a look at the api info here - http://umbraco.org/apiDocs/html/M_umbraco_BusinessLogic_Permission_MakeNew.htm

    But I'm still a bit confused about it.

    Can the User parameter just be the one I've used to create the document using Document.MakeNew?

    Also, its requesting a CMSNode object, I've created the node using the Document object,

    Document doc = Document.MakeNew(strDocumentName, dt, author, _intParentId);

    Do I need to create it using a different method? Or is there a way I can read the object back as a a CMSNode?

    Finally, whats the permissionkey? I can't seem to find a description of it anywhere?

    Sorry to be so dense.

     

  • Shaun 248 posts 475 karma points
    Dec 03, 2009 @ 18:11
    Shaun
    0

    Aha, posted that before I saw Tims answers.  You're quite right Tim. I'm creating new pages that can only be accessed by members of a particular group. Your answer seems more like what I'm after. I'll have a look at that now.

  • Shaun 248 posts 475 karma points
    Dec 03, 2009 @ 18:25
    Shaun
    0

    I've given it a bash, but it appears its obsolete as a method (I'm using V4)

     umbraco.cms.businesslogic.web.Access.AddMemberGroupToDocument(doc.Id, 1146);

    Error    7    'umbraco.cms.businesslogic.web.Access.AddMemberGroupToDocument(int, int)' is obsolete: 'This method is no longer supported. Use the ASP.NET MemberShip methods instead' .

    I'll see if I can work out how to do it using the membership methods.

     

     

  • Shaun 248 posts 475 karma points
    Dec 03, 2009 @ 18:33
    Shaun
    0

    I've discovered another method - AddMemberShipRoleToDocument.

    It sounds more like what I'm after, inasmuch as I have a member group called "View Comments" which I'd normally add to a node using Public Access ->Role based access in the admin section if I were doing it manually.

    I tried passing it data using this code, but its returned an odd error

    Server Error in '/' Application.

    Document is not protected!

     
    My code is below, have I missed something obvious or am I barking up the wrong tree by using this method?

    //create node        
    string strDocumentName = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_ffffff");          

    DocumentType dt = DocumentType.GetByAlias("Comment");
               
     //author uses a dummy user called CommentWriter
     User author = User.GetUser(2);          

    Document doc = Document.MakeNew(strDocumentName, dt, author, _intParentId);          

    Member myMember = Member.GetCurrentMember();

     //add properties          
    doc.getProperty("commentName").Value = myMember.Text;
    doc.getProperty("commentBody").Value = tbComments.Text;
    doc.getProperty("commentID").Value = myMember.Id;
               
     //set public access to "View Comments" - 1146
     umbraco.cms.businesslogic.web.Access.AddMembershipRoleToDocument(doc.Id, "View Comments");
              
     doc.Publish(author);
    umbraco.library.UpdateDocumentCache(doc.Id);

     

  • Tim 225 posts 690 karma points
    Dec 03, 2009 @ 19:00
    Tim
    0

    Hi,

    Yes you're right with the AddMemberShipRoleToDocument..

    No idea why you're getting that error - try adding the role after you have published and Updated the document cache??

    T

  • Tim 225 posts 690 karma points
    Dec 03, 2009 @ 19:09
    Tim
    4

    Hi,

    It looks like you may need to call:

    ProtectPage(bool Simple, int DocumentId, int LoginDocumentId, int ErrorDocumentId) 

    first, that way you can set your login page and not-authorised page as well.

    If you are going to use Roles then you'll need to set the Simple bool to false.

    Then when you have done that you can the roles i.e.

    //create node        
    string strDocumentName = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss_ffffff");          

    DocumentType dt = DocumentType.GetByAlias("Comment");
               
     
    //author uses a dummy user called CommentWriter
     
    User author = User.GetUser(2);          

    Document doc = Document.MakeNew(strDocumentName, dt, author, _intParentId);          

    Member myMember = Member.GetCurrentMember();

     
    //add properties          
    doc
    .getProperty("commentName").Value = myMember.Text;
    doc
    .getProperty("commentBody").Value = tbComments.Text;
    doc
    .getProperty("commentID").Value = myMember.Id;
               
     
    //set public access to "View Comments" - 1146
    int loginDocId = 1023;
    int errorDocId = 1024;
    umbraco.cms.businesslogic.web.Access.ProtectPage(false, doc.Id, loginDocId, errorDocId)
     umbraco.cms.businesslogic.web.Access.AddMembershipRoleToDocument(doc.Id, "View Comments");
               
     doc
    .Publish(author);
    umbraco
    .library.UpdateDocumentCache(doc.Id);
  • Shaun 248 posts 475 karma points
    Dec 04, 2009 @ 12:15
    Shaun
    0

    That works brilliantly. Thanks Tim.

    Interestingly, although the node has its permissions set correctly, the info doesn't seem to pass across to the admin UI, so when you select "public access" it still takes you through the whole process as if no permissions were set.

    This isn't really an issue for me, but it worth noting.

    Thanks again for your help Tim.

Please Sign in or register to post replies

Write your reply to:

Draft