When Dolores Umbridge effectively orchestrates a Ministry-led coup of Hogwarts to become Headmistress in Order of the Phoenix the Head's office bars her from entering.
"The Fat Friar told me-" he dropped his voice conspiratorially, so that Harry, Ron and Hermione had to lean closer to him to hear "-that Umbridge tried to get back into his office last night after they'd searched the castle and grounds for him. Couldn't get past the gargoyle. The Head's office has sealed itself against her." Ernie smirked. "Apparently, she had a right little tantrum."
Order of the Phoenix, Chapter 28, Snape's Worst Memory
Later, when Harry is sent back to Dumbledore's office, Phineas Nigellus has a more interesting way of phrasing it:
"And what brings you here in the early hours of the morning?" said Phineas eventually. "This office is supposed to be barred to all but the rightful Headmaster..."
Order of the Phoenix, Chapter 27, The Lost Prophecy
The gargoyle clearly has the authority to keep illegitimate wannabe Heads from entering the office. However, it isn't made clear here who exactly gets to determine which witch/wizard is the legitimate (or "rightful") Head. It clearly isn't connected to the Ministry of Magic or Umbridge would've got in. Does the gargoyle get to decide? Is it the Head which is legitimately appointed by the governors? (I don't think the governors are mentioned in relation to Umbridge since she was appointed by Ministry decree). Does the new Head have to have the blessing of their predecessor before the office will recognise a new incumbent?
I'm not sure whether there's a canon answer or not but if not informed speculation will suffice. :)