Пример #1
0
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////

        public override string[] GetRolesForUser(string username)
        {
            SecUtility.CheckParameter(ref username, true, false, true, 255, "username");
            if (username.Length < 1)
            {
                return(new string[0]);
            }

            MySqlConnectionHolder holder     = MySqlConnectionHelper.GetConnection(_ConnectionStringName, true);
            MySqlConnection       connection = holder.Connection;
            MySqlDataReader       reader     = null;

            try {
                try {
                    int    appId  = GetApplicationId(holder);
                    string userId = MySqlConnectionHelper.GetUserID(connection, appId, username, false);

                    if (userId == "0")
                    {
                        return(new string[0]);
                    }

                    MySqlCommand     command;
                    StringCollection sc = new StringCollection();
                    String[]         strReturn;

                    string s = "SELECT RoleName FROM aspnet_UsersInRoles ur, aspnet_Roles r " +
                               "WHERE ur.UserId = '" + userId + "' AND ur.RoleId = r.RoleId " +
                               "ORDER BY RoleName";

                    command = new MySqlCommand("SELECT RoleName FROM aspnet_UsersInRoles ur, aspnet_Roles r " +
                                               "WHERE ur.UserId = '" + userId + "' AND ur.RoleId = r.RoleId " +
                                               "ORDER BY RoleName",
                                               connection);
                    //command.Parameters.Add(new MySqlParameter("?UserId", userId));
                    reader = (MySqlDataReader)command.ExecuteReader(CommandBehavior.SequentialAccess);
                    while (reader.Read())
                    {
                        sc.Add(reader.GetString(0));
                    }
                    strReturn = new String[sc.Count];
                    sc.CopyTo(strReturn, 0);
                    return(strReturn);
                }
                catch (Exception e) {
                    throw e;
                }
                finally {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    holder.Close();
                }
            }
            catch {
                throw;
            }
        }
Пример #2
0
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////

        public override bool IsUserInRole(string username, string roleName)
        {
            SecUtility.CheckParameter(ref username, true, false, true, 255, "username");
            if (username.Length < 1)
            {
                return(false);
            }
            SecUtility.CheckParameter(ref roleName, true, true, true, 255, "roleName");

            MySqlConnectionHolder holder     = MySqlConnectionHelper.GetConnection(_ConnectionStringName, true);
            MySqlConnection       connection = holder.Connection;

            try {
                try {
                    int    appId  = GetApplicationId(holder);
                    string userId = MySqlConnectionHelper.GetUserID(connection, appId, username, false);
                    int    roleId = GetRoleId(connection, appId, roleName);

                    MySqlCommand command;

                    if (userId == "0")
                    {
                        return(false);
                    }

                    if (roleId == 0)
                    {
                        return(false);
                    }

                    command = new MySqlCommand("SELECT UserId FROM aspnet_UsersInRoles WHERE UserId = ?UserId AND RoleId = ?RoleId", connection);
                    command.Parameters.Add(new MySqlParameter("?UserId", userId));
                    command.Parameters.Add(new MySqlParameter("?RoleId", roleId));

                    object result = command.ExecuteScalar();

                    if (result == null || result.ToString() != userId)
                    {
                        return(false);
                    }
                    return(true);
                }
                catch (Exception e) {
                    throw e;
                }
                finally {
                    holder.Close();
                }
            }
            catch {
                throw;
            }
        }
Пример #3
0
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////
        //////////////////////////////////////////////////////////////////////

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            SecUtility.CheckArrayParameter(ref roleNames, true, true, true, 255, "roleNames");
            SecUtility.CheckArrayParameter(ref usernames, true, true, true, 255, "usernames");

            MySqlConnectionHolder holder     = MySqlConnectionHelper.GetConnection(_ConnectionStringName, true);
            MySqlConnection       connection = holder.Connection;
            bool fBeginTransCalled           = false;

            try {
                try {
                    int      appId   = GetApplicationId(holder);
                    string[] userIds = new string[usernames.Length];
                    int[]    roleIds = new int[roleNames.Length];

                    MySqlCommand command;
                    command = new MySqlCommand("START TRANSACTION", connection);
                    command.ExecuteNonQuery();
                    fBeginTransCalled = true;


                    for (int iterU = 0; iterU < usernames.Length; iterU++)
                    {
                        userIds[iterU] = MySqlConnectionHelper.GetUserID(connection, appId, usernames[iterU], false);
                        if (userIds[iterU] == "0")
                        {
                            throw new ProviderException("User not found: " + usernames[iterU]);
                        }
                    }
                    for (int iterR = 0; iterR < roleNames.Length; iterR++)
                    {
                        roleIds[iterR] = GetRoleId(connection, appId, roleNames[iterR]);
                        if (roleIds[iterR] == 0)
                        {
                            throw new ProviderException("Role not found: " + roleNames[iterR]);
                        }
                    }
                    for (int iterU = 0; iterU < usernames.Length; iterU++)
                    {
                        for (int iterR = 0; iterR < roleNames.Length; iterR++)
                        {
                            command = new MySqlCommand("SELECT UserId FROM aspnet_UsersInRoles WHERE UserId = ?UserId AND RoleId = ?RoleId",
                                                       connection);
                            command.Parameters.Add(new MySqlParameter("?UserId", userIds[iterU]));
                            command.Parameters.Add(new MySqlParameter("?RoleId", roleIds[iterR]));

                            object result = command.ExecuteScalar();
                            if (result == null || result.ToString() != userIds[iterU])   // doesn't exist!

                            {
                                throw new ProviderException("The user " + usernames[iterU] + " is already not in role " + roleNames[iterR]);
                            }
                        }
                    }

                    for (int iterU = 0; iterU < usernames.Length; iterU++)
                    {
                        for (int iterR = 0; iterR < roleNames.Length; iterR++)
                        {
                            command = new MySqlCommand("DELETE FROM aspnet_UsersInRoles WHERE UserId = ?UserId AND RoleId = ?RoleId",
                                                       connection);
                            command.Parameters.Add(new MySqlParameter("?UserId", userIds[iterU]));
                            command.Parameters.Add(new MySqlParameter("?RoleId", roleIds[iterR]));
                            if (command.ExecuteNonQuery() != 1)
                            {
                                throw new ProviderException("Unknown failure");
                            }
                        }
                    }
                    command = new MySqlCommand("COMMIT", connection);
                    command.ExecuteNonQuery();
                }
                catch (Exception e) {
                    try {
                        if (fBeginTransCalled)
                        {
                            MySqlCommand command = new MySqlCommand("ROLLBACK", connection);
                            command.ExecuteNonQuery();
                        }
                    }
                    catch { }
                    throw e;
                }
                finally {
                    holder.Close();
                }
            }
            catch {
                throw;
            }
        }