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);
}
}
}
}
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); } } } }
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.
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!!!
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.
Cheers, Lee.
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.
It seems to work.
Have a great day
Best Regards
Kristoffer
Damn. Missed something important
is working on a reply...