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