Add [Category ("NotWorking")] to failing test.
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.Security / DerbyRoleProvider.cs
1 //\r
2 // Mainsoft.Web.Security.DerbyRoleProvider\r
3 //\r
4 // Authors:\r
5 //      Ben Maurer (bmaurer@users.sourceforge.net)\r
6 //      Chris Toshok (toshok@ximian.com)\r
7 //      Vladimir Krasnov (vladimirk@mainsoft.com)\r
8 //\r
9 //\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
17 // \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
20 // \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
28 //\r
29 \r
30 #if NET_2_0\r
31 \r
32 using System;\r
33 using System.Collections;\r
34 using System.Collections.Specialized;\r
35 using System.Data;\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
42 \r
43 namespace Mainsoft.Web.Security\r
44 {\r
45         /// <summary>\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
48         /// </summary>\r
49         public class DerbyRoleProvider : RoleProvider\r
50         {\r
51                 ConnectionStringSettings connectionString;\r
52                 string applicationName;\r
53                 bool schemaChecked = false;\r
54                 DerbyUnloadManager.DerbyShutDownPolicy shutDownPolicy = DerbyUnloadManager.DerbyShutDownPolicy.Default;\r
55 \r
56                 DbConnection CreateConnection ()\r
57                 {\r
58                         if (!schemaChecked) {\r
59                                 DerbyDBSchema.CheckSchema (connectionString.ConnectionString);\r
60                                 schemaChecked = true;\r
61 \r
62                                 DerbyUnloadManager.RegisterUnloadHandler (connectionString.ConnectionString, shutDownPolicy);\r
63                         }\r
64 \r
65                         OleDbConnection connection = new OleDbConnection (connectionString.ConnectionString);\r
66                         connection.Open ();\r
67                         return connection;\r
68                 }\r
69 \r
70                 public override void AddUsersToRoles (string [] usernames, string [] rolenames)\r
71                 {\r
72                         Hashtable h = new Hashtable ();\r
73 \r
74                         foreach (string u in usernames) {\r
75                                 if (u == null)\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
81                                 h.Add (u, u);\r
82                         }\r
83 \r
84                         h = new Hashtable ();\r
85                         foreach (string r in rolenames) {\r
86                                 if (r == null)\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
92                                 h.Add (r, r);\r
93                         } \r
94                         \r
95                         using (DbConnection connection = CreateConnection ()) {\r
96                                 int returnValue = DerbyRolesHelper.UsersInRoles_AddUsersToRoles (connection, ApplicationName, usernames, rolenames, DateTime.UtcNow);\r
97 \r
98                                 if (returnValue == 0)\r
99                                         return;\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
104                                 else\r
105                                         throw new ProviderException ("Failed to create new user/role association.");\r
106                         }\r
107                 }\r
108 \r
109                 public override void CreateRole (string rolename)\r
110                 {\r
111                         if (rolename == null)\r
112                                 throw new ArgumentNullException ("rolename");\r
113 \r
114                         if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)\r
115                                 throw new ArgumentException ("rolename is in invalid format");\r
116 \r
117                         using (DbConnection connection = CreateConnection ()) {\r
118                                 int returnValue = DerbyRolesHelper.Roles_CreateRole (connection, ApplicationName, rolename);\r
119                                 \r
120                                 if (returnValue == 2)\r
121                                         throw new ProviderException (rolename + " already exists in the database");\r
122                                 else\r
123                                         return;\r
124                         }\r
125                 }\r
126 \r
127                 public override bool DeleteRole (string rolename, bool throwOnPopulatedRole)\r
128                 {\r
129                         if (rolename == null)\r
130                                 throw new ArgumentNullException ("rolename");\r
131 \r
132                         if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)\r
133                                 throw new ArgumentException ("rolename is in invalid format");\r
134 \r
135                         using (DbConnection connection = CreateConnection ()) {\r
136                                 int returnValue = DerbyRolesHelper.Roles_DeleteRole (connection, ApplicationName, rolename, throwOnPopulatedRole);\r
137 \r
138                                 if (returnValue == 0)\r
139                                         return true;\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
144                                 else\r
145                                         return false;\r
146                         }\r
147                 }\r
148 \r
149                 public override string [] FindUsersInRole (string roleName, string usernameToMatch)\r
150                 {\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
159 \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
164 \r
165                                 if (returnValue == 2)\r
166                                         throw new ProviderException ("The role '" + roleName + "' was not found.");\r
167 \r
168                                 using (reader) {\r
169                                         if (reader == null)\r
170                                                 return new string [] { };\r
171 \r
172                                         while (reader.Read ())\r
173                                                 userList.Add (reader.GetString (0));\r
174                                 }\r
175                                 return (string []) userList.ToArray (typeof (string));\r
176                         }\r
177                 }\r
178 \r
179                 public override string [] GetAllRoles ()\r
180                 {\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
185                                 using (reader) {\r
186                                         if (reader == null)\r
187                                                 return new string [] { };\r
188 \r
189                                         while (reader.Read ())\r
190                                                 roleList.Add (reader.GetString (0));\r
191                                 }\r
192                                 return (string []) roleList.ToArray (typeof (string));\r
193                         }\r
194                 }\r
195 \r
196                 public override string [] GetRolesForUser (string username)\r
197                 {\r
198                         if (username == null)\r
199                                 throw new ArgumentNullException ("rolename");\r
200 \r
201                         if (username.Length == 0 || username.Length > 256 || username.IndexOf (",") != -1)\r
202                                 throw new ArgumentException ("username is in invalid format");\r
203 \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
208 \r
209                                 if (returnValue == 2)\r
210                                         throw new ProviderException ("username was not found in the database");\r
211 \r
212                                 using (reader) {\r
213                                         if (reader == null)\r
214                                                 return new string [] { };\r
215 \r
216                                         while (reader.Read ())\r
217                                                 roleList.Add (reader.GetString (0));\r
218                                 }\r
219                                 return (string []) roleList.ToArray (typeof (string));\r
220                         }\r
221                 }\r
222 \r
223                 public override string [] GetUsersInRole (string rolename)\r
224                 {\r
225                         if (rolename == null)\r
226                                 throw new ArgumentNullException ("rolename");\r
227 \r
228                         if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)\r
229                                 throw new ArgumentException ("rolename is in invalid format");\r
230 \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
235 \r
236                                 if (returnValue == 2)\r
237                                         throw new ProviderException ("The role '" + rolename + "' was not found.");\r
238 \r
239                                 using (reader) {\r
240                                         if (reader == null)\r
241                                                 return new string [] { };\r
242 \r
243                                         while (reader.Read ())\r
244                                                 roleList.Add (reader.GetString (0));\r
245                                 }\r
246                                 return (string []) roleList.ToArray (typeof (string));\r
247                         }\r
248                 }\r
249 \r
250                 string GetStringConfigValue (NameValueCollection config, string name, string def)\r
251                 {\r
252                         string rv = def;\r
253                         string val = config [name];\r
254                         if (val != null)\r
255                                 rv = val;\r
256                         return rv;\r
257                 }\r
258 \r
259                 public override void Initialize (string name, NameValueCollection config)\r
260                 {\r
261                         if (config == null)\r
262                                 throw new ArgumentNullException ("config");\r
263 \r
264                         base.Initialize (name, config);\r
265 \r
266                         applicationName = config ["applicationName"];\r
267                         string connectionStringName = config ["connectionStringName"];\r
268 \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
273 \r
274                         // XXX check connectionStringName and commandTimeout\r
275 \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
279 \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
283                 }\r
284 \r
285                 public override bool IsUserInRole (string username, string rolename)\r
286                 {\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
295 \r
296                         using (DbConnection connection = CreateConnection ()) {\r
297                                 int returnValue = DerbyRolesHelper.UsersInRoles_IsUserInRole (connection, ApplicationName, username, rolename);\r
298 \r
299                                 if (returnValue == 4)\r
300                                         return true;\r
301 \r
302                                 return false;\r
303                         }\r
304                 }\r
305 \r
306                 public override void RemoveUsersFromRoles (string [] usernames, string [] rolenames)\r
307                 {\r
308                         Hashtable h = new Hashtable ();\r
309 \r
310                         foreach (string u in usernames) {\r
311                                 if (u == null)\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
317                                 h.Add (u, u);\r
318                         }\r
319 \r
320                         h = new Hashtable ();\r
321                         foreach (string r in rolenames) {\r
322                                 if (r == null)\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
328                                 h.Add (r, r);\r
329                         } \r
330 \r
331                         using (DbConnection connection = CreateConnection ()) {\r
332                                 int returnValue = DerbyRolesHelper.UsersInRoles_RemoveUsersFromRoles (connection, ApplicationName, usernames, rolenames);\r
333 \r
334                                 if (returnValue == 0)\r
335                                         return;\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
342                                 else\r
343                                         throw new ProviderException ("Failed to remove users from roles");\r
344                         }\r
345                 }\r
346 \r
347                 public override bool RoleExists (string rolename)\r
348                 {\r
349                         if (rolename == null)\r
350                                 throw new ArgumentNullException ("rolename");\r
351 \r
352                         if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)\r
353                                 throw new ArgumentException ("rolename is in invalid format");\r
354 \r
355                         using (DbConnection connection = CreateConnection ()) {\r
356                                 int returnValue = DerbyRolesHelper.Roles_RoleExists (connection, ApplicationName, rolename);\r
357 \r
358                                 if (returnValue == 2)\r
359                                         return true;\r
360 \r
361                                 return false;\r
362                         }\r
363                 }\r
364 \r
365                 public override string ApplicationName\r
366                 {\r
367                         get { return applicationName; }\r
368                         set\r
369                         {\r
370                                 applicationName = value;\r
371                         }\r
372                 }\r
373         }\r
374 }\r
375 #endif\r
376 \r