Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 597 posts 2395 karma points
    Oct 20, 2017 @ 07:41
    Bo Jacobsen
    0

    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.

    /* 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?

    PublicAccessService pubAccService = new PublicAccessServicePublicAccessService(IDatabaseUnitOfWorkProvider provider, RepositoryFactory repositoryFactory, ILogger logger, IEventMessagesFactory eventMessagesFactory);
    
  • Bo Jacobsen 597 posts 2395 karma points
    Jul 10, 2018 @ 17:58
    Bo Jacobsen
    104

    Hi all.

    I found out.

    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");
    
  • Sam 79 posts 426 karma points
    Sep 20, 2019 @ 16:15
    Sam
    0

    Thank you for this post.

Please Sign in or register to post replies

Write your reply to:

Draft