Question: are you legally allowed to leave class?
Answer: It's not a legal thing though. It's school policy. Beyond that time the school isn't supposed to take any action against you, but it's not a legal thin at all. "People" are allowed to leave anywhere, anytime, for any reason, at least in a free society.