I am automatic protecting pages with roles when creating new documents. But the method i am using is obsolete.
/* Class 'umbraco.cms.businesslogic.web.Access' is obsolete: "Use Umbraco.Core.Service.IPublicAccessService instead" */
umbraco.cms.businesslogic.web.Access.AddMembershipRoleToDocument(pageId, roleName);
The documentation on IPublicAccessService is here. But i cannot figure out how AddRule(IContent content, string ruleType, string ruleValue) can replace AddMembershipRoleToDocument(int documentId, string role).
What are ruleType and ruleValue for? And how would i use this service? can anyone explain?
ruleType is always MemberRole and ruleValue is the name of your Member Group name.
// Umbraco Services
var publicAccessService = ApplicationContext.Current.Services.PublicAccessService;
var contentService = ApplicationContext.Current.Services.ContentService;
// Get IContent that should be protectet.
var content = contentService.GetById(1000);
// Get the current role for the content
var entry = publicAccessService.GetEntryForContent(content);
// Update entry
entry.LoginNodeId = 1500;
entry.NoAccessNodeId = 1660;
entry.AddRule("MemberRole", "NewCustomRole");
publicAccessService.Save(entry);
// If entry does not exists, create one this way.
var loginPage = ApplicationContext.Current.Services.ContentService.GetById(1001);
var noAccessPage = ApplicationContext.Current.Services.ContentService.GetById(1002);
var entry = new PublicAccessEntry(content, loginPage, noAccessPage, new List<PublicAccessRule>());
publicAccessService.Save(entry);
// After the new entry is saved, you can add new rule this way.
// Or if you just wanna add rules and not LoginNodeId and NoAccessNodeId.
// If entry is null. This won't work.
var roleBasedProtectionAttempt = publicAccessService.AddRule(content, "MemberRole", "MyCustomRole");
How to use IPublicAccessService instead of Access
Hi all.
I am automatic protecting pages with roles when creating new documents. But the method i am using is obsolete.
The documentation on IPublicAccessService is here. But i cannot figure out how AddRule(IContent content, string ruleType, string ruleValue) can replace AddMembershipRoleToDocument(int documentId, string role).
What are ruleType and ruleValue for? And how would i use this service? can anyone explain?
Hi all.
I found out.
ruleType is always MemberRole and ruleValue is the name of your Member Group name.
Thank you for this post.
is working on a reply...