By stmind on
I'd like to provide a custom "access-denied" node, preferably with a login form embedded in it. When the user submits the form, they should be redirected to the page they were trying to access intially.
By default, when using the build-in access-denied function, and the user then logs in using the normal login-block, it works as it should, with the user being directed to the right page.
However, when I implement a custom access-denied page, and a user logs in from there, they are returned to the "access-denied" page as though it were the node they were trying to access.
How can I preserve the original node request to get the proper redirect?
Thanks!
Comments
How to solve your problem
How to solve your problem depends on how you are serving your custom Access Denied page.
Ideally, you sould be able to use Drupal's built-in fuctions to redirect the user back to the page they originally requested. In your login form (you are using Drupal's
form()function, right?), just useurl('user/login', drupal_get_destination())for the form target.Hope this helps.
closer
Thanks for the response.
When used on a custom access-denied node,
drupal_get_destination()returns the path to the current node, not the one that was "requested but denied".I've also tried just getting
$_GET['q'], but that also just prints the current node.To be clearer, my custom access-denied node is node 26, with an alias of 'private'. I have a custom 403 set to "node/26".
This node is a drupal page, with a text greeting and a php block containing
I have another node, let's say node 10, that only accessible to authenticated users.
When an anonymous user attempts to access node/10, the browser's location bar is /node/10 but they are given node 26, with the following output:
So how do I get the intended destination?
Just a shot in the dark
Just a shot in the dark before I really start digging into this, but have you looked in the $_SERVER[] superglobal?
OK, this can be done in the
OK, this can be done in the code, but I don't know how pretty it is.
the function
drupal_access_denied()changes the node request that is returned byget_destination(), etc.My response is to set a global variable with the information *you* want just before that change, so you can reference it later.
Around line 226 of common.inc add
then, in your custom 403 page, use this for the target of your form:
Let's see where that gets us...
Thanks Corey
That solution works for me. Here's my 2.6.3 common.inc drupal_access_denied() after your changes:
I'm not too thrilled about modifying Drupal code, as of yet I have avoided it (which speaks very highly of the theming and module APIs). But, this is a small hack that I can live with, and I've documented it in my site's changelog so I'm not scratching my head later at upgrade time. It would be nice if in the future there could be a better request object available that contained this type of information.
So my custom error 403 node looks like this:
I have this node aliased to "/private". It is also flagged as unpublished, so it does not show up in searches and lists. Lastly, I configured the login block not to appear on the page "private".
In my site settings I have specified the custom error 403 page as "private".
Works like a charm. Thanks for your help!
This works for me on 4.6.2
Put the following form in a page and define it as your access-denied page. You can set the destination in the FORM tag. I have left all the < tags off the beginning of the lines as I was having trouble posting it.
gpdinoz
Thought for the day
"If you're not getting the answers you want maybe you're asking the wrong questions"
Regards
Geoff
The destination is not known
Thanks for your reponse. The problem is that the destination is not known ahead of time, it could be any node that they were denied access to.
For anyone still struggling with this...
I was able to determine the original requested URL on my custom 403 page by accessing Server Variables.
You can then add $pageurl to the destination parameter in the querystring to provide links for round-trip login that returns to the original request. I've also used this URI to build a simple request access form for users that are authenticated but not in the correct role/organic group to access the URI.
e.g.
I'm not certain if this works in all configurations, but it's working well for me on a production site.
This is exactly what I'm
This is exactly what I'm looking for (minus the registration form). Unfortunately, I can't seem to get it to work. (Drupal 6.20)
I have created an Access Denied node with the following PHP input:
And I get the following error:
Have I misunderstood where the PageURI function is supposed to go?
Thanks in advance for any help that can be thrown my way.
Update
I've discovered the r4032login module (a.k.a. Redirect 403 to User Login) and am using that to accomplish the flow-through on access denied pages.
To be clear
To be clear, in my example above I was using external webserver authentication, not drupal. If you are logging via drupal http://drupal.org/project/r4032login is a far more elegant solution.