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 public class DerbyRoleProvider : RoleProvider
\r
48 ConnectionStringSettings connectionString;
\r
49 string applicationName;
\r
51 DbConnection CreateConnection ()
\r
53 OleDbConnection connection = new OleDbConnection (connectionString.ConnectionString);
\r
58 public override void AddUsersToRoles (string [] usernames, string [] rolenames)
\r
60 Hashtable h = new Hashtable ();
\r
62 foreach (string u in usernames) {
\r
64 throw new ArgumentNullException ("null element in usernames array");
\r
65 if (h.ContainsKey (u))
\r
66 throw new ArgumentException ("duplicate element in usernames array");
\r
67 if (u.Length == 0 || u.Length > 256 || u.IndexOf (",") != -1)
\r
68 throw new ArgumentException ("element in usernames array in illegal format");
\r
72 h = new Hashtable ();
\r
73 foreach (string r in rolenames) {
\r
75 throw new ArgumentNullException ("null element in rolenames array");
\r
76 if (h.ContainsKey (r))
\r
77 throw new ArgumentException ("duplicate element in rolenames array");
\r
78 if (r.Length == 0 || r.Length > 256 || r.IndexOf (",") != -1)
\r
79 throw new ArgumentException ("element in rolenames array in illegal format");
\r
83 using (DbConnection connection = CreateConnection ()) {
\r
84 int returnValue = DerbyRolesHelper.UsersInRoles_AddUsersToRoles (connection, ApplicationName, usernames, rolenames, DateTime.UtcNow);
\r
86 if (returnValue == 0)
\r
88 else if (returnValue == 2)
\r
89 throw new ProviderException ("One or more of the specified role names was not found.");
\r
90 else if (returnValue == 3)
\r
91 throw new ProviderException ("One or more of the specified user names is already associated with one or more of the specified role names.");
\r
93 throw new ProviderException ("Failed to create new user/role association.");
\r
97 public override void CreateRole (string rolename)
\r
99 if (rolename == null)
\r
100 throw new ArgumentNullException ("rolename");
\r
102 if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
\r
103 throw new ArgumentException ("rolename is in invalid format");
\r
105 using (DbConnection connection = CreateConnection ()) {
\r
106 int returnValue = DerbyRolesHelper.Roles_CreateRole (connection, ApplicationName, rolename);
\r
108 if (returnValue == 2)
\r
109 throw new ProviderException (rolename + " already exists in the database");
\r
115 public override bool DeleteRole (string rolename, bool throwOnPopulatedRole)
\r
117 if (rolename == null)
\r
118 throw new ArgumentNullException ("rolename");
\r
120 if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
\r
121 throw new ArgumentException ("rolename is in invalid format");
\r
123 using (DbConnection connection = CreateConnection ()) {
\r
124 int returnValue = DerbyRolesHelper.Roles_DeleteRole (connection, ApplicationName, rolename, throwOnPopulatedRole);
\r
126 if (returnValue == 0)
\r
128 if (returnValue == 2)
\r
129 return false; //role does not exists
\r
130 else if (returnValue == 3 && throwOnPopulatedRole)
\r
131 throw new ProviderException (rolename + " is not empty");
\r
137 public override string [] FindUsersInRole (string roleName, string usernameToMatch)
\r
139 if (roleName == null)
\r
140 throw new ArgumentNullException ("roleName");
\r
141 if (usernameToMatch == null)
\r
142 throw new ArgumentNullException ("usernameToMatch");
\r
143 if (roleName.Length == 0 || roleName.Length > 256 || roleName.IndexOf (",") != -1)
\r
144 throw new ArgumentException ("roleName is in invalid format");
\r
145 if (usernameToMatch.Length == 0 || usernameToMatch.Length > 256)
\r
146 throw new ArgumentException ("usernameToMatch is in invalid format");
\r
148 using (DbConnection connection = CreateConnection ()) {
\r
149 DbDataReader reader;
\r
150 ArrayList userList = new ArrayList ();
\r
151 int returnValue = DerbyRolesHelper.UsersInRoles_FindUsersInRole (connection, applicationName, roleName, usernameToMatch, out reader);
\r
153 if (returnValue == 2)
\r
154 throw new ProviderException ("roleName was not found in the database");
\r
157 if (reader == null)
\r
158 return new string [] { };
\r
160 while (reader.Read ())
\r
161 userList.Add (reader.GetString (0));
\r
163 return (string []) userList.ToArray (typeof (string));
\r
167 public override string [] GetAllRoles ()
\r
169 using (DbConnection connection = CreateConnection ()) {
\r
170 DbDataReader reader;
\r
171 ArrayList roleList = new ArrayList ();
\r
172 DerbyRolesHelper.Roles_GetAllRoles (connection, applicationName, out reader);
\r
174 if (reader == null)
\r
175 return new string [] { };
\r
177 while (reader.Read ())
\r
178 roleList.Add (reader.GetString (0));
\r
180 return (string []) roleList.ToArray (typeof (string));
\r
184 public override string [] GetRolesForUser (string username)
\r
186 if (username == null)
\r
187 throw new ArgumentNullException ("rolename");
\r
189 if (username.Length == 0 || username.Length > 256 || username.IndexOf (",") != -1)
\r
190 throw new ArgumentException ("username is in invalid format");
\r
192 using (DbConnection connection = CreateConnection ()) {
\r
193 DbDataReader reader;
\r
194 ArrayList roleList = new ArrayList ();
\r
195 int returnValue = DerbyRolesHelper.UsersInRoles_GetRolesForUser (connection, applicationName, username, out reader);
\r
197 if (returnValue == 2)
\r
198 throw new ProviderException ("username was not found in the database");
\r
201 if (reader == null)
\r
202 return new string [] { };
\r
204 while (reader.Read ())
\r
205 roleList.Add (reader.GetString (0));
\r
207 return (string []) roleList.ToArray (typeof (string));
\r
211 public override string [] GetUsersInRole (string rolename)
\r
213 if (rolename == null)
\r
214 throw new ArgumentNullException ("rolename");
\r
216 if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
\r
217 throw new ArgumentException ("rolename is in invalid format");
\r
219 using (DbConnection connection = CreateConnection ()) {
\r
220 DbDataReader reader;
\r
221 ArrayList roleList = new ArrayList ();
\r
222 int returnValue = DerbyRolesHelper.UsersInRoles_GetUsersInRoles (connection, applicationName, rolename, out reader);
\r
224 if (returnValue == 2)
\r
225 throw new ProviderException ("rolename was not found in the database");
\r
228 if (reader == null)
\r
229 return new string [] { };
\r
231 while (reader.Read ())
\r
232 roleList.Add (reader.GetString (0));
\r
234 return (string []) roleList.ToArray (typeof (string));
\r
238 string GetStringConfigValue (NameValueCollection config, string name, string def)
\r
241 string val = config [name];
\r
247 public override void Initialize (string name, NameValueCollection config)
\r
249 if (config == null)
\r
250 throw new ArgumentNullException ("config");
\r
252 base.Initialize (name, config);
\r
254 applicationName = config ["applicationName"];
\r
255 string connectionStringName = config ["connectionStringName"];
\r
257 if (applicationName.Length > 256)
\r
258 throw new ProviderException ("The ApplicationName attribute must be 256 characters long or less.");
\r
259 if (connectionStringName == null || connectionStringName.Length == 0)
\r
260 throw new ProviderException ("The ConnectionStringName attribute must be present and non-zero length.");
\r
262 // XXX check connectionStringName and commandTimeout
\r
264 connectionString = WebConfigurationManager.ConnectionStrings [connectionStringName];
\r
265 if (connectionString == null)
\r
266 throw new ProviderException (String.Format("The connection name '{0}' was not found in the applications configuration or the connection string is empty.", connectionStringName));
\r
268 DerbyDBSchema.InitializeSchema (connectionString.ConnectionString);
\r
271 public override bool IsUserInRole (string username, string rolename)
\r
273 if (username == null)
\r
274 throw new ArgumentNullException ("rolename");
\r
275 if (username.Length == 0 || username.Length > 256 || username.IndexOf (",") != -1)
\r
276 throw new ArgumentException ("username is in invalid format");
\r
277 if (rolename == null)
\r
278 throw new ArgumentNullException ("rolename");
\r
279 if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
\r
280 throw new ArgumentException ("rolename is in invalid format");
\r
282 using (DbConnection connection = CreateConnection ()) {
\r
283 int returnValue = DerbyRolesHelper.UsersInRoles_IsUserInRole (connection, ApplicationName, username, rolename);
\r
285 if (returnValue == 4)
\r
292 public override void RemoveUsersFromRoles (string [] usernames, string [] rolenames)
\r
294 Hashtable h = new Hashtable ();
\r
296 foreach (string u in usernames) {
\r
298 throw new ArgumentNullException ("null element in usernames array");
\r
299 if (h.ContainsKey (u))
\r
300 throw new ArgumentException ("duplicate element in usernames array");
\r
301 if (u.Length == 0 || u.Length > 256 || u.IndexOf (",") != -1)
\r
302 throw new ArgumentException ("element in usernames array in illegal format");
\r
306 h = new Hashtable ();
\r
307 foreach (string r in rolenames) {
\r
309 throw new ArgumentNullException ("null element in rolenames array");
\r
310 if (h.ContainsKey (r))
\r
311 throw new ArgumentException ("duplicate element in rolenames array");
\r
312 if (r.Length == 0 || r.Length > 256 || r.IndexOf (",") != -1)
\r
313 throw new ArgumentException ("element in rolenames array in illegal format");
\r
317 using (DbConnection connection = CreateConnection ()) {
\r
318 int returnValue = DerbyRolesHelper.UsersInRoles_RemoveUsersFromRoles (connection, ApplicationName, usernames, rolenames);
\r
320 if (returnValue == 0)
\r
322 else if (returnValue == 2)
\r
323 throw new ProviderException ("One or more of the specified user names was not found.");
\r
324 else if (returnValue == 3)
\r
325 throw new ProviderException ("One or more of the specified role names was not found.");
\r
326 else if (returnValue == 4)
\r
327 throw new ProviderException ("One or more of the specified user names is not associated with one or more of the specified role names.");
\r
329 throw new ProviderException ("Failed to remove users from roles");
\r
333 public override bool RoleExists (string rolename)
\r
335 if (rolename == null)
\r
336 throw new ArgumentNullException ("rolename");
\r
338 if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
\r
339 throw new ArgumentException ("rolename is in invalid format");
\r
341 using (DbConnection connection = CreateConnection ()) {
\r
342 int returnValue = DerbyRolesHelper.Roles_RoleExists (connection, ApplicationName, rolename);
\r
344 if (returnValue == 2)
\r
351 public override string ApplicationName
\r
353 get { return applicationName; }
\r
356 applicationName = value;
\r