A necessary condition is that $R$ is a subdirect product of copies of $Z/2$ and $Z/3$. Claim 1: The class of rings satisfying your property is closed under direct product and homomorphic images. Pf. Exercise. Claim 2: If the ring is indecomposable (not a direct product), then the ring either is a boolean algebra or satisfies $z^3=z$. Pf. See Will's answer and the comments. Claim 3: the ring $R$ is reduced (i.e., has no non-zero nilpotents). Pf. Clear since it is a product of a ring satisfying $z^2=z$ and one satisfying $z^3=z$. Theorem 12.7 in Lam's book on non-commutative rings shows a subdirectly irreducible reduced ring is an integral domain. Since 0,1 are the unique idempotents of an integral domain, we conclude that $R$ is subdirect product of $Z/2$ or $Z/3$. Now boolean algebras (=subdirect products of $Z/2$) all have the desired property so it remains to see which subdirect products of $Z/3$ have the property.