Say you have an Umbraco installation with two sites with their respective Homepages and Pages, e.g
- Content (-1)
- Homepage 1 (1000)
- Homepage 2 (1002)
In C# the current node can be obtained with
Node currentNode = Node.GetCurrent();
and its corresponding home node can be found with
Node currentHome = new Node(int.Parse(currentNode.Path.Split(',')[1]));
Now, currentNode.Path
returns a string of comma separated integers that starts with -1,
i.e. the root, the master root as you called it, under which all Homepages 'live'.
E.g. the path value of Page 2.1 is "-1,1002,1003". When split at the comma, you'll end up with an array with 3 elements indexed 0,1,2. Now, the second one, with index 1 will give the id of the home node. As you can see, the last id is the id of the current node. As an aside, the indexes also tell the level of the node, so the level of a Homepage is 1.
I used the following script on a template that was used on an intranet/extranet and has protected pages. When a visitor follows a link to a protected page, he/she is denied access and redirected to the homepage, which has a member login.
<%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %>
<%@ Import Namespace="umbraco.NodeFactory" %>
<script runat="server" language="CSharp">
protected void Page_Load(object sender, EventArgs e)
{
// prevents template to be run without proper authorisation
Node currentNode = Node.GetCurrent();
Node currentHome = new Node(int.Parse(currentNode.Path.Split(',')[1]));
Boolean HasAccess = umbraco.library.HasAccess(currentNode.Id, currentNode.Path);
Boolean IsProtected = umbraco.library.IsProtected(currentNode.Id, currentNode.Path);
if (IsProtected && !HasAccess)
{
// redirect to ancestor-or-self::HomePage
Response.Status = "403 Forbidden";
Response.Redirect(umbraco.library.NiceUrl(currentHome.Id), true);
}
}
</script>
<asp:Content ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
<!-- redirect to home page -->
</asp:Content>