2

For a particular project I'm working on, I'd like to return access denied instead of a 404 for non-existant user accounts. I've tried setting a custom 404 page callback and access callback in hook_menu(), and then returning FALSE when paths like user/ are visited, but that doesn't help. I've also tried calling drupal_access_denied() directly in the callback, but that just results in a server error.

I've also tried overriding the user/%user menu item in hook_menu_alter(), but that has no effect on 404 not found pages.

Is there any way to get paths like user/239841239 (any non-existent user ID) to show an access denied page instead of a 404?

[Update: I simply moved the code I was using in the custom 404 page access callback (see below) to the page callback, and now the access denied page is being served correctly.]

// If visiting a non-existent user page, redirect to access denied.
if (strpos(request_path(), 'user/') !== FALSE) {
  drupal_access_denied();
  drupal_exit();
}
5
  • 1
    Nice: my karma is partying like it's 1999. Jan 29, 2013 at 17:20
  • @Clive - well, now it's A Space Odyssey. And probably next time I notice, it'll be meaningless. Jan 29, 2013 at 17:50
  • Sorry I did up-vote in the end, now it's just the year that [insert random fact from en.wikipedia.org/wiki/2006 here] happened ;)
    – Clive
    Jan 29, 2013 at 18:03
  • re your server error, in a menu callback, you should return MENU_ACCESS_DENIED; (or return MENU_NOT_FOUND;) instead of calling drupal_access_denied() etc
    – Jimajamma
    Jan 29, 2013 at 19:03
  • @Jimajamma - I tried that, too, and was getting an infinite loop for some reason, probably because something is different about the 404 handler + a custom page callback. Jan 29, 2013 at 21:06

1 Answer 1

2

I would try setting up a conditional redirect from your 404 page to your custom users-specific error page, that only fires if the path of the referrer is user/*.

I would try to do this using Rules.

1
  • Sometimes it's the simplest thing... I moved the code from the 404 page's access callback to page callback, followed by a drupal_exit(), and it works. I'll update the question with the code I used (I'd rather not use Rules on this particular site). Jan 29, 2013 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.