Copied to clipboard

Flag this post as spam?

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


  • Kristoffer Eriksen 185 posts 465 karma points
    Sep 23, 2011 @ 18:48
    Kristoffer Eriksen
    0

    Bug in sender.remove??

    Hey Fellow Umbraco developers

    I've created a solution, with several users to the backoffice, in different usergroups.

    On AfterNodeRender I check which usergroup the user is in, to determine if the node should be displayed or not.

    using System;
    using umbraco;
    using umbraco.BusinessLogic;
    using umbraco.NodeFactory;
    using umbraco.cms.presentation.Trees;

    namespace Intranet.Events
    {
        public class HideNodesInTree : ApplicationBase
        {
            public HideNodesInTree() {
                BaseTree.AfterNodeRender += BaseTree_AfterNodeRender;
            }

            private void BaseTree_AfterNodeRender(ref XmlTree sender, ref XmlTreeNode node, EventArgs e) {
                string userTypeAlias = helper.GetCurrentUmbracoUser().UserType.Alias.ToLower();
                if (userTypeAlias != "admin") {
                    bool showNode = false;
                    var treeNode = new Node(Convert.ToInt32(node.NodeID));
                    if (userTypeAlias == "AllowNodeVisible") {
                        //if (treeNode.GetProperty("shownode").Value == "1")
                        //{
                        //}
                    }
                    if (!showNode) {
                        sender.Remove(node);
                    }
                }
            }
        }
    }

    With the above code nothing, offcause, happens. It just checks usertypealias and continues.

    BUT...If  add the outcommented code, then my thought would be, that again, nothing should happen.

    BUT...NO nodes is shown at all....?? I don't do anything, but get a property ??

     

    It's driving me crazy!!!

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Sep 26, 2011 @ 01:20
    Lee Kelleher
    0

    Hi Kristoffer,

    Hopefully a fresh pair of eyes can help here?  The variable "userTypeAlias" is assigned in lowercase, so it would never match "AllowNodeVisible".

    The variable "showNode" is set to false, and doesn't get changed, so the "!showNode" condition is always true, hence removing the node (all nodes - if you are not an admin user).

    I've revised your code a little... added a few more checks in there - so that it only applied to the 'content' section, and that the node has a property called "shownode", etc.

    private void BaseTree_AfterNodeRender(ref XmlTree sender, ref XmlTreeNode node, EventArgs e)
    {
        if (node.NodeType.ToLower() == "content")
        {
            string userTypeAlias = helper.GetCurrentUmbracoUser().UserType.Alias.ToLower();
            if (userTypeAlias != "admin")
            {
                bool showNode = false;
                if (userTypeAlias == "allownodevisible")
                {
                    int nodeId;
                    if (int.TryParse(node.NodeID, out nodeId))
                    {
                        var treeNode = new Node(nodeId);
                        var property = treeNode.GetProperty("shownode");
                        if (property != null && property.Value != null)
                        {
                            showNode = (property.Value == "1")
                        }
                    }
                }
    
                if (!showNode)
                {
                    sender.Remove(node);
                }
            }
        }
    }

    Cheers, Lee.

  • Kristoffer Eriksen 185 posts 465 karma points
    Sep 26, 2011 @ 09:02
    Kristoffer Eriksen
    0

    Hey Lee

    Thanks for the reply.

    I can see I made some mistakes in my example code :-) The UserTypeAlias in my code is in lowercase, but it was just in my exampe code, I wrote in wrong.

    I did some changes to my code, and now it works. I created a method for the allow-status, and used some of your code as well.

    using System;
    using umbraco;
    using umbraco.BusinessLogic;
    using umbraco.NodeFactory;
    using umbraco.cms.presentation.Trees;

    namespace Intranet.Events
    {
        public class HideNodesInTree : ApplicationBase
        {
            public HideNodesInTree()
            {
                BaseTree.AfterNodeRender += BaseTree_AfterNodeRender;
            }
           
            private void BaseTree_AfterNodeRender(ref XmlTree sender, ref XmlTreeNode node, EventArgs e)
            {
                if (node.NodeType.ToLower() == "content")
                {
                    string userTypeAlias = helper.GetCurrentUmbracoUser().UserType.Alias.ToLower();
                    if (userTypeAlias != "admin")
                    {
                        var userType = userTypeAlias;
                        bool showNode = true;
                        if (userType == "usertype")
                        {
                            int nodeId;
                            if (int.TryParse(node.NodeID, out nodeId))
                            {
                                var treeNode = new Node(nodeId);
                                showNode = GetShowStatus(treeNode);
                            }
                        }
                        if (!showNode)
                        {
                            sender.Remove(node);
                        }
                    }
                }
            }

            private bool GetShowStatus(Node treeNode)
            {
                var prop = treeNode.GetProperty("showToGroup1");
                if (prop != null && prop.Value != null)
                {
                    return true;
                }
                prop = treeNode.GetProperty("showToGroup2");
                if (prop != null && prop.Value != null)
                {
                    return true;
                }
                prop = treeNode.GetProperty("showToGroup3");
                if (prop != null && prop.Value != null)
                {
                    return true;
                }
                return false;
            }
        }
    }

     

    It seems to work.

    Have a great day

     

    Best Regards

    Kristoffer

  • Kristoffer Eriksen 185 posts 465 karma points
    Sep 26, 2011 @ 09:28
    Kristoffer Eriksen
    1

    Damn. Missed something important

    private bool GetShowStatus(Node treeNode)
            {
                var prop = treeNode.GetProperty("showToGroup1");
                if (prop != null && prop.Value != null)
                {
                    if (prop.Value == "1")
                        return true;
                }
                prop = treeNode.GetProperty("showToGroup2");
                if (prop != null && prop.Value != null)
                {
                    if (prop.Value == "1")
                        return true;
                }
                prop = treeNode.GetProperty("showToGroup3");
                if (prop != null && prop.Value != null)
                {
                    if (prop.Value == "1")
                        return true;
                }
                return false;
            }
Please Sign in or register to post replies

Write your reply to:

Draft