Communities can’t pass ordinances to preempt Michigan’s 5-year-old medical marijuana law, according to a Michigan Supreme Court ruling Thursday — a far-reaching move that “ends a long, tortuous battle in the courts,” one legal expert said.

The decision may invalidate marijuana measures at least three Metro Detroit cities that run contrary to the state law, legal experts say.

Wyoming, near Grand Rapids, in 2010 created a zoning ordinance that prohibited the use, cultivation or manufacture of marijuana. Wyoming resident John Ter Beek challenged the law because he said he was a qualified medical marijuana patient and argued that the 2008 state law preempted the local ordinance. The court agreed.

“It’s a good victory for medical marijuana patients,” said Matthew Abel, a senior partner at Cannabis Counsel PLC, a Detroit-based law firm that handles marijuana cases.

Abel said the ruling upholds the rights of residents who want to “avail themselves of the benefits of the medical marijuana law if it applies to them.”

Wyoming’s ordinance “directly conflicts” with the state’s medical marijuana law by allowing what the law “expressly prohibits: the imposition of any penalty, including a civil one, on a registered qualifying patient whose medical use of marijuana falls with ... the immunity” granted under state law, Justice Bridget McCormack wrote in a unanimous opinion.

The court held that federal law criminalizing marijuana doesn’t invalidate the state’s medical marijuana law because the state law “doesn’t interfere with or undermine federal enforcement of that prohibition,” McCormack wrote. The state legally allows “a limited class of individuals to engage in certain uses in an effort to provide for the health and welfare of Michigan citizens,” the ruling said.