Mark tests as not working under TARGET_JVM
[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 \r
46         public class DerbyRoleProvider : RoleProvider\r
47         {\r
48                 ConnectionStringSettings connectionString;\r
49                 string applicationName;\r
50                 bool schemaChecked = false;\r
51                 DerbyUnloadManager.DerbyShutDownPolicy shutDownPolicy = DerbyUnloadManager.DerbyShutDownPolicy.Default;\r
52 \r
53                 DbConnection CreateConnection ()\r
54                 {\r
55                         if (!schemaChecked) {\r
56                                 DerbyDBSchema.CheckSchema (connectionString.ConnectionString);\r
57                                 schemaChecked = true;\r
58 \r
59                                 DerbyUnloadManager.RegisterUnloadHandler (connectionString.ConnectionString, shutDownPolicy);\r
60                         }\r
61 \r
62                         OleDbConnection connection = new OleDbConnection (connectionString.ConnectionString);\r
63                         connection.Open ();\r
64                         return connection;\r
65                 }\r
66 \r
67                 public override void AddUsersToRoles (string [] usernames, string [] rolenames)\r
68                 {\r
69                         Hashtable h = new Hashtable ();\r
70 \r
71                         foreach (string u in usernames) {\r
72                                 if (u == null)\r
73                                         throw new ArgumentNullException ("null element in usernames array");\r
74                                 if (h.ContainsKey (u))\r
75                                         throw new ArgumentException ("duplicate element in usernames array");\r
76                                 if (u.Length == 0 || u.Length > 256 || u.IndexOf (",") != -1)\r
77                                         throw new ArgumentException ("element in usernames array in illegal format");\r
78                                 h.Add (u, u);\r
79                         }\r
80 \r
81                         h = new Hashtable ();\r
82                         foreach (string r in rolenames) {\r
83                                 if (r == null)\r
84                                         throw new ArgumentNullException ("null element in rolenames array");\r
85                                 if (h.ContainsKey (r))\r
86                                         throw new ArgumentException ("duplicate element in rolenames array");\r
87                                 if (r.Length == 0 || r.Length > 256 || r.IndexOf (",") != -1)\r
88                                         throw new ArgumentException ("element in rolenames array in illegal format");\r
89                                 h.Add (r, r);\r
90                         } \r
91                         \r
92                         using (DbConnection connection = CreateConnection ()) {\r
93                                 int returnValue = DerbyRolesHelper.UsersInRoles_AddUsersToRoles (connection, ApplicationName, usernames, rolenames, DateTime.UtcNow);\r
94 \r
95                                 if (returnValue == 0)\r
96                                         return;\r
97                                 else if (returnValue == 2)\r
98                                         throw new ProviderException ("One or more of the specified role names was not found.");\r
99                                 else if (returnValue == 3)\r
100                                         throw new ProviderException ("One or more of the specified user names is already associated with one or more of the specified role names.");\r
101                                 else\r
102                                         throw new ProviderException ("Failed to create new user/role association.");\r
103                         }\r
104                 }\r
105 \r
106                 public override void CreateRole (string rolename)\r
107                 {\r
108                         if (rolename == null)\r
109                                 throw new ArgumentNullException ("rolename");\r
110 \r
111                         if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)\r
112                                 throw new ArgumentException ("rolename is in invalid format");\r
113 \r
114                         using (DbConnection connection = CreateConnection ()) {\r
115                                 int returnValue = DerbyRolesHelper.Roles_CreateRole (connection, ApplicationName, rolename);\r
116                                 \r
117                                 if (returnValue == 2)\r
118                                         throw new ProviderException (rolename + " already exists in the database");\r
119                                 else\r
120                                         return;\r
121                         }\r
122                 }\r
123 \r
124                 public override bool DeleteRole (string rolename, bool throwOnPopulatedRole)\r
125                 {\r
126                         if (rolename == null)\r
127                                 throw new ArgumentNullException ("rolename");\r
128 \r
129                         if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)\r
130                                 throw new ArgumentException ("rolename is in invalid format");\r
131 \r
132                         using (DbConnection connection = CreateConnection ()) {\r
133                                 int returnValue = DerbyRolesHelper.Roles_DeleteRole (connection, ApplicationName, rolename, throwOnPopulatedRole);\r
134 \r
135                                 if (returnValue == 0)\r
136                                         return true;\r
137                                 if (returnValue == 2)\r
138                                         return false; //role does not exists\r
139                                 else if (returnValue == 3 && throwOnPopulatedRole)\r
140                                         throw new ProviderException (rolename + " is not empty");\r
141                                 else\r
142                                         return false;\r
143                         }\r
144                 }\r
145 \r
146                 public override string [] FindUsersInRole (string roleName, string usernameToMatch)\r
147                 {\r
148                         if (roleName == null)\r
149                                 throw new ArgumentNullException ("roleName");\r
150                         if (usernameToMatch == null)\r
151                                 throw new ArgumentNullException ("usernameToMatch");\r
152                         if (roleName.Length == 0 || roleName.Length > 256 || roleName.IndexOf (",") != -1)\r
153                                 throw new ArgumentException ("roleName is in invalid format");\r
154                         if (usernameToMatch.Length == 0 || usernameToMatch.Length > 256)\r
155                                 throw new ArgumentException ("usernameToMatch is in invalid format");\r
156 \r
157                         using (DbConnection connection = CreateConnection ()) {\r
158                                 DbDataReader reader;\r
159                                 ArrayList userList = new ArrayList ();\r
160                                 int returnValue = DerbyRolesHelper.UsersInRoles_FindUsersInRole (connection, applicationName, roleName, usernameToMatch, out reader);\r
161 \r
162                                 if (returnValue == 2)\r
163                                         throw new ProviderException ("roleName was not found in the database");\r
164 \r
165                                 using (reader) {\r
166                                         if (reader == null)\r
167                                                 return new string [] { };\r
168 \r
169                                         while (reader.Read ())\r
170                                                 userList.Add (reader.GetString (0));\r
171                                 }\r
172                                 return (string []) userList.ToArray (typeof (string));\r
173                         }\r
174                 }\r
175 \r
176                 public override string [] GetAllRoles ()\r
177                 {\r
178                         using (DbConnection connection = CreateConnection ()) {\r
179                                 DbDataReader reader;\r
180                                 ArrayList roleList = new ArrayList ();\r
181                                 DerbyRolesHelper.Roles_GetAllRoles (connection, applicationName, out reader);\r
182                                 using (reader) {\r
183                                         if (reader == null)\r
184                                                 return new string [] { };\r
185 \r
186                                         while (reader.Read ())\r
187                                                 roleList.Add (reader.GetString (0));\r
188                                 }\r
189                                 return (string []) roleList.ToArray (typeof (string));\r
190                         }\r
191                 }\r
192 \r
193                 public override string [] GetRolesForUser (string username)\r
194                 {\r
195                         if (username == null)\r
196                                 throw new ArgumentNullException ("rolename");\r
197 \r
198                         if (username.Length == 0 || username.Length > 256 || username.IndexOf (",") != -1)\r
199                                 throw new ArgumentException ("username is in invalid format");\r
200 \r
201                         using (DbConnection connection = CreateConnection ()) {\r
202                                 DbDataReader reader;\r
203                                 ArrayList roleList = new ArrayList ();\r
204                                 int returnValue = DerbyRolesHelper.UsersInRoles_GetRolesForUser (connection, applicationName, username, out reader);\r
205 \r
206                                 if (returnValue == 2)\r
207                                         throw new ProviderException ("username was not found in the database");\r
208 \r
209                                 using (reader) {\r
210                                         if (reader == null)\r
211                                                 return new string [] { };\r
212 \r
213                                         while (reader.Read ())\r
214                                                 roleList.Add (reader.GetString (0));\r
215                                 }\r
216                                 return (string []) roleList.ToArray (typeof (string));\r
217                         }\r
218                 }\r
219 \r
220                 public override string [] GetUsersInRole (string rolename)\r
221                 {\r
222                         if (rolename == null)\r
223                                 throw new ArgumentNullException ("rolename");\r
224 \r
225                         if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)\r
226                                 throw new ArgumentException ("rolename is in invalid format");\r
227 \r
228                         using (DbConnection connection = CreateConnection ()) {\r
229                                 DbDataReader reader;\r
230                                 ArrayList roleList = new ArrayList ();\r
231                                 int returnValue = DerbyRolesHelper.UsersInRoles_GetUsersInRoles (connection, applicationName, rolename, out reader);\r
232 \r
233                                 if (returnValue == 2)\r
234                                         throw new ProviderException ("rolename was not found in the database");\r
235 \r
236                                 using (reader) {\r
237                                         if (reader == null)\r
238                                                 return new string [] { };\r
239 \r
240                                         while (reader.Read ())\r
241                                                 roleList.Add (reader.GetString (0));\r
242                                 }\r
243                                 return (string []) roleList.ToArray (typeof (string));\r
244                         }\r
245                 }\r
246 \r
247                 string GetStringConfigValue (NameValueCollection config, string name, string def)\r
248                 {\r
249                         string rv = def;\r
250                         string val = config [name];\r
251                         if (val != null)\r
252                                 rv = val;\r
253                         return rv;\r
254                 }\r
255 \r
256                 public override void Initialize (string name, NameValueCollection config)\r
257                 {\r
258                         if (config == null)\r
259                                 throw new ArgumentNullException ("config");\r
260 \r
261                         base.Initialize (name, config);\r
262 \r
263                         applicationName = config ["applicationName"];\r
264                         string connectionStringName = config ["connectionStringName"];\r
265 \r
266                         if (applicationName.Length > 256)\r
267                                 throw new ProviderException ("The ApplicationName attribute must be 256 characters long or less.");\r
268                         if (connectionStringName == null || connectionStringName.Length == 0)\r
269                                 throw new ProviderException ("The ConnectionStringName attribute must be present and non-zero length.");\r
270 \r
271                         // XXX check connectionStringName and commandTimeout\r
272 \r
273                         connectionString = WebConfigurationManager.ConnectionStrings [connectionStringName];\r
274                         if (connectionString == null)\r
275                                 throw new ProviderException (String.Format("The connection name '{0}' was not found in the applications configuration or the connection string is empty.", connectionStringName));\r
276 \r
277                         string shutdown = config ["shutdown"];\r
278                         if (!String.IsNullOrEmpty (shutdown))\r
279                                 shutDownPolicy = (DerbyUnloadManager.DerbyShutDownPolicy) Enum.Parse (typeof (DerbyUnloadManager.DerbyShutDownPolicy), shutdown, true);\r
280                 }\r
281 \r
282                 public override bool IsUserInRole (string username, string rolename)\r
283                 {\r
284                         if (username == null)\r
285                                 throw new ArgumentNullException ("rolename");\r
286                         if (username.Length == 0 || username.Length > 256 || username.IndexOf (",") != -1)\r
287                                 throw new ArgumentException ("username is in invalid format");\r
288                         if (rolename == null)\r
289                                 throw new ArgumentNullException ("rolename");\r
290                         if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)\r
291                                 throw new ArgumentException ("rolename is in invalid format");\r
292 \r
293                         using (DbConnection connection = CreateConnection ()) {\r
294                                 int returnValue = DerbyRolesHelper.UsersInRoles_IsUserInRole (connection, ApplicationName, username, rolename);\r
295 \r
296                                 if (returnValue == 4)\r
297                                         return true;\r
298 \r
299                                 return false;\r
300                         }\r
301                 }\r
302 \r
303                 public override void RemoveUsersFromRoles (string [] usernames, string [] rolenames)\r
304                 {\r
305                         Hashtable h = new Hashtable ();\r
306 \r
307                         foreach (string u in usernames) {\r
308                                 if (u == null)\r
309                                         throw new ArgumentNullException ("null element in usernames array");\r
310                                 if (h.ContainsKey (u))\r
311                                         throw new ArgumentException ("duplicate element in usernames array");\r
312                                 if (u.Length == 0 || u.Length > 256 || u.IndexOf (",") != -1)\r
313                                         throw new ArgumentException ("element in usernames array in illegal format");\r
314                                 h.Add (u, u);\r
315                         }\r
316 \r
317                         h = new Hashtable ();\r
318                         foreach (string r in rolenames) {\r
319                                 if (r == null)\r
320                                         throw new ArgumentNullException ("null element in rolenames array");\r
321                                 if (h.ContainsKey (r))\r
322                                         throw new ArgumentException ("duplicate element in rolenames array");\r
323                                 if (r.Length == 0 || r.Length > 256 || r.IndexOf (",") != -1)\r
324                                         throw new ArgumentException ("element in rolenames array in illegal format");\r
325                                 h.Add (r, r);\r
326                         } \r
327 \r
328                         using (DbConnection connection = CreateConnection ()) {\r
329                                 int returnValue = DerbyRolesHelper.UsersInRoles_RemoveUsersFromRoles (connection, ApplicationName, usernames, rolenames);\r
330 \r
331                                 if (returnValue == 0)\r
332                                         return;\r
333                                 else if (returnValue == 2)\r
334                                         throw new ProviderException ("One or more of the specified user names was not found.");\r
335                                 else if (returnValue == 3)\r
336                                         throw new ProviderException ("One or more of the specified role names was not found.");\r
337                                 else if (returnValue == 4)\r
338                                         throw new ProviderException ("One or more of the specified user names is not associated with one or more of the specified role names.");\r
339                                 else\r
340                                         throw new ProviderException ("Failed to remove users from roles");\r
341                         }\r
342                 }\r
343 \r
344                 public override bool RoleExists (string rolename)\r
345                 {\r
346                         if (rolename == null)\r
347                                 throw new ArgumentNullException ("rolename");\r
348 \r
349                         if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)\r
350                                 throw new ArgumentException ("rolename is in invalid format");\r
351 \r
352                         using (DbConnection connection = CreateConnection ()) {\r
353                                 int returnValue = DerbyRolesHelper.Roles_RoleExists (connection, ApplicationName, rolename);\r
354 \r
355                                 if (returnValue == 2)\r
356                                         return true;\r
357 \r
358                                 return false;\r
359                         }\r
360                 }\r
361 \r
362                 public override string ApplicationName\r
363                 {\r
364                         get { return applicationName; }\r
365                         set\r
366                         {\r
367                                 applicationName = value;\r
368                         }\r
369                 }\r
370         }\r
371 }\r
372 #endif\r
373 \r