Copied to clipboard

Flag this post as spam?

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


  • Sam 75 posts 391 karma points
    Jun 19, 2019 @ 04:40
    Sam
    0

    How to set Public Access Member Role On a form Post

    Hello, and thanks in advance.

    I am trying to make the data entered into a form visible to a specific member group that I created. I am Able to post the data to Umbraco without an issue, but I am unable to find clear documentation about setting the Public Access for the Post. here is what I have below.

    public ActionResult HandleFormPost(WarrantyModel model) //Links functions to the form in the partial view...BeginUmbracoForm
        {
            if (ModelState.IsValid) // For Field Validation 
            {
                // This will post submittion to Umbraco Database 
                var newWarranty = Services.ContentService.CreateContent(model.WarLastName + "-" + DateTime.Now, 1204, "warrantyFormula");
                var myService = ApplicationContext.Services.DataTypeService;
                string InstallType = model.WarInstallType;
                string Applications = model.WarApplication;
                string PurchasedFroms = model.WarPurchasedFrom;
                int ColorValue = model.WarColor;
                var ColorNode = new Node(ColorValue);
    
    newWarranty.SetValue("WarInstallDate", model.WarInstallDate);
                newWarranty.SetValue("WarSqFt", model.WarSqFt);
                newWarranty.SetValue("WarInstallType", InstallType);
                newWarranty.SetValue("WarApplication", Applications);
                newWarranty.SetValue("WarColor", ColorNode.Name);
    
                newWarranty.SetValue("WarFabCompany", model.WarFabCompany);
                newWarranty.SetValue("WarFabAddress", model.WarFabAddress);
                newWarranty.SetValue("WarFabCity", model.WarFabCity);
                newWarranty.SetValue("WarFabSt", model.WarFabSt);
                newWarranty.SetValue("WarFabZip", model.WarFabZip);
                newWarranty.SetValue("WarFabCountry", model.WarFabCountry);
                newWarranty.SetValue("WarFabPhone", model.WarFabPhone);
                newWarranty.SetValue("umbracoNaviHide", true);
                Services.ContentService.SaveAndPublishWithStatus(newWarranty);
    
                // Set Member Role Access
                int newWarrantyId = model.Id;
                var publicAccessService = ApplicationContext.Current.Services.PublicAccessService;
                var entry = publicAccessService.GetEntryForContent(newWarrantyId);
    
                entry.LoginNodeId = 1447;
                entry.NoAccessNodeId = 1097;
                entry.AddRule("MemberRole", "Warranty Admin");
                publicAccessService.Save(entry);
        }
    }
    

    I think I am Trying to Get the ID of the Post that was just saved and Published, and then update the Public Access mode, Login page, error page, and the member group. Every test I do does not apply the member group.

  • Sam 75 posts 391 karma points
    Jun 26, 2019 @ 17:28
    Sam
    0

    Does anybody have any insight? I keep getting errors that the entry variable is null or no set. I am not able to get the ID of the newly created Data

  • Natalie 22 posts 162 karma points
    Jun 27, 2019 @ 07:22
    Natalie
    0

    Hi,

    I dunno how to do it, but I am actually managing the public acces lie that...

    Managing public access

  • Sam 75 posts 391 karma points
    Jun 27, 2019 @ 14:15
    Sam
    0

    Thanks for your response. I am aware of how to set up the access manually, but I need to do it from the controller because the node is created from the form post by the public user. once the data is submitted I don't want the new page/content to be public

  • Sam 75 posts 391 karma points
    Sep 20, 2019 @ 16:26
    Sam
    100

    I found The answer to this post Here. the Answer is from Bo Jacobsen.

    here is what changed in the controller:

             ...
                newWarranty.SetValue("WarFabZip", model.WarFabZip);
                newWarranty.SetValue("WarFabCountry", model.WarFabCountry);
                newWarranty.SetValue("WarFabPhone", model.WarFabPhone);
                newWarranty.SetValue("umbracoNaviHide", true);
             Services.ContentService.SaveAndPublishWithStatus(newWarranty);
                // Set Member Role Access
                // Umbraco Services
                var publicAccessService = ApplicationContext.Current.Services.PublicAccessService;
                var contentService = ApplicationContext.Current.Services.ContentService;
    
                // If entry does not exists, create one this way.
                var loginPage = contentService.GetById(1447);
                var noAccessPage = contentService.GetById(1097);
                var entry = new PublicAccessEntry(newWarranty, 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(newWarranty, "MemberRole", "Warranty Admin");
    
Please Sign in or register to post replies

Write your reply to:

Draft