Hm, you could use a unit like Arcane Vault, however, All allies of Red and Purple would be able to buy from the shop, and I am not sure on how player built shops works. But I don't think it can be ajusted.
Easy way is run a Trigger that checks the owner of the buying unit, and if it ISN'T Red or Purple, then remove the bought unit, refund the gold and genarate a error message.