Jesus had the legal authority to cleanse the temple not because he was a rabbi but because he claimed to be like Solomon, the "Son of David" and thus the builder of God's house (2 Samuel 7). This is evident from a careful reading of the gospels through the lens of the Hebrew Bible.
In the synoptics the temple cleansing is immediately preceded by Jesus' entry into Jerusalem on a donkey. In this event Jesus symbolically replays a crucial moment in Israel's monarchy, the day Solomon was crowned king in Israel (1 Kings 1).
When David was old and enfeebled his eldest son, Adonijah, took advantage of his father’s weakness and united publically with the king’s men, declaring his intentions to the throne. But a few were troubled by this, among them Bathsheba who went to David and reminded him of the promise he made to her and her son. David swears an oath to her, saying, "Solomon your son shall be king after me and he will sit on my throne in my place." He then instructs them how to go about the coronation.
set Solomon my son on my own mule and take him down to the Gihon.
There have Zadok the priest and Nathan the prophet anoint him king
over Israel. Blow the trumpet and shout, ‘Long live King Solomon!’
And they did as he said.
Then they sounded the trumpet and all the people shouted, “Long live
King Solomon. And all the people went up after him, playing flutes
and rejoicing greatly, so that the ground shook with the sound.
But of course the news didn’t make everyone glad. Adonijah and his supporters fled from their feast in fear.
The fact that the elements of the coronation, most notably the riding into Jerusalem on mule, are repeated three times in 1 Kings 1(1:32-35; 38-40; 43-48) indicates that they were very important. This is the first dynastic transfer of power in Israel’s history. An event the nation could not easily forget. And while I do not know for certain, it would not surprise me to discover that these elements, including the entry into Jerusalem on a mule, became standard practice for all subsequent davidic coronations. Zechariah 9:9 could be hinting at this practice
Rejoice greatly, O Daughter of Zion! Shout, Daughter of Jerusalem!
See, your king comes to you, righteous and having salvation, gentle
and riding on a donkey, on a colt, the foal of a donkey.
Certainly the people present at Jesus’ entry that day understood his actions. There interpretation of the events is evident from their cries recorded in each of the four gospels. In Matthew they shout,
Hosanna to the Son of David!
Mark records them saying,
Blessed is the coming kingdom of our father David!
In Luke we hear them say,
Blessed is the king who comes in the name of the Lord!
And in John they cry,
Blessed is the King of Israel.
That the public recognizes Jesus’ claim to the throne is significant because no where in the gospels does Jesus openly declare himself king. Peter’s confession in Matthew 16:13-19, Mark 8:27-30 and Luke 9:18-21 is met by Jesus with a blessing as well as a command for silence. (Matthew 16:13-20). While it may be likely that some in the crowd remembered this small passage in Zachariah, Its more likely that they looked upon Jesus’ actions with one eye on the past and remembered how the first ‘son of David’ was crowned king in Jerusalem.
Jesus actions that day were symbolic. By riding a donkey into Jerusalem, Jesus declared himself to be like Solomon, the "Son of David."
We therefore need to read the subsequent events in the temple in light of this identification. In 2 Samuel 7 the promise of eternal dynasty or "house" for David is coupled with the building of a house for God. God says through the Nathan the prophet
When your days are over and you rest with your fathers, I will raise
up your offspring to succeed you, who will come from your own body,
and I will establish his kingdom. He is the one who will build a
house for my Name, and I will establish the throne of his kingdom
forever. I will be his father and he will be my son.
Of course David’s son Solomon built a temple. But Jesus by entering Jerusalem on a donkey and then cleansing the temple demonstrates that “one greater than Solomon is here (Matthew 12:42).” In the temple cleansing Jesus demonstrates how he has taken up the responsibility given to David’s son.