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