The U.S. Supreme Court on Monday ended a long legal struggle to sue top officials such as former FBI director Robert Mueller and former Attorney Gen. John Ashcroft for damages for alleged mistreatment of post-Sept. 11 detainees held in a Brooklyn federal jail, ruling that the lawsuit can’t go forward.

Despite allegations that Arab and Muslim men were rounded up with high-level approval and held in restrictive conditions based on their religion or country of origin, the high court said suits for money damages could interfere with decision making at times of national crisis.

“National security policy is the prerogative of the Congress and the President,” Justice Anthony Kennedy wrote in the majority opinion. “ . . . The risk of personal damages liability is more likely to cause an official to second-guess difficult but necessary decisions concerning national-security policy.”

The case split the court 4-2 along partisan lines, with the court’s conservative bloc joining Kennedy while liberal justices Ruth Ginsberg and Stephen Breyer dissented.

“History tells us of far too many instances where the Executive or Legislative branch took actions during time of war that on later examination turned out unnecessarily and unreasonably to have deprived American citizens of basic constitutional rights,” the two said in their dissenting opinion.

Three members of the court did not participate. Justice Neil Gorsuch was not on the Supreme Court when the case was argued, and Justices Elena Kagan and Sonia Sotomayor were involved with the case before joining the court.

The lawsuit was filed in 2002 on behalf of eight men who were swept up for immigration violations in a dragnet following the Sept. 11 attacks and held in harsh, high-security conditions before eventually being cleared and released.

They said the punitive treatment, later documented in Justice Department internal reports, included solitary confinement, strip searches and abuse by guards, and that “maximum pressure” tactics were condoned by top officials despite lack of evidence of terrorist links.

“We are very disappointed with the Court’s dismissal of our clients’ claims,” said Rachel Meeropol of the Center for Constitutional Rights in New York. “The Court’s decision allows for high-level officials to violate the Constitution without fear of personal accountability.”

Before Monday’s ruling, the Second Circuit Court of Appeals in New York had given the go ahead to the suit. The Supreme Court said the lawsuit could proceed against one official, the warden of Brooklyn’s Metropolitan Detention Center, over allegations of “deliberate indifference “ to abusive treatment.