The basic principle with regard to the washrooms that are found in restaurants is that they are only for the customers of the restaurant, but the owners of the restaurant may be easy-going and allow anyone who needs to use it to do so, even if they are not going to buy anything. But the behaviour of the one who goes to these places should be free of any cheating or deceit, so you should not act as if you are going in to eat; rather you should ask permission from the manager to go into the washroom, then if he gives you permission you may go, otherwise you should leave. If permission is given to go in, it is permissible to use the soap and towels and hand dryers, because that is part of the whole thing, but if you refrain from using these things, that is better.
If it is known that the owner of the restaurant does not like that but is forced to give permission because of shyness or embarrassment, it is not permissible to go in except in cases of necessity, because that which is taken by means of embarrassment is not permissible. The Prophet (blessings and peace of Allah be upon him) said: “It is not permissible to take a man's wealth except if he gives it willingly.” Narrated by Ahmad, 20172; classed as saheeh by al-Albaani in Saheeh al-Jaami’, no. 7662.
For the prohibition on that which is taken by means of embarrassment, see: Tuhfat al-Muhtaaj, 6/317.
In cases of necessity, you may go in. The fuqaha’ have stated the situations in which it is permissible to take the food and water of another person without his permission for fear of dying, then the price of it should be given to him in compensation.
Ibn Qudaamah said in al-Mughni (9/335): In the case of necessity, when there is no other food, it depends. If a person is in urgent need of food and he could find food only with someone else, then we examine the situation. If the one who owns the food is in urgent need of that food, then he is more entitled to it and no one else has the right to take it away from him, because they are both in equal need of the food, but one owns it. Therefore this is like the case in which someone takes the food of another without any urgent need. If anyone takes the food from him and he dies, then he is liable for his death, because he killed him unlawfully. But if the owner of the food is not in urgent need of it, then he has to give it to the one who is in urgent need of it so as to preserve the life of a human being which is protected by sharee’ah. Therefore he has to give the food to him, as he would have to do whatever he can to save him from drowning or burning. If he does not do that, then the one who is in urgent need of the food has the right to take it from him because he is more entitled to it than the one who owns it, so it is permissible for him to take it from him. If he needs to fight in order to do that then he may fight for it, and if the one who is in need is killed then he is a martyr and the one who killed him is liable. If that fighting leads to the owner of the food being killed, then there is no retaliation or compensation, because he did wrong by fighting him. That is similar to the case of one who defends himself against an aggressor. But if it is possible to take it by buying it or asking for it, then he does not have the right to fight for it, because it is possible to acquire it without doing that. End quote.
And Allah knows best.