That's actually what housing cooperatives do, and it's legal. The cooperative board gets to vote on who moves in. Lafayette/Elmwood area of Detroit has a number of examples of successful cooperatives. This couldn't be implemented in a traditional neighborhood of single family homes unless someone first bought up all the houses and then converted the whole thing to a cooperative.

Once, in New York City, a cooperative board rejected Richard Nixon.