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.
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");
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.
publicclassLecturePermissions :ApplicationBase
{
public LecturePermissions()
{
Document.BeforePublish += newDocument.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 lecturesvar publicPropertyValue = sender.getProperty("public").Value;
var isPublic = publicPropertyValue == null ? false : Convert.ToBoolean(publicPropertyValue);
if (!isPublic)
{
// Add the default protectionsint 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);
}
}
}
}
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
Hi,
This post might help: http://our.umbraco.org/forum/developers/api-questions/5669-Setting-Public-Access-on-a-node-programatically
Tim
Hi Jan,
this may not help, but did you perform a save on the document after setting the membership to the group?
Also, I think AddMemberGroupToDocument is obsolete.
Thanks,
Nik
Here's an example (from the other thread..)
Thanks for the fast response guys. I got it solved now using Tim's example as an inspiration. :)
/Jan
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.
is working on a reply...