BOSTON — Cities and towns that don’t cooperate with federal immigration agents could be turned down for public safety grants under a recent court ruling that amounts to a win for the Trump administration’s efforts to punish communities for sanctuary policies.

The ruling last week by the Ninth Circuit Court of Appeals overturned a nationwide injunction prohibiting the U.S. Department of Justice from favoring communities and counties that cooperate with U.S. Immigration and Customs Enforcement for deportations.

Los Angeles sued the Justice Department because it didn’t receive funding from the Community Oriented Policing Services program, which helps pay for local initiatives to prevent school violence, anti-drug task forces and community policing programs. The city said the Justice Department’s preference in awarding grants to communities that would use the money to focus on illegal immigration amounted to coercion. A federal judge in California granted a preliminary injunction against the policy.

But a three-judge panel of the federal appeals court determined the Trump administration may legally reward communities that would use the money to focus on illegal immigration.

The 2-1 ruling has implications for several Massachusetts communities that have policies on their books restricting cooperation with federal immigration agents, and comes as Beacon Hill is once again considering a proposal that would cement a de facto statewide ban on cooperation between the feds and local law enforcement.

At least nine communities — Salem, Lawrence, Boston, Somerville, Cambridge, Newton, Concord, Amherst and Northampton — have either declared themselves sanctuary cities or adopted ordinances restricting cooperation with federal agents. Many communities in the state, including Lawrence and Salem, are past recipients of community policing grants.

Immigrant rights groups and officials who support the local ordinances say giving police authority to cooperate with immigration agents makes communities less safe, dissuading people from reporting crime for fear of deportation.

Opponents of such policies say police should be working with federal authorities to enforce the law, instead of blocking criminals from arrest and deportation.

“There are definitely cities and towns in Massachusetts that will lose federal funding if they don’t change what they are doing,” said Jessica Vaughan, director of policy studies for the Center for Immigration Studies, a conservative Washington think tank that advocates for stricter limits on immigration. “They should take notice from the court’s ruling that they shouldn’t be shielding criminal aliens from arrest.”

Sanctuary policies vary widely by community and many are aimed at preventing local law enforcement officers from acting as immigration agents.

Under Salem’s “Sanctuary for Peace” ordinance, which was upheld by voters in 2017, authorities do not question people’s immigration status during routine interactions, such as when someone calls the police or another city department. The ordinance does not, however, prevent police from cooperating with immigration officials in criminal matters.

Lawrence’s policy, approved by the City Council in 2015, directs police not to cooperate with immigration agents seeking illegal immigrants unless they have criminal warrants.

Boston’s 2014 Trust Act prohibits law enforcement from honoring so-called detainers, issued when an undocumented person has been arrested on criminal charges and federal authorities believe there is probable cause to deport the person without a criminal warrant. Cambridge and Northampton use similar criteria.

It’s not clear if any of the Massachusetts communities that might be affected by the Justice Department policy will be seeking grant money from the community policing program.

The state and several communities have been awarded more than $12.5 million through the program since 2012, according to Justice Department figures. Lawrence received $750,000 seven years ago, but it hasn’t been awarded any money through the program since then. Salem received $500,000 through the program in 2016, figures show.

The Justice Department has made about $70 million in grant money available for this year’s round of funding, including $32 million for addressing opioid trafficking.

Nationally, more than 200 local and state governments have adopted policies limiting cooperation with federal requests to detain people for possible deportation.

President Donald Trump has vowed to crack down on sanctuary communities. The Republican signed an executive order in 2017 vowing to withhold grants from those refusing to cooperate. But his crackdown has been blunted by a series of federal court rulings that local police cannot hold people at the behest of immigration agents.

Last year, Massachusetts’ Supreme Judicial Court similarly ruled that local police don’t have the power to detain people on immigration charges unless they face criminal charges. The court also ruled that local police cannot cooperate if ICE asks for someone to be held until its agents arrive, but the court said the Legislature has the power to require that.

In response to the ruling, Gov. Charlie Baker filed legislation seeking to authorize, but not require, state and local law enforcement to honor specific ICE detainers for “aliens who pose a threat to public safety.” Lawmakers haven’t taken up his proposal.

Meanwhile, Democratic lawmakers have refiled the so-called “Safe Communities Act” to declare Massachusetts a sanctuary state but still honor detainers for criminal suspects.

Baker has said he doesn’t oppose communities that adopt sanctuary policies, but he has threatened to veto the sanctuary state bill if it reaches his desk.

Christian M. Wade covers the Massachusetts Statehouse for North of Boston Media Group’s newspapers and websites.