Merge pull request #901 from Blewzman/FixAggregateExceptionGetBaseException
[mono.git] / mcs / class / System.Web / System.Web.Security / SqlRoleProvider.cs
index 873cea89876f848f021754e10144e1a7a3b2d442..7c6fa45e89ffeaa399b0de88b88c49ddb1800073 100644 (file)
@@ -45,12 +45,16 @@ namespace System.Web.Security
        {
 
                string applicationName;
+               bool schemaIsOk = false;
 
                ConnectionStringSettings connectionString;
                DbProviderFactory factory;
 
                DbConnection CreateConnection ()
                {
+                       if (!schemaIsOk && !(schemaIsOk = AspNetDBSchemaChecker.CheckMembershipSchemaVersion (factory, connectionString.ConnectionString, "role manager", "1")))
+                               throw new ProviderException ("Incorrect ASP.NET DB Schema Version.");
+
                        DbConnection connection = factory.CreateConnection ();
                        connection.ConnectionString = connectionString.ConnectionString;
 
@@ -73,6 +77,17 @@ namespace System.Web.Security
                        return dbp;
                }
 
+               static DbParameter AddParameter (DbCommand command, string parameterName, ParameterDirection direction, DbType type, object parameterValue)
+               {
+                       DbParameter dbp = command.CreateParameter ();
+                       dbp.ParameterName = parameterName;
+                       dbp.Value = parameterValue;
+                       dbp.Direction = direction;
+                       dbp.DbType = type;
+                       command.Parameters.Add (dbp);
+                       return dbp;
+               }
+
                public override void AddUsersToRoles (string [] usernames, string [] rolenames)
                {
                        Hashtable h = new Hashtable ();
@@ -82,7 +97,7 @@ namespace System.Web.Security
                                        throw new ArgumentNullException ("null element in usernames array");
                                if (h.ContainsKey (u))
                                        throw new ArgumentException ("duplicate element in usernames array");
-                               if (u.Length == 0 || u.Length > 256 || u.IndexOf (",") != -1)
+                               if (u.Length == 0 || u.Length > 256 || u.IndexOf (',') != -1)
                                        throw new ArgumentException ("element in usernames array in illegal format");
                                h.Add (u, u);
                        }
@@ -93,7 +108,7 @@ namespace System.Web.Security
                                        throw new ArgumentNullException ("null element in rolenames array");
                                if (h.ContainsKey (r))
                                        throw new ArgumentException ("duplicate element in rolenames array");
-                               if (r.Length == 0 || r.Length > 256 || r.IndexOf (",") != -1)
+                               if (r.Length == 0 || r.Length > 256 || r.IndexOf (',') != -1)
                                        throw new ArgumentException ("element in rolenames array in illegal format");
                                h.Add (r, r);
                        } 
@@ -105,11 +120,11 @@ namespace System.Web.Security
                                command.Connection = connection;
                                command.CommandType = CommandType.StoredProcedure;
 
-                               AddParameter (command, "RoleNames", String.Join (",", rolenames));
-                               AddParameter (command, "UserNames", String.Join (",", usernames));
-                               AddParameter (command, "ApplicationName", ApplicationName);
-                               AddParameter (command, "CurrentTimeUtc", DateTime.UtcNow);
-                               DbParameter dbpr = AddParameter (command, null, ParameterDirection.ReturnValue, null);
+                               AddParameter (command, "@RoleNames", String.Join (",", rolenames));
+                               AddParameter (command, "@UserNames", String.Join (",", usernames));
+                               AddParameter (command, "@ApplicationName", ApplicationName);
+                               AddParameter (command, "@CurrentTimeUtc", DateTime.UtcNow);
+                               DbParameter dbpr = AddParameter (command, "@ReturnVal", ParameterDirection.ReturnValue, DbType.Int32, null);
 
                                command.ExecuteNonQuery ();
 
@@ -130,7 +145,7 @@ namespace System.Web.Security
                        if (rolename == null)
                                throw new ArgumentNullException ("rolename");
 
