It was a significant legal victory for the Republican president after a string of courtroom setbacks.