Exemplo n.º 1
0
        public override bool RoleExists(string roleName)
        {
            try
            {
                SecUtility.CheckParameter(ref roleName, true, true, true, 255, "roleName");
            }
            catch
            {
                return(false);
            }
            AccessConnectionHolder holder     = MyConnectionHelper.GetConnection(_DatabaseFileName, true);
            SqlConnection          connection = holder.Connection;

            try
            {
                try
                {
                    int appId  = GetApplicationId(holder);
                    int roleId = GetRoleId(connection, appId, roleName);

                    return(roleId != 0);
                }
                catch (Exception e)
                {
                    throw MyConnectionHelper.GetBetterException(e, holder);
                }
                finally
                {
                    holder.Close();
                }
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 2
0
        public override void CreateRole(string roleName)
        {
            SecUtility.CheckParameter(ref roleName, true, true, true, 255, "roleName");

            AccessConnectionHolder holder     = MyConnectionHelper.GetConnection(_DatabaseFileName, true);
            SqlConnection          connection = holder.Connection;

            try
            {
                try
                {
                    int        appId = GetApplicationId(holder);
                    SqlCommand command;
                    command = new SqlCommand(@"INSERT INTO Roles (RoleName) VALUES (@RName)", connection);
                    command.Parameters.Add(new SqlParameter("@RName", roleName));
                    int returnValue = command.ExecuteNonQuery();
                    if (returnValue == 1)
                    {
                        return;
                    }
                    throw new ProviderException("Unknown provider failure");
                }
                catch (Exception e)
                {
                    throw MyConnectionHelper.GetBetterException(e, holder);
                }
                finally
                {
                    holder.Close();
                }
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 3
0
        public override string[] GetRolesForUser(string username)
        {
            SecUtility.CheckParameter(ref username, true, false, true, 255, "username");
            if (username.Length < 1)
            {
                return(new string[0]);
            }

            AccessConnectionHolder holder     = MyConnectionHelper.GetConnection(_DatabaseFileName, true);
            SqlConnection          connection = holder.Connection;
            SqlDataReader          reader     = null;

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

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

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


                    command = new SqlCommand(@"SELECT RoleName FROM UsersInRoles ur, Roles r " +
                                             @"WHERE ur.UserId = @UserId AND ur.RoleId = r.RoleId " +
                                             @"ORDER BY RoleName",
                                             connection);
                    command.Parameters.Add(new SqlParameter("@UserId", userId));
                    reader = 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 MyConnectionHelper.GetBetterException(e, holder);
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    holder.Close();
                }
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 4
0
        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            SecUtility.CheckParameter(ref roleName, true, true, true, 255, "roleName");
            AccessConnectionHolder holder     = MyConnectionHelper.GetConnection(_DatabaseFileName, true);
            SqlConnection          connection = holder.Connection;
            bool fBeginTransCalled            = false;

            try
            {
                try
                {
                    int        appId = GetApplicationId(holder);
                    SqlCommand command;
                    int        roleId = GetRoleId(connection, appId, roleName);

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

                    if (throwOnPopulatedRole)
                    {
                        command = new SqlCommand(@"SELECT COUNT(*) " +
                                                 @"FROM UsersInRoles ur, Users u " +
                                                 @"WHERE ur.RoleId = @RoleId AND ur.UserId = u.UserId",
                                                 connection);

                        command.Parameters.Add(new SqlParameter("@RoleId", roleId));
                        object num = command.ExecuteScalar();
                        if (!(num is int) || ((int)num) != 0)
                        {
                            throw new ProviderException("Role is not empty");
                        }
                    }

                    command = new SqlCommand("BEGIN TRANSACTION", connection);
                    command.ExecuteNonQuery();
                    fBeginTransCalled = true;
                    command           = new SqlCommand(@"DELETE FROM Roles WHERE RoleId = @RoleId", connection);
                    command.Parameters.Add(new SqlParameter("@RoleId", roleId));
                    int returnValue = command.ExecuteNonQuery();
                    command = new SqlCommand("COMMIT TRANSACTION", connection);
                    command.ExecuteNonQuery();
                    fBeginTransCalled = false;

                    return(returnValue == 1);
                }
                catch (Exception e)
                {
                    if (fBeginTransCalled)
                    {
                        try
                        {
                            SqlCommand command = new SqlCommand("ROLLBACK TRANSACTION", connection);
                            command.ExecuteNonQuery();
                        }
                        catch { }
                    }
                    throw MyConnectionHelper.GetBetterException(e, holder);
                }
                finally
                {
                    holder.Close();
                }
            }
            catch
            {
                throw;
            }
        }
Exemplo n.º 5
0
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            SecUtility.CheckParameter(ref username, true, true, true, 255, "username");

            AccessConnectionHolder holder     = MyConnectionHelper.GetConnection(_databaseFileName, true);
            SqlConnection          connection = holder.Connection;
            bool fBeginTransCalled            = false;

            try
            {
                try
                {
                    int appId  = GetAppplicationId(holder);
                    int userId = MyConnectionHelper.GetUserID(connection, appId, username, false);

                    if (userId == 0)
                    {
                        return(false); // User not found
                    }
                    SqlCommand command;

                    //
                    // Start transaction
                    //

                    command = new SqlCommand("BEGIN TRANSACTION", connection);
                    command.ExecuteNonQuery();
                    fBeginTransCalled = true;

                    bool returnValue = false;
                    if (deleteAllRelatedData)
                    {
                        command = new SqlCommand(@"DELETE FROM UsersInRoles WHERE UserId = @UserId", connection);
                        command.Parameters.Add(new SqlParameter("@UserId", userId));
                        command.ExecuteNonQuery();

                        command = new SqlCommand(@"DELETE FROM Users WHERE UserId = @UserId", connection);
                        command.Parameters.Add(new SqlParameter("@UserId", userId));
                        returnValue = (command.ExecuteNonQuery() == 1);
                    }

                    //
                    // End transaction
                    //

                    command = new SqlCommand("COMMIT TRANSACTION", connection);
                    command.ExecuteNonQuery();
                    fBeginTransCalled = false;

                    return(returnValue);
                }
                catch (Exception e)
                {
                    throw MyConnectionHelper.GetBetterException(e, holder);
                }
                finally
                {
                    if (fBeginTransCalled)
                    {
                        try
                        {
                            SqlCommand cmd = new SqlCommand("ROLLBACK TRANSACTION",
                                                            connection);
                            cmd.ExecuteNonQuery();
                        }
                        catch { }
                    }

                    holder.Close();
                }
            }
            catch
            {
                throw;
            }
        }