Whenever īmān enters the heart and one makes his whole affair sincerely for Allāh’s sake, Allāh will indeed repel and defend one from all types of evil and vulgarity because of one’s īmān and sincerity due to Allāh’s statement, “Thus it was, that We might turn away from him evil and illegal sexual intercourse. Surely, he was one of Our sincere slaves.
Regarding the last āyah of Sūrah Yūsuf (ʿalayhi al-Salām) wherein Allāh subḥānahu wa taā’la says,
“Indeed in their stories, there is a lesson for men of understanding”
[Sūrah Yūsuf, 12:111]
This story is from the best of stories because of what it relates to the varying phases of life; from trial to tribulation, from being tested to being blessed, from humiliation to grandeur, from slavery to kingship, from division to unity, from grief to joy, from superfluity to famine, from famine to glut, from hardship to ease, and from denial to affirmation. So blessed be the One who revealed it in the best manner possible.