The scholars have mentioned: If the issue with the person being expelled from the masjid is that he is harmful, then according to al-qiyās (deductive reasoning), anyone who is harmful to his neighbours at the masjid, whether it be by way of being foul-mouthed, foolish, foul-smelling, inflicted with a contagious disease (like leprosy) and all that which the people are harmed by. [In such cases] it is permissible for them [i.e. the Imām] to banish him [from the masjid] as long as there is a harm, up until this harm comes to an end.
Abū ʿUmar ibn ʿAbd al-Barr said: I witnessed our shaykh, Abū ʿUmar Aḥmad ibn ʿAbd al-Mālik ibn Hishām (رحمه الله), issue a verdict with regards to a man [whose neighbours] complained about him. They complained that he had harmed them in the masjid, verbally and physically. So they went to consult him [i.e. Abū ʿUmar Aḥmad ibn ʿAbd al-Mālik ibn Hishām], he then issued a verdict expelling the man, keeping him away from the masjid, and preventing him from observing the [daily] prayers, due to his prolonged foolishness, thus keeping [the community] safe from him.
So one day, I [Ibn ʿAbd al-Barr] reminded him (Abū ʿUmar) of this affair, reviewed his statement with him, and requested evidence for his verdict. He used as evidence the narration concerning garlic saying: He is—in my opinion—more harmful than the one who eats garlic, and he [the one who eats garlic] is prevented from joining the prayers at the masjid.1
 ʿUmar ibn al-Khaṭṭāb (رضي الله عنه) said: You people eat from two plants which I see to be repulsive: onion and garlic. Verily, I witnessed that when the Messenger of Allāh (صلى الله عليه وسلم) smelt onion or garlic from a man in the masjid, he would order him to be taken out to al-Baqīʿ. So whoever eats them, let him cook them till death [i.e. until the odour erodes]. [Reported by Muslim in his Ṣaḥīḥ #567]
Source: Tafsīr Imām al-Qurtubī Vol.12, pg. 267-268
Translated by: Munīb al-Ṣumālī