1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| using System;
using Susing System;
using System.Web;
using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Configuration;
using System.Web.Configuration;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Configuration.Provider;
using System.Security.Permissions;
using System.Data.Common;
using System.Data;
[SqlClientPermission(SecurityAction.Demand, Unrestricted = true)]
public class SqlSiteMapProvider : StaticSiteMapProvider
{
private const string _errmsg1 = "Missing node ID";
private const string _errmsg2 = "Duplicate node ID";
private const string _errmsg3 = "Missing parent ID";
private const string _errmsg4 = "Invalid parent ID";
private const string _errmsg5 =
"Empty or missing connectionStringName";
private const string _errmsg6 = "Missing connection string";
private const string _errmsg7 = "Empty connection string";
private string _connect;
private int _indexID, _indexTitle, _indexUrl,
_indexDesc, _indexRoles, _indexParent;
private Dictionary<int, SiteMapNode> _nodes =
new Dictionary<int, SiteMapNode>(16);
private SiteMapNode _root;
public override void Initialize(string name,
NameValueCollection config)
{
// Verify that config isn't null
if (config == null)
throw new ArgumentNullException("config");
// Assign the provider a default name if it doesn't have one
if (String.IsNullOrEmpty(name))
name = "SqlSiteMapProvider";
// Add a default "description" attribute to config if the
// attribute doesn't exist or is empty
if (string.IsNullOrEmpty(config["description"]))
{
config.Remove("description");
config.Add("description", "SQL site map provider");
}
// Call the base class's Initialize method
base.Initialize(name, config);
// Initialize _connect
string connect = config["connectionStringName"];
if (String.IsNullOrEmpty(connect))
throw new ProviderException(_errmsg5);
config.Remove("connectionStringName");
if (WebConfigurationManager.ConnectionStrings[connect] == null)
throw new ProviderException(_errmsg6);
_connect = WebConfigurationManager.ConnectionStrings
[connect].ConnectionString;
if (String.IsNullOrEmpty(_connect))
throw new ProviderException(_errmsg7);
// In beta 2, SiteMapProvider processes the
// securityTrimmingEnabled attribute but fails to remove it.
// Remove it now so we can check for unrecognized
// configuration attributes.
if (config["securityTrimmingEnabled"] != null)
config.Remove("securityTrimmingEnabled");
// Throw an exception if unrecognized attributes remain
if (config.Count > 0)
{
string attr = config.GetKey(0);
if (!String.IsNullOrEmpty(attr))
throw new ProviderException
("Unrecognized attribute: " + attr);
}
}
public override SiteMapNode BuildSiteMap()
{
lock (this)
{
// Return immediately if this method has been called before
if (_root != null)
return _root;
// Query the database for site map nodes
SqlConnection connection = new SqlConnection(_connect);
try
{
connection.Open();
SqlCommand command =
new SqlCommand("siteMenu", connection);
command.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = command.ExecuteReader();
_indexID = reader.GetOrdinal("idMenu");
_indexUrl = reader.GetOrdinal("url");
_indexTitle = reader.GetOrdinal("Menu");
//_indexDesc = reader.GetOrdinal("Description");
//_indexRoles = reader.GetOrdinal("Roles");
_indexParent = reader.GetOrdinal("idParent");
if (reader.Read())
{
// Create the root SiteMapNode and add it to
// the site map
_root = CreateSiteMapNodeFromDataReader(reader);
AddNode(_root, null);
// Build a tree of SiteMapNodes underneath
// the root node
while (reader.Read())
{
// Create another site map node and
// add it to the site map
SiteMapNode node =
CreateSiteMapNodeFromDataReader(reader);
AddNode(node,
GetParentNodeFromDataReader(reader));
}
}
}
finally
{
connection.Close();
}
// Return the root SiteMapNode
return _root;
}
}
protected override SiteMapNode GetRootNodeCore()
{
BuildSiteMap();
return _root;
}
// Helper methods
private SiteMapNode
CreateSiteMapNodeFromDataReader(DbDataReader reader)
{
// Make sure the node ID is present
if (reader.IsDBNull(_indexID))
throw new ProviderException(_errmsg1);
// Get the node ID from the DataReader
int id = reader.GetInt32(_indexID);
// Make sure the node ID is unique
if (_nodes.ContainsKey(id))
throw new ProviderException(_errmsg2);
// Get title, URL, description, and roles from the DataReader
string title = reader.IsDBNull(_indexTitle) ?
null : reader.GetString(_indexTitle).Trim();
string url = reader.IsDBNull(_indexUrl) ?
null : reader.GetString(_indexUrl).Trim();
//string description = reader.IsDBNull(_indexDesc) ?
//null : reader.GetString(_indexDesc).Trim();
//string roles = reader.IsDBNull(_indexRoles) ?
//null : reader.GetString(_indexRoles).Trim();
// If roles were specified, turn the list into a string array
/*string[] rolelist = null;
if (!String.IsNullOrEmpty(roles))
rolelist = roles.Split(new char[] { ',', ';' }, 512);*/
// Create a SiteMapNode
SiteMapNode node = new SiteMapNode(this, id.ToString(), url, title);
// Record the node in the _nodes dictionary
_nodes.Add(id, node);
// Return the node
return node;
}
private SiteMapNode
GetParentNodeFromDataReader(DbDataReader reader)
{
// Make sure the parent ID is present
if (reader.IsDBNull(_indexParent))
throw new ProviderException(_errmsg3);
// Get the parent ID from the DataReader
int pid = reader.GetInt32(_indexParent);
// Make sure the parent ID is valid
if (!_nodes.ContainsKey(pid))
throw new ProviderException(_errmsg4);
// Return the parent SiteMapNode
return _nodes[pid];
}
} |
Partager