-                       if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
+                       if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (',') != -1)
                                throw new ArgumentException ("rolename is in invalid format");
 
                        using (DbConnection connection = CreateConnection ()) {
@@ -139,9 +154,9 @@ namespace System.Web.Security
                                command.Connection = connection;
                                command.CommandType = CommandType.StoredProcedure;
                                
-                               AddParameter (command, "ApplicationName", ApplicationName);
-                               AddParameter (command, "RoleName", rolename);
-                               DbParameter dbpr = AddParameter (command, null, ParameterDirection.ReturnValue, null);
+                               AddParameter (command, "@ApplicationName", ApplicationName);
+                               AddParameter (command, "@RoleName", rolename);
+                               DbParameter dbpr = AddParameter (command, "@ReturnVal", ParameterDirection.ReturnValue, DbType.Int32, null);
 
                                command.ExecuteNonQuery ();
                                int returnValue = (int) dbpr.Value;
@@ -158,7 +173,7 @@ namespace System.Web.Security
                        if (rolename == null)
                                throw new ArgumentNullException ("rolename");
 
-                       if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (",") != -1)
+                       if (rolename.Length == 0 || rolename.Length > 256 || rolename.IndexOf (',') != -1)
                                throw new ArgumentException ("rolename is in invalid format");
 
                        using (DbConnection connection = CreateConnection ()) {
@@ -167,10 +182,10 @@ namespace System.Web.Security
                                command.CommandText = @"dbo.aspnet_Roles_DeleteRole";
                                command.Connection = connection;
                                command.CommandType = CommandType.StoredProcedure;
-                               AddParameter (command, "ApplicationName", ApplicationName);
-                               AddParameter (command, "RoleName", rolename);
-                               AddParameter (command, "DeleteOnlyIfRoleIsEmpty", throwOnPopulatedRole);
-                               DbParameter dbpr = AddParameter (command, null, ParameterDirection.ReturnValue, null);
+                               AddParameter (command, "@ApplicationName", ApplicationName);
+                               AddParameter (command, "@RoleName", rolename);
+                               AddParameter (command, "@DeleteOnlyIfRoleIsEmpty", throwOnPopulatedRole);
+                               DbParameter dbpr = AddParameter (command, "@ReturnVal", ParameterDirection.ReturnValue, DbType.Int32, null);
 
                                command.ExecuteNonQuery ();
                                int returnValue = (int)dbpr.Value;
@@ -178,7 +193,7 @@ namespace System.Web.Security
                                if (returnValue == 0)
                                        return true;
                                if (returnValue == 1)
-                                       return false; //role does not exists
+                                       return false; //role does not exist
                                else if (returnValue == 2 && throwOnPopulatedRole)
                                        throw new ProviderException (rolename + " is not empty");
                                else
@@ -192,7 +207,7 @@ namespace System.Web.Security
                                throw new ArgumentNullException ("roleName");
                        if (usernameToMatch == null)
                                throw new ArgumentNullException ("usernameToMatch");
-                       if (roleName.Length == 0 || roleName.Length > 256 || roleName.IndexOf (",") != -1)
+                       if (roleName.Length == 0 || roleName.Length > 256 || roleName.IndexOf (',') != -1)
                                throw new ArgumentException ("roleName is in invalid format");
                        if (usernameToMatch.Length == 0 || usernameToMatch.Length > 256)
                                throw new ArgumentException ("usernameToMatch is in invalid format");
@@ -203,9 +218,9 @@ namespace System.Web.Security
                                command.CommandText = @"dbo.aspnet_UsersInRoles_FindUsersInRole";
                                command.CommandType = CommandType.StoredProcedure;
 
-                               AddParameter (command, "ApplicationName", ApplicationName);
-                               AddParameter (command, "RoleName", roleName);
-                               AddParameter (command, "UsernameToMatch", usernameToMatch);
+                               AddParameter (command, "@ApplicationName", ApplicationName);
+                               AddParameter (command, "@RoleName", roleName);
+                               AddParameter (command, "@UsernameToMatch", usernameToMatch);
 
                                DbDataReader reader = command.ExecuteReader ();
                                ArrayList userList = new ArrayList ();
@@ -225,7 +240,7 @@ namespace System.Web.Security
                                command.Connection = connection;
 
                                command.CommandType = CommandType.StoredProcedure;
-                               AddParameter (command, "ApplicationName", ApplicationName);
+                               AddParameter (command, "@ApplicationName", ApplicationName);
 
                                DbDataReader reader = command.ExecuteReader ();
                                ArrayList roleList = new ArrayList ();
@@ -245,8 +260,8 @@ namespace System.Web.Security
                                command.Connection = connection;
 
                                command.CommandType = CommandType.StoredProcedure;
-                               AddParameter (command, "UserName", username);
-                               AddParameter (command, "ApplicationName", ApplicationName);
+                               AddParameter (command, "@UserName", username);
+                               AddParameter (command, "@ApplicationName", ApplicationName);
 
                                DbDataReader reader = command.ExecuteReader ();
                                ArrayList roleList = new ArrayList ();
@@ -266,8 +281,8 @@ namespace System.Web.Security
                                command.Connection = connection;
 
                                command.CommandType = CommandType.StoredProcedure;
-                               AddParameter (command, "RoleName", rolename);
-                               AddParameter (command, "ApplicationName", ApplicationName);
+                               AddParameter (command, "@RoleName", rolename);
+                               AddParameter (command, "@ApplicationName", ApplicationName);
 
                                DbDataReader reader = command.ExecuteReader ();
                                ArrayList userList = new ArrayList ();
@@ -288,7 +303,6 @@ namespace System.Web.Security
                        return rv;
                }
 
