2 // System.Web.SiteMapProvider
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Lluis Sanchez Gual (lluis@novell.com)
9 // (C) 2005 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.ComponentModel;
35 using System.Collections;
36 using System.Collections.Specialized;
38 using System.Configuration.Provider;
39 using System.Web.Util;
40 using System.Globalization;
41 using System.Web.Configuration;
43 namespace System.Web {
44 public abstract class SiteMapProvider : ProviderBase {
45 static readonly object siteMapResolveEvent = new object ();
47 internal object this_lock = new object ();
49 bool enableLocalization;
50 SiteMapProvider parentProvider;
51 SiteMapProvider rootProviderCache;
52 bool securityTrimming;
53 object resolveLock = new Object();
56 EventHandlerList events = new EventHandlerList ();
58 public event SiteMapResolveEventHandler SiteMapResolve {
59 add { events.AddHandler (siteMapResolveEvent, value); }
60 remove { events.RemoveHandler (siteMapResolveEvent, value); }
63 protected virtual void AddNode (SiteMapNode node)
68 internal protected virtual void AddNode (SiteMapNode node, SiteMapNode parentNode)
70 throw new NotImplementedException ();
73 public virtual SiteMapNode FindSiteMapNode (HttpContext context)
78 SiteMapNode ret = this.FindSiteMapNode (context.Request.RawUrl);
80 ret = this.FindSiteMapNode (context.Request.Path);
84 public abstract SiteMapNode FindSiteMapNode (string rawUrl);
86 public virtual SiteMapNode FindSiteMapNodeFromKey (string key)
88 /* msdn2 says this function always returns
89 * null, but it seems to just call
90 * FindSiteMapNode(string rawUrl) */
91 return FindSiteMapNode (key);
94 public abstract SiteMapNodeCollection GetChildNodes (SiteMapNode node);
96 public virtual SiteMapNode GetCurrentNodeAndHintAncestorNodes (int upLevel)
98 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
103 public virtual SiteMapNode GetCurrentNodeAndHintNeighborhoodNodes (int upLevel, int downLevel)
105 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
106 if (downLevel < -1) throw new ArgumentOutOfRangeException ("downLevel");
111 public abstract SiteMapNode GetParentNode (SiteMapNode node);
113 public virtual SiteMapNode GetParentNodeRelativeToCurrentNodeAndHintDownFromParent (int walkupLevels, int relativeDepthFromWalkup)
115 if (walkupLevels < 0) throw new ArgumentOutOfRangeException ("walkupLevels");
116 if (relativeDepthFromWalkup < 0) throw new ArgumentOutOfRangeException ("relativeDepthFromWalkup");
118 SiteMapNode node = GetCurrentNodeAndHintAncestorNodes (walkupLevels);
119 for (int n=0; n<walkupLevels && node != null; n++)
120 node = GetParentNode (node);
122 if (node == null) return null;
124 HintNeighborhoodNodes (node, 0, relativeDepthFromWalkup);
128 public virtual SiteMapNode GetParentNodeRelativeToNodeAndHintDownFromParent (SiteMapNode node, int walkupLevels, int relativeDepthFromWalkup)
130 if (walkupLevels < 0) throw new ArgumentOutOfRangeException ("walkupLevels");
131 if (relativeDepthFromWalkup < 0) throw new ArgumentOutOfRangeException ("relativeDepthFromWalkup");
132 if (node == null) throw new ArgumentNullException ("node");
134 HintAncestorNodes (node, walkupLevels);
135 for (int n=0; n<walkupLevels && node != null; n++)
136 node = GetParentNode (node);
138 if (node == null) return null;
140 HintNeighborhoodNodes (node, 0, relativeDepthFromWalkup);
144 protected internal abstract SiteMapNode GetRootNodeCore ();
146 protected static SiteMapNode GetRootNodeCoreFromProvider (SiteMapProvider provider)
148 return provider.GetRootNodeCore ();
151 public virtual void HintAncestorNodes (SiteMapNode node, int upLevel)
153 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
154 if (node == null) throw new ArgumentNullException ("node");
157 public virtual void HintNeighborhoodNodes (SiteMapNode node, int upLevel, int downLevel)
159 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
160 if (downLevel < -1) throw new ArgumentOutOfRangeException ("downLevel");
161 if (node == null) throw new ArgumentNullException ("node");
164 protected virtual void RemoveNode (SiteMapNode node)
166 throw new NotImplementedException ();
169 public override void Initialize (string name, NameValueCollection attributes)
171 base.Initialize (name, attributes);
172 if (attributes ["securityTrimmingEnabled"] != null)
173 securityTrimming = (bool) Convert.ChangeType (attributes ["securityTrimmingEnabled"], typeof (bool));
176 [MonoTODO ("need to implement cases 2 and 3")]
177 public virtual bool IsAccessibleToUser (HttpContext context, SiteMapNode node)
179 if (context == null) throw new ArgumentNullException ("context");
180 if (node == null) throw new ArgumentNullException ("node");
182 if (!SecurityTrimmingEnabled)
185 /* The node is accessible (according to msdn2) if:
187 * 1. The Roles exists on node and the current user is in at least one of the specified roles.
189 * 2. The current thread has an associated WindowsIdentity that has file access to the requested URL and
190 * the URL is located within the directory structure for the application.
192 * 3. The current user is authorized specifically for the requested URL in the authorization element for
193 * the current application and the URL is located within the directory structure for the application.
197 IList roles = node.Roles;
198 if (roles != null && roles.Count > 0) {
199 foreach (string rolename in roles)
200 if (rolename == "*" || context.User.IsInRole (rolename))
208 string url = node.Url;
209 if(!String.IsNullOrEmpty(url)) {
210 // TODO check url is located within the current application
212 if (VirtualPathUtility.IsAppRelative (url) || !VirtualPathUtility.IsAbsolute (url))
213 url = VirtualPathUtility.Combine (VirtualPathUtility.AppendTrailingSlash (HttpRuntime.AppDomainAppVirtualPath), url);
215 AuthorizationSection config = (AuthorizationSection) WebConfigurationManager.GetSection (
216 "system.web/authorization",
219 return config.IsValidUser (context.User, context.Request.HttpMethod);
225 public virtual SiteMapNode CurrentNode {
227 if (HttpContext.Current != null) {
228 SiteMapNode ret = ResolveSiteMapNode (HttpContext.Current);
229 if (ret != null) return ret;
230 return FindSiteMapNode (HttpContext.Current);
236 public virtual SiteMapProvider ParentProvider {
237 get { return parentProvider; }
238 set { parentProvider = value; }
241 public virtual SiteMapProvider RootProvider {
244 if (rootProviderCache == null) {
245 SiteMapProvider current = this;
246 while (current.ParentProvider != null)
247 current = current.ParentProvider;
249 rootProviderCache = current;
252 return rootProviderCache;
256 protected SiteMapNode ResolveSiteMapNode (HttpContext context)
258 SiteMapResolveEventHandler eh = events [siteMapResolveEvent] as SiteMapResolveEventHandler;
265 SiteMapResolveEventArgs args = new SiteMapResolveEventArgs (context, this);
266 SiteMapNode r = eh (this, args);
274 public bool EnableLocalization {
275 get { return enableLocalization; }
276 set { enableLocalization = value; }
279 public bool SecurityTrimmingEnabled {
280 get { return securityTrimming; }
284 public string ResourceKey {
285 get { return resourceKey; }
286 set { resourceKey = value; }
289 public virtual SiteMapNode RootNode {
291 SiteMapNode node = GetRootNodeCore ();
292 return ReturnNodeIfAccessible (node);
296 internal static SiteMapNode ReturnNodeIfAccessible (SiteMapNode node)
298 if (node.IsAccessibleToUser (HttpContext.Current))
301 throw new InvalidOperationException (); /* need