2 // Mainsoft.Web.Security.DerbyRoleProvider
\r
5 // Ben Maurer (bmaurer@users.sourceforge.net)
\r
6 // Chris Toshok (toshok@ximian.com)
\r
7 // Vladimir Krasnov (vladimirk@mainsoft.com)
\r
10 // Permission is hereby granted, free of charge, to any person obtaining
\r
11 // a copy of this software and associated documentation files (the
\r
12 // "Software"), to deal in the Software without restriction, including
\r
13 // without limitation the rights to use, copy, modify, merge, publish,
\r
14 // distribute, sublicense, and/or sell copies of the Software, and to
\r
15 // permit persons to whom the Software is furnished to do so, subject to
\r
16 // the following conditions:
\r
18 // The above copyright notice and this permission notice shall be
\r
19 // included in all copies or substantial portions of the Software.
\r
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
33 using System.Collections;
\r
34 using System.Collections.Specialized;
\r
36 using System.Data.OleDb;
\r
37 using System.Data.Common;
\r
38 using System.Configuration;
\r
39 using System.Configuration.Provider;
\r
40 using System.Web.Configuration;
\r
41 using System.Web.Security;
\r
43 namespace Mainsoft.Web.Security
\r
46 /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
\r
47 /// <para>Manages storage of role membership information for an ASP.NET application in a Derby database.</para>
\r
49 public class DerbyRoleProvider : RoleProvider
\r
51 ConnectionStringSettings connectionString;
\r
52 string applicationName;
\r
53 bool schemaChecked = false;
\r
54 DerbyUnloadManager.DerbyShutDownPolicy shutDownPolicy = DerbyUnloadManager.DerbyShutDownPolicy.Default;
\r
56 DbConnection CreateConnection ()
\r
58 if (!schemaChecked) {
\r
59 DerbyDBSchema.CheckSchema (connectionString.ConnectionString);
\r
60 schemaChecked = true;
\r
62 DerbyUnloadManager.RegisterUnloadHandler (connectionString.ConnectionString, shutDownPolicy);
\r
65 OleDbConnection connection = new OleDbConnection (connectionString.ConnectionString);
\r
70 public override void AddUsersToRoles (string [] usernames, string [] rolenames)
\r
72 Hashtable h = new Hashtable ();
\r
74 foreach (string u in usernames) {
\r
76 throw new ArgumentNullException ("null element in usernames array");
\r
77 if (h.ContainsKey (u))
\r
78 throw new ArgumentException ("duplicate element in usernames array");
\r
79 if (u.Length == 0 || u.Length > 256 || u.IndexOf (",") != -1)
\r
80 throw new ArgumentException ("element in usernames array in illegal format");
\r
84 h = new Hashtable ();
\r
85 foreach (string r in rolenames) {
\r
87 throw new ArgumentNullException ("null element in rolenames array");
\r
88 if (h.ContainsKey (r))
\r
89 throw new ArgumentException ("duplicate element in rolenames array");
\r
90 if (r.Length == 0 || r.Length > 256 || r.IndexOf (",") != -1)
\r
91 throw new ArgumentException ("element in rolenames array in illegal format");
\r
95 using (DbConnection connection = CreateConnection ()) {
\r
96 int returnValue = DerbyRolesHelper.UsersInRoles_AddUsersToRoles (connection, ApplicationName, usernames, rolenames, DateTime.UtcNow);
\r
98 if (returnValue == 0)
\r
100 else if (returnValue == 2)
\r
101 throw new ProviderException ("One or more of the specified role names was not found.");
\r
102 else if (returnValue == 3)
\r
103 throw new ProviderException ("One or more of the specified user names is already associated with one or more of the specified role names.");
\r
105 throw new ProviderException ("Failed to create new user/role association.");
\r
109 public override void CreateRole (string rolename)
\r
111 if (rolename == null)
\r
112 throw new ArgumentNullException ("rolename");
\r
114 if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
\r
115 throw new ArgumentException ("rolename is in invalid format");
\r
117 using (DbConnection connection = CreateConnection ()) {
\r
118 int returnValue = DerbyRolesHelper.Roles_CreateRole (connection, ApplicationName, rolename);
\r
120 if (returnValue == 2)
\r
121 throw new ProviderException (rolename + " already exists in the database");
\r
127 public override bool DeleteRole (string rolename, bool throwOnPopulatedRole)
\r
129 if (rolename == null)
\r
130 throw new ArgumentNullException ("rolename");
\r
132 if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
\r
133 throw new ArgumentException ("rolename is in invalid format");
\r
135 using (DbConnection connection = CreateConnection ()) {
\r
136 int returnValue = DerbyRolesHelper.Roles_DeleteRole (connection, ApplicationName, rolename, throwOnPopulatedRole);
\r
138 if (returnValue == 0)
\r
140 if (returnValue == 2)
\r
141 return false; //role does not exists
\r
142 else if (returnValue == 3 && throwOnPopulatedRole)
\r
143 throw new ProviderException (rolename + " is not empty");
\r
149 public override string [] FindUsersInRole (string roleName, string usernameToMatch)
\r
151 if (roleName == null)
\r
152 throw new ArgumentNullException ("roleName");
\r
153 if (usernameToMatch == null)
\r
154 throw new ArgumentNullException ("usernameToMatch");
\r
155 if (roleName.Length == 0 || roleName.Length > 256 || roleName.IndexOf (",") != -1)
\r
156 throw new ArgumentException ("roleName is in invalid format");
\r
157 if (usernameToMatch.Length == 0 || usernameToMatch.Length > 256)
\r
158 throw new ArgumentException ("usernameToMatch is in invalid format");
\r
160 using (DbConnection connection = CreateConnection ()) {
\r
161 DbDataReader reader;
\r
162 ArrayList userList = new ArrayList ();
\r
163 int returnValue = DerbyRolesHelper.UsersInRoles_FindUsersInRole (connection, applicationName, roleName, usernameToMatch, out reader);
\r
165 if (returnValue == 2)
\r
166 throw new ProviderException ("The role '" + roleName + "' was not found.");
\r
169 if (reader == null)
\r
170 return new string [] { };
\r
172 while (reader.Read ())
\r
173 userList.Add (reader.GetString (0));
\r
175 return (string []) userList.ToArray (typeof (string));
\r
179 public override string [] GetAllRoles ()
\r
181 using (DbConnection connection = CreateConnection ()) {
\r
182 DbDataReader reader;
\r
183 ArrayList roleList = new ArrayList ();
\r
184 DerbyRolesHelper.Roles_GetAllRoles (connection, applicationName, out reader);
\r
186 if (reader == null)
\r
187 return new string [] { };
\r
189 while (reader.Read ())
\r
190 roleList.Add (reader.GetString (0));
\r
192 return (string []) roleList.ToArray (typeof (string));
\r
196 public override string [] GetRolesForUser (string username)
\r
198 if (username == null)
\r
199 throw new ArgumentNullException ("rolename");
\r
201 if (username.Length == 0 || username.Length > 256 || username.IndexOf (",") != -1)
\r
202 throw new ArgumentException ("username is in invalid format");
\r
204 using (DbConnection connection = CreateConnection ()) {
\r
205 DbDataReader reader;
\r
206 ArrayList roleList = new ArrayList ();
\r
207 int returnValue = DerbyRolesHelper.UsersInRoles_GetRolesForUser (connection, applicationName, username, out reader);
\r
209 if (returnValue == 2)
\r
210 throw new ProviderException ("username was not found in the database");
\r
213 if (reader == null)
\r
214 return new string [] { };
\r
216 while (reader.Read ())
\r
217 roleList.Add (reader.GetString (0));
\r
219 return (string []) roleList.ToArray (typeof (string));
\r
223 public override string [] GetUsersInRole (string rolename)
\r
225 if (rolename == null)
\r
226 throw new ArgumentNullException ("rolename");
\r
228 if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
\r
229 throw new ArgumentException ("rolename is in invalid format");
\r
231 using (DbConnection connection = CreateConnection ()) {
\r
232 DbDataReader reader;
\r
233 ArrayList roleList = new ArrayList ();
\r
234 int returnValue = DerbyRolesHelper.UsersInRoles_GetUsersInRoles (connection, applicationName, rolename, out reader);
\r
236 if (returnValue == 2)
\r
237 throw new ProviderException ("The role '" + rolename + "' was not found.");
\r
240 if (reader == null)
\r
241 return new string [] { };
\r
243 while (reader.Read ())
\r
244 roleList.Add (reader.GetString (0));
\r
246 return (string []) roleList.ToArray (typeof (string));
\r
250 string GetStringConfigValue (NameValueCollection config, string name, string def)
\r
253 string val = config [name];
\r
259 public override void Initialize (string name, NameValueCollection config)
\r
261 if (config == null)
\r
262 throw new ArgumentNullException ("config");
\r
264 base.Initialize (name, config);
\r
266 applicationName = config ["applicationName"];
\r
267 string connectionStringName = config ["connectionStringName"];
\r
269 if (applicationName.Length > 256)
\r
270 throw new ProviderException ("The ApplicationName attribute must be 256 characters long or less.");
\r
271 if (connectionStringName == null || connectionStringName.Length == 0)
\r
272 throw new ProviderException ("The ConnectionStringName attribute must be present and non-zero length.");
\r
274 // XXX check connectionStringName and commandTimeout
\r
276 connectionString = WebConfigurationManager.ConnectionStrings [connectionStringName];
\r
277 if (connectionString == null)
\r
278 throw new ProviderException (String.Format("The connection name '{0}' was not found in the applications configuration or the connection string is empty.", connectionStringName));
\r
280 string shutdown = config ["shutdown"];
\r
281 if (!String.IsNullOrEmpty (shutdown))
\r
282 shutDownPolicy = (DerbyUnloadManager.DerbyShutDownPolicy) Enum.Parse (typeof (DerbyUnloadManager.DerbyShutDownPolicy), shutdown, true);
\r
285 public override bool IsUserInRole (string username, string rolename)
\r
287 if (username == null)
\r
288 throw new ArgumentNullException ("rolename");
\r
289 if (username.Length == 0 || username.Length > 256 || username.IndexOf (",") != -1)
\r
290 throw new ArgumentException ("username is in invalid format");
\r
291 if (rolename == null)
\r
292 throw new ArgumentNullException ("rolename");
\r
293 if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
\r
294 throw new ArgumentException ("rolename is in invalid format");
\r
296 using (DbConnection connection = CreateConnection ()) {
\r
297 int returnValue = DerbyRolesHelper.UsersInRoles_IsUserInRole (connection, ApplicationName, username, rolename);
\r
299 if (returnValue == 4)
\r
306 public override void RemoveUsersFromRoles (string [] usernames, string [] rolenames)
\r
308 Hashtable h = new Hashtable ();
\r
310 foreach (string u in usernames) {
\r
312 throw new ArgumentNullException ("null element in usernames array");
\r
313 if (h.ContainsKey (u))
\r
314 throw new ArgumentException ("duplicate element in usernames array");
\r
315 if (u.Length == 0 || u.Length > 256 || u.IndexOf (",") != -1)
\r
316 throw new ArgumentException ("element in usernames array in illegal format");
\r
320 h = new Hashtable ();
\r
321 foreach (string r in rolenames) {
\r
323 throw new ArgumentNullException ("null element in rolenames array");
\r
324 if (h.ContainsKey (r))
\r
325 throw new ArgumentException ("duplicate element in rolenames array");
\r
326 if (r.Length == 0 || r.Length > 256 || r.IndexOf (",") != -1)
\r
327 throw new ArgumentException ("element in rolenames array in illegal format");
\r
331 using (DbConnection connection = CreateConnection ()) {
\r
332 int returnValue = DerbyRolesHelper.UsersInRoles_RemoveUsersFromRoles (connection, ApplicationName, usernames, rolenames);
\r
334 if (returnValue == 0)
\r
336 else if (returnValue == 2)
\r
337 throw new ProviderException ("One or more of the specified user names was not found.");
\r
338 else if (returnValue == 3)
\r
339 throw new ProviderException ("One or more of the specified role names was not found.");
\r
340 else if (returnValue == 4)
\r
341 throw new ProviderException ("One or more of the specified user names is not associated with one or more of the specified role names.");
\r
343 throw new ProviderException ("Failed to remove users from roles");
\r
347 public override bool RoleExists (string rolename)
\r
349 if (rolename == null)
\r
350 throw new ArgumentNullException ("rolename");
\r
352 if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
\r
353 throw new ArgumentException ("rolename is in invalid format");
\r
355 using (DbConnection connection = CreateConnection ()) {
\r
356 int returnValue = DerbyRolesHelper.Roles_RoleExists (connection, ApplicationName, rolename);
\r
358 if (returnValue == 2)
\r
365 public override string ApplicationName
\r
367 get { return applicationName; }
\r
370 applicationName = value;
\r