// Inherited from RoleProvider ==> Forwarded to previous provider if this provider hasn't been initialized public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { if (!InitializeCalled) { return(PreviousProvider.DeleteRole(roleName, throwOnPopulatedRole)); } using (var db = ConnectToDatabase()) { int roleId = FindRoleId(db, roleName); if (roleId == -1) { return(false); } if (throwOnPopulatedRole) { int usersInRole = db.Query(@"SELECT * FROM " + UsersInRoleTableName + " WHERE (RoleId = @0)", roleId).Count(); if (usersInRole > 0) { throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, WebDataResources.SimpleRoleProvder_RolePopulated, roleName)); } } else { // Delete any users in this role first db.Execute(@"DELETE FROM " + UsersInRoleTableName + " WHERE (RoleId = @0)", roleId); } int rows = db.Execute(@"DELETE FROM " + RoleTableName + " WHERE (RoleId = @0)", roleId); return(rows == 1); // REVIEW: should this ever be > 1? } }
/// <summary> /// Removes a role from the data source for the configured applicationName. /// </summary> /// <remarks>Inherited from RoleProvider ==> Forwarded to previous provider if this provider hasn't been initialized</remarks> /// <param name="roleName">The name of the role to delete.</param> /// <param name="throwOnPopulatedRole">If true, throw an exception if <paramref name="roleName" /> has one or more members and do not delete <paramref name="roleName" />.</param> /// <returns>true if the role was successfully deleted; otherwise, false.</returns> /// <exception cref="System.InvalidOperationException"></exception> public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { if (!InitializeCalled) { return(PreviousProvider.DeleteRole(roleName, throwOnPopulatedRole)); } using (var db = NewMySqlMembershipContext) { var roleId = FindRoleId(db, roleName); if (roleId == -1) { return(false); } var usersInRoles = db.UsersInRoles.Where(x => x.RoleId == roleId); if (throwOnPopulatedRole) { if (usersInRoles.Any()) { throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture, Resources.Security.SimpleRoleProvder_RolePopulated, roleName)); } } else { // Delete any users in this role first foreach (var usersInRole in usersInRoles) { db.UsersInRoles.Remove(usersInRole); } } var role = db.Roles.SingleOrDefault(x => x.RoleId == roleId); db.Roles.Remove(role); return(db.SaveChanges() > 0); } }