public void UnallowUser(ChatUser user, ChatUser targetUser, ChatRoom targetRoom)
{
EnsureOwnerOrAdmin(user, targetRoom);
if (targetUser == user)
{
throw new InvalidOperationException("Why would you want to unallow yourself?");
}
if (!targetRoom.Private)
{
throw new InvalidOperationException(String.Format("{0} is not a private room.", targetRoom.Name));
}
if (!targetUser.AllowedRooms.Contains(targetRoom))
{
throw new InvalidOperationException(String.Format("{0} isn't allowed to access {1}.", targetUser.Name, targetRoom.Name));
}
// only admin can unallow admin
if (!user.IsAdmin && targetUser.IsAdmin)
{
throw new InvalidOperationException("You cannot unallow an admin. Only admin can unallow admin.");
}
// If this user isn't the creator and the target user is an owner then throw
if (targetRoom.Creator != user && targetRoom.Owners.Contains(targetUser) && !user.IsAdmin)
{
throw new InvalidOperationException("Owners cannot unallow other owners. Only the room creator can unallow an owner.");
}
targetRoom.AllowedUsers.Remove(targetUser);
targetUser.AllowedRooms.Remove(targetRoom);
// Make the user leave the room
LeaveRoom(targetUser, targetRoom);
_repository.CommitChanges();
}