It shouldn't be legal, it really shouldn't, but there is precedence.
The Lautenberg Amendment retroactively took away anyone's Second Amendment rights if they had ever been convicted of misdemeanor domestic violence, no matter how long in the past the conviction occurred.
This caught a lot of military and law enforcement, since many plead guilty to misdemeanor domestic violence charges and paid a small fine rather than fight the charges in court and pay thousands, usually during a messy divorce.
The Lautenberg Amendment was upheld by SCOTUS.
I recall. I was livid that thanks to a gutless legislature and a tyrannical judiciary another very important part of the Constitution has been gutted.
The prohibition of ex post facto laws was very important to our founders because they had seen first hand the evil they represent.