The decision by the Army to discharge an officer who posed for a photo with the words “Communism will win” written inside his cap at his graduation from West Point, and the court-martial conviction of a Marine who joined a neo-Nazi group, illustrate an important principle.

The principle should be obvious: The U.S. military is no place for anti-American extremists who do not believe in the foundational values that form the basis for our democratic government under the Constitution.

I served in the U.S. Army from 1983 to 1998 and as a member of Special Forces participated in counterterrorism, counterinsurgency, diplomatic, intelligence and humanitarian operations in more than a dozen countries. Like millions of other Americans who have proudly served our country, I was honored to defend the values that make the United States the greatest nation on Earth.

The first thing that I did -- and that every new member of our military does -- was to swear an oath to protect and defend the Constitution against all enemies, foreign and domestic. That certainly precludes members of the military from joining groups that are rightly considered domestic enemies like communists, neo-Nazis and supporters of radical Islamist extremism.

We rely on the patriotism and fidelity of those who serve in the military to keep us all safe. Anyone who associates with extremist groups disloyal to our nation and our laws has forfeited the right to serve and deserves the maximum punishment the law and military regulations allow.

Specifically, an Army regulation states that it “requires military personnel to reject participation in extremist organizations and activities. Extremist organizations are ones that advocate racial, gender, or ethnic hatred or intolerance; advocate, create, or engage in illegal discrimination based on race, color, gender, religion, or national origin; advocate the use of or use force, violence or unlawful means to deprive individuals of their rights under the United States Constitution or the laws of the United States or any State by unlawful means.”

In cases where a service member gets involved with one these groups, charges are warranted under the Uniform Code of Military Justice to punish them.

The discharged Army officer discussed above was second lieutenant named Spenser Rapone. He was given an other-than-honorable discharge for conduct unbecoming an officer and resigned from the Army on Monday.

Rapone did more than pose for a picture he distributed on social media holding his service cap while at the U.S. Military Academy at West Point with the phrase “Communism will win” written inside. He also posed for a photo unbuttoning the jacket of his cadet’s uniform to reveal a T-shirt with the image of anti-American Cuban communist revolutionary Che Guevara.

Rapone said he considers himself a “revolutionary socialist’ and added: “I would encourage all soldiers who have a conscience to lay down their arms and join me and so many others who are willing to stop serving the agents of imperialism and join us in a revolutionary movement.”

Rapone was rightly booted out of the Army for consistently showing that his allegiance was with our enemies. Now he plans to speak at a conference on socialism in July.

What’s next? Can we expect Rapone to defect to Cuba to join that nation’s communist military, which reveres Che Guevara? Seek political office to bring a communist revolution to the U.S.? Become a commentator on MSNBC or CNN?

Rapone didn’t have any problem accepting four years of a free college education at West Point, funded by U.S. taxpayers. Now he may have to pay back the cost of his education. Perhaps he can turn to his friends in the Resistance or Cuban communists to help foot the bill.

While nearly every member of our military is loyal and patriotic, Rapone is not the only disgraceful exception to the rule.

In a case that drew less attention, Marine Lance Cpl. Vasillios Pistolis was convicted Monday in a summary court-martial for failure to obey an order or regulation and making a false official statement.

Pistolis had joined a neo-Nazi organization and participated in the white supremacist march on Charlottesville, Virginia last August that erupted in violence. ProPublica reported that Pistolis boasted online: “Today, cracked 3 skulls open with virtually no damage to myself.”

ProPublica also reported that Pistolis bragged about attacking a transgender woman and said he kept a blood-soaked flag as a memento.

Pistolis was sentenced to 28 days of confinement, reduction in rank to E-1 and forfeiture of two-third pay for one month.

We rely on the patriotism and fidelity of those who serve in the military to keep us all safe. Anyone who associates with extremist groups disloyal to our nation and our laws has forfeited the right to serve and deserves the maximum punishment the law and military regulations allow.

We must also keep in mind that military personnel do not give up their First Amendment rights, but they do voluntarily accept restrictions like the ones described above. Determining what constitutes an extremist group is a touchy subject itself and care must be taken to ensure that partisan politics do not come into play.

There has been a push to identify many groups as extremist by the political left and we must ensure that this does not infect the decision-making process for the military.

It is a disgrace to all those who serve honorably in our armed forces when others who are hostile to the foundational principles of our democracy wear our uniform. Betraying your comrades on the battlefield is one of the most dishonorable things, but betraying your country by joining groups dedicated to its destruction is far worse.

We must recognize free speech but also ensure the military’s need for good order and discipline. There is no place within the ranks for those who advocate damaging our country and Constitution.