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.Collections;
35 using System.Collections.Specialized;
37 using System.Configuration.Provider;
38 using System.Web.Util;
39 using System.Globalization;
41 namespace System.Web {
42 public abstract class SiteMapProvider : ProviderBase {
44 bool enableLocalization;
45 SiteMapProvider parentProvider;
46 SiteMapProvider rootProviderCache;
47 bool securityTrimming;
48 object resolveLock = new Object();
51 protected virtual void AddNode (SiteMapNode node)
56 internal protected virtual void AddNode (SiteMapNode node, SiteMapNode parentNode)
58 throw new NotImplementedException ();
61 public virtual SiteMapNode FindSiteMapNode (HttpContext context)
66 SiteMapNode ret = this.FindSiteMapNode (context.Request.RawUrl);
68 ret = this.FindSiteMapNode (context.Request.Path);
72 public abstract SiteMapNode FindSiteMapNode (string rawUrl);
74 public virtual SiteMapNode FindSiteMapNodeFromKey (string key)
76 /* msdn2 says this function always returns
77 * null, but it seems to just call
78 * FindSiteMapNode(string rawUrl) */
79 return FindSiteMapNode (key);
82 public abstract SiteMapNodeCollection GetChildNodes (SiteMapNode node);
84 public virtual SiteMapNode GetCurrentNodeAndHintAncestorNodes (int upLevel)
86 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
91 public virtual SiteMapNode GetCurrentNodeAndHintNeighborhoodNodes (int upLevel, int downLevel)
93 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
94 if (downLevel < -1) throw new ArgumentOutOfRangeException ("downLevel");
99 public abstract SiteMapNode GetParentNode (SiteMapNode node);
101 public virtual SiteMapNode GetParentNodeRelativeToCurrentNodeAndHintDownFromParent (int walkupLevels, int relativeDepthFromWalkup)
103 if (walkupLevels < 0) throw new ArgumentOutOfRangeException ("walkupLevels");
104 if (relativeDepthFromWalkup < 0) throw new ArgumentOutOfRangeException ("relativeDepthFromWalkup");
106 SiteMapNode node = GetCurrentNodeAndHintAncestorNodes (walkupLevels);
107 for (int n=0; n<walkupLevels && node != null; n++)
108 node = GetParentNode (node);
110 if (node == null) return null;
112 HintNeighborhoodNodes (node, 0, relativeDepthFromWalkup);
116 public virtual SiteMapNode GetParentNodeRelativeToNodeAndHintDownFromParent (SiteMapNode node, int walkupLevels, int relativeDepthFromWalkup)
118 if (walkupLevels < 0) throw new ArgumentOutOfRangeException ("walkupLevels");
119 if (relativeDepthFromWalkup < 0) throw new ArgumentOutOfRangeException ("relativeDepthFromWalkup");
120 if (node == null) throw new ArgumentNullException ("node");
122 HintAncestorNodes (node, walkupLevels);
123 for (int n=0; n<walkupLevels && node != null; n++)
124 node = GetParentNode (node);
126 if (node == null) return null;
128 HintNeighborhoodNodes (node, 0, relativeDepthFromWalkup);
132 protected internal abstract SiteMapNode GetRootNodeCore ();
134 protected static SiteMapNode GetRootNodeCoreFromProvider (SiteMapProvider provider)
136 return provider.GetRootNodeCore ();
139 public virtual void HintAncestorNodes (SiteMapNode node, int upLevel)
141 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
142 if (node == null) throw new ArgumentNullException ("node");
145 public virtual void HintNeighborhoodNodes (SiteMapNode node, int upLevel, int downLevel)
147 if (upLevel < -1) throw new ArgumentOutOfRangeException ("upLevel");
148 if (downLevel < -1) throw new ArgumentOutOfRangeException ("downLevel");
149 if (node == null) throw new ArgumentNullException ("node");
152 protected virtual void RemoveNode (SiteMapNode node)
154 throw new NotImplementedException ();
157 public override void Initialize (string name, NameValueCollection attributes)
159 base.Initialize (name, attributes);
160 if (attributes["securityTrimmingEnabled"] != null)
161 securityTrimming = (bool) Convert.ChangeType (attributes ["securityTrimmingEnabled"], typeof(bool));
164 [MonoTODO ("need to implement cases 2 and 3")]
165 public virtual bool IsAccessibleToUser (HttpContext context, SiteMapNode node)
167 if (context == null) throw new ArgumentNullException ("context");
168 if (node == null) throw new ArgumentNullException ("node");
170 /* the node is accessible (according to msdn2)
173 * 1. the current user is in the node's Roles.
175 * 2. the current thread's WindowsIdentity has
176 * file access to the url. (and the url is
177 * located within the current application).
179 * 3. the <authorization> configuration element
180 * lists the current user as being authorized
181 * for the node's url. (and the url is located
182 * within the current application)
186 foreach (string rolename in node.Roles)
187 if (context.User.IsInRole (rolename))
196 return true; // the default should be false.
199 public virtual SiteMapNode CurrentNode {
201 if (HttpContext.Current != null) {
202 SiteMapNode ret = ResolveSiteMapNode (HttpContext.Current);
203 if (ret != null) return ret;
204 return FindSiteMapNode (HttpContext.Current);
210 public virtual SiteMapProvider ParentProvider {
211 get { return parentProvider; }
212 set { parentProvider = value; }
215 public virtual SiteMapProvider RootProvider {
218 if (rootProviderCache == null) {
219 SiteMapProvider current = this;
220 while (current.ParentProvider != null)
221 current = current.ParentProvider;
223 rootProviderCache = current;
226 return rootProviderCache;
230 protected SiteMapNode ResolveSiteMapNode (HttpContext context)
232 SiteMapResolveEventArgs args = new SiteMapResolveEventArgs (context, this);
233 if (SiteMapResolve != null) {
235 if (resolving) return null;
237 SiteMapNode r = SiteMapResolve (this, args);
246 public bool EnableLocalization {
247 get { return enableLocalization; }
248 set { enableLocalization = value; }
251 public bool SecurityTrimmingEnabled {
252 get { return securityTrimming; }
256 public string ResourceKey {
257 get { return resourceKey; }
258 set { resourceKey = value; }
261 public virtual SiteMapNode RootNode {
263 SiteMapNode node = GetRootNodeCore ();
264 return ReturnNodeIfAccessible (node);
268 public event SiteMapResolveEventHandler SiteMapResolve;
270 internal static SiteMapNode ReturnNodeIfAccessible (SiteMapNode node)
272 if (node.IsAccessibleToUser (HttpContext.Current))
275 throw new InvalidOperationException (); /* need