Ruling on keeping a dog for protection inside an apartment. If it is kept in a separate room, will that prevent the angels from entering the rest of the house?
Question

Is it permissible for a woman who is living on her own, with no one else, to keep a dog for protection in her apartment? She has no option but to keep the dog inside the apartment for the purpose of protection. What is the ruling? Will the angels refuse to enter the house or not? What is the rule on praying, if the angels refuse to enter the house? If we allocate a specific room that the dog cannot enter, will the angels enter that room, or will they refuse to enter the house altogether?
Praise be to Allah.Firstly:
Ruling on keeping dogs
It is not permissible to keep dogs , except a dog for hunting or guarding livestock or guarding crops.
The evidence for that is the report narrated by al-Bukhari (2145) from Abu Hurayrah (may Allah be pleased with him), who said: The Messenger of Allah (blessings and peace of Allah be upon him) said: “Whoever keeps a dog, one or two qirats will be deducted from his good deeds every day, except a dog for guarding the crops or guarding livestock.
Muslim (2974) narrated from Abu Hurayrah (may Allah be pleased with him) that the Messenger of Allah (blessings and peace of Allah be upon him) said: “Whoever keeps a dog, except a dog for hunting or guarding livestock or land, two qirats will be deducted from his reward every day.”
And Muslim (2943) narrated from ‘Abdullah ibn ‘Umar (may Allah be pleased with him) that the Messenger of Allah (blessings and peace of Allah be upon him) said: Whoever keeps a dog, except a dog for guarding livestock or a dog for hunting, a qirat will be deducted from his good deeds each day.” ‘Abdullah said: Abu Hurayrah said: Or a dog for guarding crops.
Secondly:
Difference of scholarly opinion regarding the permissibility of keeping a dog for purposes other than hunting or guarding livestock and crops
The scholars differed regarding keeping dogs for purposes other than these three, such as keeping them to protect houses and their occupants. Most of the scholars say that this is permissible, and that is more likely to be correct.
An-Nawawi (may Allah have mercy on him) said: There is a difference of scholarly opinion regarding the permissibility of keeping dogs for purposes other than these three, such as protecting houses and roads. The more correct view is that it is permissible, by analogy with these three, because of the reason implied in the hadith, which is if there is a need for that."(Sharh Muslim  10/236).
That is if there is a need to protect the house, but the fact that a woman is living alone in the house does not on its own justify keeping a dog, unless she fears for herself because she is living in a remote area, or because she has no neighbours around her, and so on, and having a dog with her will serve the purpose of protection, and it will not just be company for her or something that is done for fun.
Shaykh Ibn ‘Uthaymin (may Allah have mercy on him) said: Based on that, if the house is in the middle of the city, there is no need to have a dog to guard it, so keeping a dog for this purpose in this situation is haram and is not permissible, and it will detract one or two qirats from the reward of its owners every day. So they should get rid of the dog and not keep it. But if the house is in the wilderness, with no one around, then it is permissible to keep a dog to guard the house and the people in it, because it is more important to guard the people of the household than to guard flocks and crops."(Majmu‘ Fatawa Ibn ‘Uthaymin 4/246).
Thirdly:
If there is a need to have a dog and keep it inside the house
If there is a need to have a dog and keep it inside the house, it should be in its own room, so as to protect the house from being contaminated with impurity (najasah). That will not prevent the angels from entering the rest of the house, in sha Allah, because the Prophet (blessings and peace of Allah be upon him) said: “The angels do not enter a room in which there are images.” Narrated by al-Bukhari (5961) and Muslim (2104). 
Shaykh Ibn ‘Uthaymin (may Allah have mercy on him) said: If the image is haram, then what appears to be the case – and Allah knows best – is that if it is in one room, that does not include the entire house, especially if the people of the household do not approve of having these images in the house, because in some families there may be one person who has been misled by the devils, so he collects images and keeps them with him or hangs them on the walls, and the other family members dislike that but cannot stop him from doing it. In this case, if the other family members dislike that, the angels will not refrain from entering the house.
As for a room in which there are images, the angels do not enter a room in which there are images."(Liqa’ al-Bab al-Maftuh  75/28).
In the case of a dog which it is permissible to keep, that does not prevent the angels from entering the room where it is, according to the correct view.
An-Nawawi (may Allah have mercy on him) said: al-Khattabi said: The angels only refrain from entering the room in which there is a dog or image when it is haram to keep that dog or image. But if the dog or image is not haram – such as dogs for hunting, and guarding crops and livestock, and images that are not treated with respect, as they are on rugs, pillows and the like – the angels do not refrain from entering because of that. Al-Qadi referred to something similar to what al-Khattabi said."(Sharh Muslim 14/84).
It says in Fatawa al-Lajnah ad-Da’imah (26/163): The fact that the angels do not enter a house in which there is a dog or an image is narrated in as-Sahihayn. My question is: If someone keeps a dog for a necessary reason, such as guarding chickens, what does Islam say about that?
Answer: If someone keeps a dog for hunting or guarding, that is permissible for him, so it does not prevent the angels from entering the house.
‘Abdullah ibn Ghadyan, Abd ar-Razzaq ‘Afifi, ‘Abd al-‘Aziz ibn ‘Abdillah ibn Baz. End quote.
Conclusion:
If there is a need for protection, for fear of thieves and the like, and there is no alternative but to keep the dog in the apartment, there is nothing wrong with that. But it should have its own room, to protect the rest of the house from becoming contaminated with impurity (najasah).
And Allah knows best.