Ruling marks a victory for the president in one of his remaining legal headaches after return to the White House