2010-07-25 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.Security / DerbyRolesHelper.cs
1 //\r
2 // Mainsoft.Web.Security.DerbyRolesHelper\r
3 //\r
4 // Authors:\r
5 //      Vladimir Krasnov (vladimirk@mainsoft.com)\r
6 //\r
7 // (C) 2006 Mainsoft\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.Web.Security;\r
34 using System.Data;\r
35 using System.Data.OleDb;\r
36 using System.Data.Common;\r
37 using System.Collections.Generic;\r
38 using System.Text;\r
39 \r
40 namespace Mainsoft.Web.Security\r
41 {\r
42         static class DerbyRolesHelper\r
43         {\r
44                 private static OleDbParameter AddParameter (OleDbCommand command, string paramName, object paramValue)\r
45                 {\r
46                         OleDbParameter prm = new OleDbParameter (paramName, paramValue);\r
47                         command.Parameters.Add (prm);\r
48                         return prm;\r
49                 }\r
50 \r
51                 public static int Roles_CreateRole (DbConnection connection, string applicationName, string rolename)\r
52                 {\r
53                         string appId = (string) DerbyApplicationsHelper.Applications_CreateApplication (connection, applicationName);\r
54                         if (appId == null)\r
55                                 return 1;\r
56 \r
57                         string querySelect = "SELECT RoleName FROM aspnet_Roles WHERE ApplicationId = ? AND LoweredRoleName = ?";\r
58                         OleDbCommand cmdSelect = new OleDbCommand (querySelect, (OleDbConnection) connection);\r
59                         AddParameter (cmdSelect, "ApplicationId", appId);\r
60                         AddParameter (cmdSelect, "LoweredRoleName", rolename.ToLowerInvariant ());\r
61 \r
62                         using (OleDbDataReader reader = cmdSelect.ExecuteReader ()) {\r
63                                 if (reader.Read ())\r
64                                         return 2; // role already exists\r
65                         }\r
66 \r
67                         string queryInsert = "INSERT INTO aspnet_Roles (ApplicationId, RoleId, RoleName, LoweredRoleName) VALUES (?, ?, ?, ?)";\r
68                         OleDbCommand cmdInsert = new OleDbCommand (queryInsert, (OleDbConnection) connection);\r
69                         AddParameter (cmdInsert, "ApplicationId", appId);\r
70                         AddParameter (cmdInsert, "RoleId", Guid.NewGuid ().ToString ());\r
71                         AddParameter (cmdInsert, "RoleName", rolename);\r
72                         AddParameter (cmdInsert, "LoweredRoleName", rolename.ToLowerInvariant ());\r
73                         cmdInsert.ExecuteNonQuery ();\r
74 \r
75                         return 0;\r
76                 }\r
77 \r
78                 public static int Roles_DeleteRole (DbConnection connection, string applicationName, string rolename, bool deleteOnlyIfRoleIsEmpty)\r
79                 {\r
80                         string appId = DerbyApplicationsHelper.GetApplicationId (connection, applicationName);\r
81                         if (appId == null)\r
82                                 return 1;\r
83 \r
84                         string roleId = GetRoleId (connection, appId, rolename);\r
85                         if (roleId == null)\r
86                                 return 2;\r
87 \r
88                         if (deleteOnlyIfRoleIsEmpty) {\r
89                                 string querySelect = "SELECT RoleId FROM aspnet_UsersInRoles WHERE RoleId = ?";\r
90                                 OleDbCommand cmdSelect = new OleDbCommand (querySelect, (OleDbConnection) connection);\r
91                                 AddParameter (cmdSelect, "RoleId", roleId);\r
92                                 using (OleDbDataReader reader = cmdSelect.ExecuteReader ()) {\r
93                                         if (reader.Read ())\r
94                                                 // role is not empty\r
95                                                 return 3;\r
96                                 }\r
97                         }\r
98 \r
99                         string queryDelUsers = "DELETE FROM aspnet_UsersInRoles WHERE RoleId = ?";\r
100                         OleDbCommand cmdDelUsers = new OleDbCommand (queryDelUsers, (OleDbConnection) connection);\r
101                         AddParameter (cmdDelUsers, "RoleId", roleId);\r
102                         cmdDelUsers.ExecuteNonQuery ();\r
103 \r
104                         string queryDelRole = "DELETE FROM aspnet_Roles WHERE ApplicationId = ? AND RoleId = ? ";\r
105                         OleDbCommand cmdDelRole = new OleDbCommand (queryDelRole, (OleDbConnection) connection);\r
106                         AddParameter (cmdDelRole, "ApplicationId", appId);\r
107                         AddParameter (cmdDelRole, "RoleId", roleId);\r
108                         cmdDelRole.ExecuteNonQuery ();\r
109 \r
110                         return 0;\r
111                 }\r
112 \r
113                 public static int Roles_GetAllRoles (DbConnection connection, string applicationName, out DbDataReader reader)\r
114                 {\r
115                         reader = null;\r
116                         string appId = DerbyApplicationsHelper.GetApplicationId (connection, applicationName);\r
117                         if (appId == null)\r
118                                 return 1;\r
119 \r
120                         string querySelect = "SELECT RoleName FROM aspnet_Roles WHERE ApplicationId = ? ORDER BY RoleName";\r
121                         OleDbCommand cmdSelect = new OleDbCommand (querySelect, (OleDbConnection) connection);\r
122                         AddParameter (cmdSelect, "ApplicationId", appId);\r
123                         reader = cmdSelect.ExecuteReader ();\r
124 \r
125                         return 0;\r
126                 }\r
127 \r
128                 public static int Roles_RoleExists (DbConnection connection, string applicationName, string rolename)\r
129                 {\r
130                         string appId = DerbyApplicationsHelper.GetApplicationId (connection, applicationName);\r
131                         if (appId == null)\r
132                                 return 1;\r
133 \r
134                         string querySelect = "SELECT RoleName FROM aspnet_Roles WHERE ApplicationId = ? AND LoweredRoleName = ?";\r
135                         OleDbCommand cmdSelect = new OleDbCommand (querySelect, (OleDbConnection) connection);\r
136                         AddParameter (cmdSelect, "ApplicationId", appId);\r
137                         AddParameter (cmdSelect, "LoweredRoleName", rolename.ToLowerInvariant ());\r
138 \r
139                         using (OleDbDataReader reader = cmdSelect.ExecuteReader ()) {\r
140                                 if (reader.Read ())\r
141                                         return 2;\r
142                         }\r
143                         return 0;\r
144                 }\r
145 \r
146                 public static int UsersInRoles_AddUsersToRoles (DbConnection connection, string applicationName, string [] userNames, string [] roleNames, DateTime currentTimeUtc)\r
147                 {\r
148                         string appId = DerbyApplicationsHelper.GetApplicationId (connection, applicationName);\r
149                         if (appId == null)\r
150                                 return 1;\r
151 \r
152                         string [] userIds = new string [userNames.Length];\r
153                         string [] loweredUsernames = new string [userNames.Length];\r
154                         string [] roleIds = new string [roleNames.Length];\r
155 \r
156                         string querySelUsers = "SELECT UserId, LoweredUserName FROM aspnet_Users WHERE ApplicationId = ? AND LoweredUserName in " + GetPrms (userNames.Length);\r
157                         OleDbCommand cmdSelUsers = new OleDbCommand (querySelUsers, (OleDbConnection) connection);\r
158                         AddParameter (cmdSelUsers, "ApplicationId", appId);\r
159                         for (int i = 0; i < userNames.Length; i++)\r
160                                 AddParameter (cmdSelUsers, "LoweredUserName", userNames [i].ToLowerInvariant ());\r
161 \r
162                         int userIndex = 0;\r
163                         using (OleDbDataReader reader = cmdSelUsers.ExecuteReader ()) {\r
164                                 while (reader.Read ()) {\r
165                                         userIds [userIndex] = reader.GetString (0);\r
166                                         loweredUsernames [userIndex] = reader.GetString (1);\r
167                                         userIndex++;\r
168                                 }\r
169                         }\r
170 \r
171                         if (userNames.Length != userIndex) {\r
172                                 // find not existing users and create them\r
173                                 for (int j = 0; j < userNames.Length; j++)\r
174                                         if (Array.IndexOf (loweredUsernames, userNames [j].ToLowerInvariant ()) < 0) {\r
175                                                 string newUserId = Guid.NewGuid ().ToString ();\r
176                                                 string queryAddUser = "INSERT INTO aspnet_Users (ApplicationId, UserId, UserName, " +\r
177                                                         "LoweredUserName, IsAnonymous, LastActivityDate) VALUES (?, ?, ?, ?, ?, ?)";\r
178                                                 OleDbCommand cmdAddUser = new OleDbCommand (queryAddUser, (OleDbConnection) connection);\r
179                                                 AddParameter (cmdAddUser, "ApplicationId", appId);\r
180                                                 AddParameter (cmdAddUser, "UserId", newUserId);\r
181                                                 AddParameter (cmdAddUser, "UserName", userNames [j]);\r
182                                                 AddParameter (cmdAddUser, "LoweredUserName", userNames [j].ToLowerInvariant ());\r
183                                                 AddParameter (cmdAddUser, "IsAnonymous", 0);\r
184                                                 AddParameter (cmdAddUser, "LastActivityDate", DateTime.UtcNow);\r
185                                                 cmdAddUser.ExecuteNonQuery ();\r
186 \r
187                                                 userIds [userIndex++] = newUserId;\r
188                                         }\r
189                         }\r
190 \r
191 \r
192                         string querySelRoles = "SELECT RoleId FROM aspnet_Roles WHERE ApplicationId = ? AND LoweredRoleName in " + GetPrms (roleNames.Length);\r
193                         OleDbCommand cmdSelRoles = new OleDbCommand (querySelRoles, (OleDbConnection) connection);\r
194                         AddParameter (cmdSelRoles, "ApplicationId", appId);\r
195                         for (int i = 0; i < roleNames.Length; i++)\r
196                                 AddParameter (cmdSelRoles, "LoweredRoleName", roleNames [i].ToLowerInvariant ());\r
197 \r
198                         using (OleDbDataReader reader = cmdSelRoles.ExecuteReader ()) {\r
199                                 int i = 0;\r
200                                 while (reader.Read ())\r
201                                         roleIds [i++] = reader.GetString (0);\r
202 \r
203                                 if (roleNames.Length != i)\r
204                                         return 2; // one or more roles not found\r
205                         }\r
206 \r
207                         string querySelCount = "SELECT COUNT(*) FROM aspnet_UsersInRoles WHERE UserId in " + GetPrms (userNames.Length) + " AND RoleId in " + GetPrms (roleNames.Length);\r
208                         OleDbCommand cmdSelCount = new OleDbCommand (querySelCount, (OleDbConnection) connection);\r
209                         foreach (string userId in userIds)\r
210                                 AddParameter (cmdSelCount, "UserId", userId);\r
211                         foreach (string roleId in roleIds)\r
212                                 AddParameter (cmdSelCount, "RoleId", roleId);\r
213                         using (OleDbDataReader reader = cmdSelCount.ExecuteReader ()) {\r
214                                 if (reader.Read ())\r
215                                         if (reader.GetInt32 (0) > 0)\r
216                                                 return 3;\r
217                         }\r
218 \r
219                         string valuesExp = string.Empty;\r
220                         int pairs = userNames.Length * roleNames.Length;\r
221                         for (int i = 0; i < pairs; i++)\r
222                                 valuesExp += "(?, ?),";\r
223 \r
224                         string queryInsert = "INSERT INTO aspnet_UsersInRoles (UserId, RoleId) VALUES " + valuesExp.Trim (',');\r
225                         OleDbCommand cmdInsert = new OleDbCommand (queryInsert, (OleDbConnection) connection);\r
226                         foreach (string roleId in roleIds)\r
227                                 foreach (string userId in userIds) {\r
228                                         AddParameter (cmdInsert, "UserId", userId);\r
229                                         AddParameter (cmdInsert, "RoleId", roleId);\r
230                                 }\r
231 \r
232                         cmdInsert.ExecuteNonQuery ();\r
233                         return 0;\r
234                 }\r
235 \r
236                 public static int UsersInRoles_FindUsersInRole (DbConnection connection, string applicationName, string rolename, string userNameToMatch, out DbDataReader reader)\r
237                 {\r
238                         reader = null;\r
239                         string appId = DerbyApplicationsHelper.GetApplicationId (connection, applicationName);\r
240                         if (appId == null)\r
241                                 return 1;\r
242 \r
243                         string roleId = GetRoleId (connection, appId, rolename);\r
244                         if (roleId == null)\r
245                                 return 2;\r
246 \r
247                         string querySelect = "SELECT usr.UserName FROM aspnet_Users usr, aspnet_UsersInRoles uir " +
248                                 "WHERE usr.UserId = uir.UserId AND usr.ApplicationId = ? AND uir.RoleId = ? AND LoweredUserName LIKE ? " +
249                                 "ORDER BY usr.UserName";\r
250                         OleDbCommand cmdSelect = new OleDbCommand (querySelect, (OleDbConnection) connection);\r
251                         AddParameter (cmdSelect, "ApplicationId", appId);\r
252                         AddParameter (cmdSelect, "RoleId", roleId);\r
253                         AddParameter (cmdSelect, "LoweredUserName", "%" + userNameToMatch.ToLowerInvariant() + "%");\r
254                         reader = cmdSelect.ExecuteReader ();\r
255 \r
256                         return 0;\r
257                 }\r
258 \r
259                 public static int UsersInRoles_GetRolesForUser (DbConnection connection, string applicationName, string username, out DbDataReader reader)\r
260                 {\r
261                         reader = null;\r
262                         string appId = DerbyApplicationsHelper.GetApplicationId (connection, applicationName);\r
263                         if (appId == null)\r
264                                 return 1;\r
265 \r
266                         string userId = GetUserId (connection, appId, username);\r
267                         if (userId == null)\r
268                                 return 2;\r
269 \r
270                         string querySelect = "SELECT rol.RoleName FROM aspnet_Roles rol, aspnet_UsersInRoles uir " +\r
271                                 "WHERE rol.RoleId = uir.RoleId AND rol.ApplicationId = ? AND uir.UserId = ? ORDER BY rol.RoleName";\r
272                         OleDbCommand cmdSelect = new OleDbCommand (querySelect, (OleDbConnection) connection);\r
273                         AddParameter (cmdSelect, "ApplicationId", appId);\r
274                         AddParameter (cmdSelect, "UserId", userId);\r
275                         reader = cmdSelect.ExecuteReader ();\r
276 \r
277                         return 0;\r
278                 }\r
279 \r
280                 public static int UsersInRoles_GetUsersInRoles (DbConnection connection, string applicationName, string rolename, out DbDataReader reader)\r
281                 {\r
282                         reader = null;\r
283                         string appId = DerbyApplicationsHelper.GetApplicationId (connection, applicationName);\r
284                         if (appId == null)\r
285                                 return 1;\r
286 \r
287                         string roleId = GetRoleId (connection, appId, rolename);\r
288                         if (roleId == null)\r
289                                 return 2;\r
290 \r
291                         string querySelect = "SELECT usr.UserName FROM aspnet_Users usr, aspnet_UsersInRoles uir " +\r
292                                 "WHERE usr.UserId = uir.UserId AND usr.ApplicationId = ? AND uir.RoleId = ? ORDER BY usr.UserName";\r
293                         OleDbCommand cmdSelect = new OleDbCommand (querySelect, (OleDbConnection) connection);\r
294                         AddParameter (cmdSelect, "ApplicationId", appId);\r
295                         AddParameter (cmdSelect, "RoleId", roleId);\r
296                         reader = cmdSelect.ExecuteReader ();\r
297 \r
298                         return 0;\r
299                 }\r
300 \r
301                 public static int UsersInRoles_IsUserInRole (DbConnection connection, string applicationName, string username, string rolename)\r
302                 {\r
303                         string appId = DerbyApplicationsHelper.GetApplicationId (connection, applicationName);\r
304                         if (appId == null)\r
305                                 return 1;\r
306 \r
307                         string userId = GetUserId (connection, appId, username);\r
308                         if (userId == null)\r
309                                 return 2;\r
310 \r
311                         string roleId = GetRoleId (connection, appId, rolename);\r
312                         if (roleId == null)\r
313                                 return 3;\r
314 \r
315                         string querySelect = "SELECT UserId FROM aspnet_UsersInRoles WHERE UserId = ? AND RoleId = ?";\r
316                         OleDbCommand cmdSelect = new OleDbCommand (querySelect, (OleDbConnection) connection);\r
317                         AddParameter (cmdSelect, "UserId", userId);\r
318                         AddParameter (cmdSelect, "RoleId", roleId);\r
319                         using (OleDbDataReader reader = cmdSelect.ExecuteReader ()) {\r
320                                 if (reader.Read ())\r
321                                         return 4;\r
322                         }\r
323                         return 0;\r
324                 }\r
325 \r
326                 public static int UsersInRoles_RemoveUsersFromRoles (DbConnection connection, string applicationName, string [] userNames, string [] roleNames)\r
327                 {\r
328                         string appId = DerbyApplicationsHelper.GetApplicationId (connection, applicationName);\r
329                         if (appId == null)\r
330                                 return 1;\r
331 \r
332                         string [] userIds = new string [userNames.Length];\r
333                         string [] roleIds = new string [roleNames.Length];\r
334 \r
335                         string querySelUsers = "SELECT UserId FROM aspnet_Users WHERE ApplicationId = ? AND LoweredUserName in " + GetPrms (userNames.Length);\r
336                         OleDbCommand cmdSelUsers = new OleDbCommand (querySelUsers, (OleDbConnection) connection);\r
337                         AddParameter (cmdSelUsers, "ApplicationId", appId);\r
338                         for (int i = 0; i < userNames.Length; i++)\r
339                                 AddParameter (cmdSelUsers, "LoweredUserName", userNames [i].ToLowerInvariant ());\r
340 \r
341                         using (OleDbDataReader reader = cmdSelUsers.ExecuteReader ()) {\r
342                                 int i = 0;\r
343                                 while (reader.Read ())\r
344                                         userIds [i++] = reader.GetString (0);\r
345 \r
346                                 if (userNames.Length != i)\r
347                                         return 2; // one or more users not found\r
348                         }\r
349 \r
350                         string querySelRoles = "SELECT RoleId FROM aspnet_Roles WHERE ApplicationId = ? AND LoweredRoleName in " + GetPrms (roleNames.Length);\r
351                         OleDbCommand cmdSelRoles = new OleDbCommand (querySelRoles, (OleDbConnection) connection);\r
352                         AddParameter (cmdSelRoles, "ApplicationId", appId);\r
353                         for (int i = 0; i < roleNames.Length; i++)\r
354                                 AddParameter (cmdSelRoles, "LoweredRoleName", roleNames [i].ToLowerInvariant ());\r
355 \r
356                         using (OleDbDataReader reader = cmdSelRoles.ExecuteReader ()) {\r
357                                 int i = 0;\r
358                                 while (reader.Read ())\r
359                                         roleIds [i++] = reader.GetString (0);\r
360 \r
361                                 if (roleNames.Length != i)\r
362                                         return 3; // one or more roles not found\r
363                         }\r
364 \r
365                         string querySelCount = "SELECT COUNT(*) FROM aspnet_UsersInRoles WHERE UserId in " + GetPrms (userNames.Length) + " AND RoleId in " + GetPrms (roleNames.Length);\r
366                         OleDbCommand cmdSelCount = new OleDbCommand (querySelCount, (OleDbConnection) connection);\r
367                         foreach (string userId in userIds)\r
368                                 AddParameter (cmdSelCount, "UserId", userId);\r
369                         foreach (string roleId in roleIds)\r
370                                 AddParameter (cmdSelCount, "RoleId", roleId);\r
371                         using (OleDbDataReader reader = cmdSelCount.ExecuteReader ()) {\r
372                                 if (reader.Read ())\r
373                                         if (userNames.Length * roleNames.Length > reader.GetInt32 (0))\r
374                                                 return 4;\r
375                         }\r
376 \r
377                         string queryDelete = "DELETE FROM aspnet_UsersInRoles WHERE UserId in " + GetPrms (userNames.Length) + " AND RoleId in " + GetPrms (roleNames.Length);\r
378                         OleDbCommand cmdDelete = new OleDbCommand (queryDelete, (OleDbConnection) connection);\r
379                         foreach (string userId in userIds)\r
380                                 AddParameter (cmdDelete, "UserId", userId);\r
381                         foreach (string roleId in roleIds)\r
382                                 AddParameter (cmdDelete, "RoleId", roleId);\r
383                         cmdDelete.ExecuteNonQuery ();\r
384 \r
385                         return 0;\r
386                 }\r
387 \r
388                 private static string GetRoleId (DbConnection connection, string applicationId, string rolename)\r
389                 {\r
390                         string selectQuery = "SELECT RoleId FROM aspnet_Roles WHERE LoweredRoleName = ? AND ApplicationId = ?";\r
391 \r
392                         OleDbCommand selectCmd = new OleDbCommand (selectQuery, (OleDbConnection) connection);\r
393                         AddParameter (selectCmd, "LoweredRoleName", rolename.ToLowerInvariant ());\r
394                         AddParameter (selectCmd, "ApplicationId", applicationId);\r
395                         using (OleDbDataReader reader = selectCmd.ExecuteReader ()) {\r
396                                 if (reader.Read ())\r
397                                         return reader.GetString (0);\r
398                         }\r
399 \r
400                         return null;\r
401                 }\r
402 \r
403                 private static string GetUserId (DbConnection connection, string applicationId, string username)\r
404                 {\r
405                         string selectQuery = "SELECT UserId FROM aspnet_Users WHERE LoweredUserName = ? AND ApplicationId = ?";\r
406 \r
407                         OleDbCommand selectCmd = new OleDbCommand (selectQuery, (OleDbConnection) connection);\r
408                         AddParameter (selectCmd, "LoweredUserName", username.ToLowerInvariant ());\r
409                         AddParameter (selectCmd, "ApplicationId", applicationId);\r
410                         using (OleDbDataReader reader = selectCmd.ExecuteReader ()) {\r
411                                 if (reader.Read ())\r
412                                         return reader.GetString (0);\r
413                         }\r
414 \r
415                         return null;\r
416                 }\r
417 \r
418                 private static string GetPrms (int n)\r
419                 {\r
420                         string exp = string.Empty;\r
421                         for (int i = 0; i < n; i++)\r
422                                 exp += "?,";\r
423 \r
424                         exp = "(" + exp.Trim (',') + ")";\r
425                         return exp;\r
426                 }\r
427         }\r
428 }\r
429 \r
430 #endif