public void DeleteRole(string roleId) { var roleGroups = _db.Groups.Where(g => g.Roles.Any(r => r.RoleId == roleId)); var roleUsers = _db.Users.Where(u => u.Roles.Any(r => r.RoleId == roleId)); var role = _db.ApplicationRoles.Find(roleId); foreach (var user in roleUsers) { this.RemoveFromRole(user.Id, role.Name); } foreach (var group in roleGroups) { var arg = new ApplicationRoleGroup() { RoleId = roleId, GroupId = group.Id, Role = role, Group = group }; group.Roles.Remove(arg); } _db.ApplicationRoles.Remove(role); _db.SaveChanges(); }
public void AddRoleToGroup(int groupId, string roleName) { var group = _db.Groups.Find(groupId); var role = _db.Roles.First(r => r.Name == roleName); var newgroupRole = new ApplicationRoleGroup() { GroupId = group.Id, Group = group, RoleId = role.Id, Role = (ApplicationRole)role }; group.Roles.Add(newgroupRole); _db.SaveChanges(); // Add all of the users in this group to the new role: var groupUsers = _db.Users.Where(u => u.Groups.Any(g => g.GroupId == group.Id)); foreach (var user in groupUsers) { this.AddUserToRole(user.Id, role.Name); } }