THE MESS at the Baltimore City Detention Center, where corrections officers allegedly colluded (and had sex) with inmates amid a wide-ranging conspiracy to smuggle drugs and other contraband into the facility, is a disgrace. It was also highly preventable.

For starters, where were the jail’s gatekeepers? Why couldn’t they stop the deluge of cellphones, narcotics and other forbidden items that, according to a federal indictment, were regularly smuggled into the facility by at least 13 corrupt guards?

The immediate answer is that the gatekeepers included the very same officers who were allegedly in league with the inmates. The broader answer involves mismanagement by the jail’s corrections department administrators.

Under the system in place at the jail, corrections officers cycled through duty at the main entrance; in other words, the same guards who patrolled the cellblocks took turns working as gatekeepers. This ensured that bad apples — and it may turn out that there were more than 13 — periodically or frequently controlled what came inside the walls.

The result was that the guards now under indictment, who are female, could allegedly pass through the main entrance with contraband tucked into their hair orunderclothes or concealed internally. As the indictment noted, they underwent pat-downs that were cursory at best, and much of the contraband, especially the drugs, did not trigger the metal detectors at the entrance.

It‘s a rudimentary precaution, and sound management, to require that guards at the gate are among the most trusted and carefully monitored of the jail’s 450 employees and that they do not rotate with guards inside the cellblocks.

Equally important is that the state establish a workable regime under which corrections officers are disciplined or fired when they flout the rules. According to the federal indictment, the guards who teamed up, and bedded down, with inmates in Baltimore faced no realistic prospect of punishment. Even though their actions were clearly prohibited and illegal, the indictment said, red tape “made the prospect of actual punishment very remote. Often suspected corrections officers were merely transferred to another facility in the immediate vicinity.”

The absurd situation described in the indictment took root at least partly because of a “bill of rights” for corrections officers, backed by Gov. Martin O’Malley (D) and enacted by the Maryland legislature in 2010 at the behest of the guards union, the American Federation of State, County and Municipal Employees. This bill of rights grants extraordinary protections to guards, including shielding them from threats of prosecution, transfer, dismissal or even disciplinary action during questioning for suspected wrongdoing.

Aides to Mr. O’Malley have downplayed the role of the guards’ bill of rights in the jail scandal. But the FBI recorded at least one indicted officer, Kimberly Dennis, saying she had been transferred from one facility “basically because I’m dirty.”

Dirty officers should be fired, not transferred. At the least, state lawmakers should revisit the guards’ bill of rights.