-               [MonoTODO]
                public override void Initialize (string name, NameValueCollection config)
                {
                        if (config == null)
@@ -296,7 +310,7 @@ namespace System.Web.Security
 
                        base.Initialize (name, config);
 
-                       applicationName = config ["applicationName"];
+                       applicationName = GetStringConfigValue (config, "applicationName", "/");
                        string connectionStringName = config ["connectionStringName"];
 
                        if (applicationName.Length > 256)
@@ -322,10 +336,10 @@ namespace System.Web.Security
                                command.Connection = connection;
 
                                command.CommandType = CommandType.StoredProcedure;
-                               AddParameter (command, "RoleName", rolename);
-                               AddParameter (command, "UserName", username);
-                               AddParameter (command, "ApplicationName", ApplicationName);
-                               DbParameter dbpr = AddParameter (command, null, ParameterDirection.ReturnValue, null);
+                               AddParameter (command, "@RoleName", rolename);
+                               AddParameter (command, "@UserName", username);
+                               AddParameter (command, "@ApplicationName", ApplicationName);
+                               DbParameter dbpr = AddParameter (command, "@ReturnVal", ParameterDirection.ReturnValue, DbType.Int32, null);
 
                                command.ExecuteNonQuery ();
                                int returnValue = (int) dbpr.Value;
@@ -346,7 +360,7 @@ namespace System.Web.Security
                                        throw new ArgumentNullException ("null element in usernames array");
                                if (h.ContainsKey (u))
                                        throw new ArgumentException ("duplicate element in usernames array");
-                               if (u.Length == 0 || u.Length > 256 || u.IndexOf (",") != -1)
+                               if (u.Length == 0 || u.Length > 256 || u.IndexOf (',') != -1)
                                        throw new ArgumentException ("element in usernames array in illegal format");
                                h.Add (u, u);
                        }
@@ -357,7 +371,7 @@ namespace System.Web.Security
                                        throw new ArgumentNullException ("null element in rolenames array");
                                if (h.ContainsKey (r))
                                        throw new ArgumentException ("duplicate element in rolenames array");
-                               if (r.Length == 0 || r.Length > 256 || r.IndexOf (",") != -1)
+                               if (r.Length == 0 || r.Length > 256 || r.IndexOf (',') != -1)
                                        throw new ArgumentException ("element in rolenames array in illegal format");
                                h.Add (r, r);
                        } 
@@ -368,10 +382,10 @@ namespace System.Web.Security
                                command.Connection = connection;
                                command.CommandType = CommandType.StoredProcedure;
 
-                               AddParameter (command, "UserNames", String.Join (",", usernames));
-                               AddParameter (command, "RoleNames", String.Join (",", rolenames));
-                               AddParameter (command, "ApplicationName", ApplicationName);
-                               DbParameter dbpr = AddParameter (command, null, ParameterDirection.ReturnValue, null);
+                               AddParameter (command, "@UserNames", String.Join (",", usernames));
+                               AddParameter (command, "@RoleNames", String.Join (",", rolenames));
+                               AddParameter (command, "@ApplicationName", ApplicationName);
+                               DbParameter dbpr = AddParameter (command, "@ReturnVal", ParameterDirection.ReturnValue, DbType.Int32, null);
 
                                command.ExecuteNonQuery ();
                                int returnValue = (int) dbpr.Value;
@@ -398,9 +412,9 @@ namespace System.Web.Security
                                command.Connection = connection;
                                command.CommandType = CommandType.StoredProcedure;
 
-                               AddParameter (command, "ApplicationName", ApplicationName);
-                               AddParameter (command, "RoleName", rolename);
-                               DbParameter dbpr = AddParameter (command, null, ParameterDirection.ReturnValue, null);
+                               AddParameter (command, "@ApplicationName", ApplicationName);
+                               AddParameter (command, "@RoleName", rolename);
+                               DbParameter dbpr = AddParameter (command, "@ReturnVal", ParameterDirection.ReturnValue, DbType.Int32, null);
 
                                command.ExecuteNonQuery ();
                                int returnValue = (int) dbpr.Value;
@@ -412,7 +426,6 @@ namespace System.Web.Security
                        }
                }
 
-               [MonoTODO]
                public override string ApplicationName
                {
                        get { return applicationName; }