These are the words of Ibn al-Qayyim in his book Tareeq al-Hijratayn, where he speaks of Allah's wisdom in letting a person commit sin, enabling him to do so and preparing all the means for him that lead to that. If He willed, He could have protected him and prevented him from doing that, but he let him do that for great and wise reasons that no one knows in their entirety except Allah. Among these reasons, he mentioned: He causes him to forget his own acts of obedience and makes him focus on his sin, so that it continues to preoccupy his mind. For when Allah wills good for a person, He makes his good deeds invisible to him and diverts him from mentioning them, and He distracts him by causing him to focus on his sin, so that he is always thinking of them, until he enters Paradise. So he does not think of his deeds that are already accepted, and he does not mention them.
One of the salaf (early generation) said: A person may commit a sin and enter Paradise because of it, or he may do a righteous deed and enter Hell because of it. They said: How is that? He said: He commits a sin then keeps thinking of it, and when he remembers that he regrets it, feels sorry, beseeches Allah, hastens to erase it and feels brokenhearted and humble before his Lord, and that removes self admiration and pride from him. Or he may do a good deed then keep thinking of it, talking about it, feeling proud of it and feeling arrogant because of it, until he enters Hell.
End quote from Tareeq al-Hijratayn, p. 169-172
What is meant is that by His wisdom, Allah, may He be exalted, may decree sin for His slave and let him commit a sin - even though He hates sin and the act of committing it, and He forbids it and warns of punishment for it - so that the person will forget about his good deeds and acts of obedience to Allah; thus his Lord protects him from self-admiration, showing off and arrogance. Then He preoccupies his mind by making him focus on his sin and the bad situation he is in, because his Lord saw him where He forbade him to be, and thus he exposed himself to the wrath and punishment of Allah. He was alarmed because of this situation he found himself in, thus he was stricken with fear of His vengeance and punishment, which prevents him from falling into sin again and encourages him to obey his Lord and feel shy before Him. And that continues, with him feeling afraid and nervous, until Allah admits him to Paradise by His mercy.
Allah, may He be exalted, does not admit him to Paradise by means of the sin he committed; rather He admits him to Paradise by His mercy as a reward for what resulted from his committing that sin of fear (of Allah) and worry (about the punishment).
Abu Naeem narrated in al-Hilyah (6/98) from Shaddaad ibn Aws that the Messenger of Allah (blessings and peace of Allah be upon him) said: Allah, may He be glorified and exalted, said: By My Glory, I will not cause My slave to feel secure in two situations or feel afraid in two situations. If he feels secure in this world, I shall make him feel afraid on the Day when I gather My slaves together. And if he fears Me in this world, I shall make him feel secure on the Day when I gather My slaves together.
Classed as hasan by al-Albaani in as-Saheehah, 742
The point is that Allah may decree that a person should commit sin because of what He wills for him of good in the form of repentance, fear (of Allah), returning to Him, humbling himself and giving up self-admiration, then turning to Allah, as happened in the case of Umar ibn Abd al-Azeez (may Allah have mercy on him). When he was the governor of Madinah, he gave Khubayb ibn Abdillah ibn az-Zubayr fifty lashes and poured water on him on a very cold winter's day, which led to his death. Then he regretted that deeply and it continued to affect him for the rest of his life. That sin resulted in him becoming humble, fearing Allah, regretting it, regarding the sin as significant and regarding as little what he did of good deeds that led him to attain the high status that he subsequently attained.
Ibn Katheer (may Allah have mercy on him) said:
Umar ibn Abd al-Azeez gave Khubayb ibn Abdillah ibn az-Zubayr fifty lashes on the orders of al-Waleed, and he poured cold water over his head on a cold winter's day, and make him stand at the door of the mosque for that day, then he died (may Allah have mercy on him). After the death of Khubayb, Umar ibn Abd al-Azeez was very afraid and did not feel safe. If he was given any glad tidings with regard to the Hereafter, he would say: How could I attain that when Khubayb is standing in my way?
According to another report he used to say: That is if Khubayb is not standing in the way, then he would weep like a woman who has lost her child.
If someone praised him, he would say: How bad is what I did to Khubayb! If I am saved from that sin, then I shall be fine.
He remained governor of Madinah until he had Khubayb flogged and he died as a result, then he resigned and immersed himself in grief and fear from that time, and he began to strive hard in worship and weeping. That (the beating of Khubayb) was a mistake on his part, but because of it he attained a great deal of good, namely his worship, weeping, grief, fear, kindness, justice, charity, righteousness, freeing slaves and so on. End quote.
Al-Bidaayah wa'n-Nihaayah, 9/87
And Allah knows best.
Reproduced from Islam QA