Copied to clipboard

Flag this post as spam?

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


  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 15, 2010 @ 14:16
    Jan Skovgaard
    0

    Programitically setting "public access" to a document

    Hi guys

    I need to set public access for a document, which is being created through the API and not in the Umbraco backend.

    I have tried using umbraco.cms.businesslogic.web.Access.AddMembershipRoleToDocument(d.Id, "mygroup"); - But when I check the "public access" properties in the Umbraco backend after a new document has been created through the API nothing has been set.

    So everyone can see the document even though it needs to be protected.

    I'm not sure if it's the correct method I'm using, since I actually think that this one would make more sense. umbraco.cms.businesslogic.web.Access.AddMemberGroupToDocument - The only reason why I'm not using this is beacuse I can't figure out where to get the group ID from?

    When one is looking in in the lower left corner the group name is being displayed instead of the id.

    /Jan

  • Tim 225 posts 690 karma points
    Feb 15, 2010 @ 14:21
  • Nik Wahlberg 639 posts 1237 karma points MVP
    Feb 15, 2010 @ 14:22
    Nik Wahlberg
    0

    Hi Jan,

    this may not help, but did you perform a save on the document after setting the membership to the group? 

    d.Save();

    Also, I think AddMemberGroupToDocument is obsolete. 

    Thanks,
    Nik

  • Tim 225 posts 690 karma points
    Feb 15, 2010 @ 14:28
    Tim
    2

    Here's an example (from the other thread..)

    DocumentType dt = DocumentType.GetByAlias("DOC TYPE ALIAS HERE");
               

    User author = User.GetUser(2);          

    Document doc = Document.MakeNew("DOC NAME HERE", dt, author, 1099);          

    Member myMember = Member.GetCurrentMember();
               
     
    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, "GROUP NAME HERE");
               
     doc
    .Publish(author);
     umbraco
    .library.UpdateDocumentCache(doc.Id);

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 15, 2010 @ 15:08
    Jan Skovgaard
    0

    Thanks for the fast response guys. I got it solved now using Tim's example as an inspiration. :)

    /Jan

  • Justin Spradlin 139 posts 347 karma points
    Sep 24, 2011 @ 00:12
    Justin Spradlin
    0

    Hi everyone,

    This post helped me get started so I thought I would show how I implemented it. My site has a document type called Lectures. These by default are only visible to paying members. However we allow some lectures to show on the site for public consumption. I added a boolean property to my Lecture data type called "Public". The default is false. I wrote this ApplicationBase class to inject my default policy. 

    This allows my users to create a new lecture and simply check a box for public instead of manually setting the permission on each item. It also enforces the permission rules so that someone does not forget to restrict access to the new lectures. 

    Also note the I store the Login and Error node id values in the web.config as a Application Setting to make this highly configurable. I guess if you wanted you could refactor this to work for any document type by making a custom configuration setting in your web.config with rules for permissions. If I ever get any free time I'd love to make a package that allows for default permission schemes. 

     public class LecturePermissions :ApplicationBase
        {
            public LecturePermissions()
            {
                Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);
            }
    
            /// <summary>
            /// This method will create/clear the permissions for Lecture nodes based on the "Public" flag status.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            void Document_BeforePublish(Document sender, umbraco.cms.businesslogic.PublishEventArgs e)
            {
                if (sender.ContentType.Alias == "Lecture")
                {
                    // Default to Non-Public for lectures
                    var publicPropertyValue = sender.getProperty("public").Value; 
                    var isPublic = publicPropertyValue == null ? false : Convert.ToBoolean(publicPropertyValue);
                    
                    if (!isPublic)
                    {
                        // Add the default protections
    
                        int loginDocId = Convert.ToInt32(ConfigurationManager.AppSettings["Premium.LoginPageId"]);
                        int errorDocId = Convert.ToInt32(ConfigurationManager.AppSettings["Premium.ErrorPageId"]);
    
                        Access.ProtectPage(false, sender.Id, loginDocId, errorDocId);
                        Access.AddMembershipRoleToDocument(sender.Id, "PremiumMembers");
                        sender.Save();
                        umbraco.library.UpdateDocumentCache(sender.Id);
    
                    }
                    else
                    {
                        // Remove the security 
                        Access.RemoveProtection(sender.Id); 
                    }
                }
            }
        }
Please Sign in or register to post replies

Write your reply to:

Draft