Depends - is the base in pre-set location or can it be placed anywhere during the game?
Can there be more than 1 base per player? Etc.
The way you could do it: Have a region of the same size for each player; when player builds his base, center that region on the point where his main building stands. Then whenever another player tries to build a building inside this region, you order that player to stop.
Another way could be by setting a distance check - for example a distance of 1500 for each base. Whenever a player would try to build a building within 1500 of another player's main building, you would order that player's worker to stop.
The attack part can be done easily by checking if a unit is issued order targeting unit and checking if ordered unit and target unit of issued order are allies and if the target is a building.