Redirect After Registration in Drupal - authentication

Any one know how to go back to the "last page" after a user is presented the login screen and chooses to create a new account?
Basically the sequence is this:
User tries to get to protected content
Redirected to login page
If he logs in he is redirected to original page
If he chooses "create new account" and fills it out, he is redirected to the home page
How do we get him automatically redirected to the original page (not a static page).

There are several ways to go about this. The most straight-forward is to have a login link somewhere in the navigation that appends the destination to the url. The code for this is something like:
<?php
if (user_is_anonymous()) {
$link = l(t('Login'), 'user/login', array('query' => drupal_get_destination()));
}
?>
You can also set a custom access denied page at admin/settings/error-reporting that could either go to a callback that outputs the above code, or to a simple php node that outputs that code.
Additionally, the user login block provided with Drupal core uses the same method to redirect a successful login back to the originating page.
Edit: Note that the above methods will rarely work for registration, because there are more steps involved there. Specifically, when a user needs to verify an email address, passing the originating destination along via the email would involve modifying the core user registration process.
It will potentially still work on a site configured to not verify email addresses. The idea then would be to provide 2 links: 1 for login and the other for registration, both passing along destination information.
LoginToboggan may also be worth pursuing, although it doesn't yet offer the exact registration feature you're looking for.

straight php would be to include a header of this form:
<?php header("Location: " . $_SERVER['HTTP_REFERER']); ?>
for more information refer to the php manual
EDIT:
you can also include a hidden field in your form and set it to
$url = $_SERVER['PHP_SELF']; // or HTTP_REFERER depending on the setup
include the header code snipped to your registration form.

Related

How to Redirect Users to the Login Page when Trying to Access an Unpublished Content Page in Drupal 7

I am trying to have users accessing some draft content (unpublished contents) links so they can log in and get redirected to these links. Once they click on the link, they will get redirected to the login page and once they log in with their credentials they will get redirected to the desired page. What happens is that when they click on any links they get redirected to the homepage.
I tried to add a rule in the /admin/config/workflow/rules but I was unable to figure out the conditions and actions of the proper event I need to add in order to have my users get redirected to login and then straight to the desired page (either published or unpublished it doesn't matter).
I am using Drupal 7.67 and mostly UI interface on a Windows 10 machine.
The easiest way I can think of is create function like this.
function modulename_redirect_when_unpublished(){
//Check if user is in rigth place (viewing nodes).
if(arg(0) == 'node' && arg(1)){
//Load this node.
$node = node_load(arg(1));
//Check if node status is different than published, and if user is not logged.
if($node->status != 1 && !user_is_logged_in()){
//Prepare destination to come back.
$destination = arg(0) . '/' . arg(1);
//Go to login page with destination saved.
drupal_goto('user/login', array('query' => array('destination' => $destination )));
}
}
}
You can pass any path in "destination" parameter in GET and after login it will redirect you there.
Only question is where to put this function. For tests purpose I called it in one of my custom module hook_init, but you can put it somewhere else, just be sure it will be called on 'node/%' pages - you mentioned something about using Rules, it could be done that way but I think it's overcomplicated to create custom rule just for one case to fire this particular function, so I suggest you to put this code in some hook_init like I did.
I've tested it on Drupal 7.57.

Opening an XPage (single page application) to a specific anchor (appPage) for unauthenticated users

I have a mobile XPages application which uses the single page application control (xe:singlePageApp) of the XPages extension library. The application also uses a workflow engine which sends out emails with links to documents to users so they can approve requests.
The link URL is composed like
http://hostname/app.nsf/m_page.xsp?action=openDocument&documentId=2A2A#requestForm
where requestForm is the name of the appPage containing the form to display a single request document.
If the user is already logged in, the browser opens and displays the document as intended.
However, if the user is not already logged in, the Domino login form is displayed (session based authentication). When the user then logs in, the same XPage is opened, but to the default page (selectedPageName attribute of the singlePageApp) instead of the appPage with the pageName requestForm. The reason for this behavior is that after submitting the login form the anchor part (#requestForm) is no longer present in the URL the browser is redirected to because the #requestForm-part is never sent to the server where the redirect URL is computed in the first place.
Possible solutions I can think of are
put the intended pageName in a real URL parameter (like documentId), parse the URL and modify the browser location (from ...&documentId=2A2A&pageName=requestForm to ...&documentId=2A2A#requestForm)
check the URL for the existence of the documentId parameter and modify the browser location (add #requestForm) if it is present
modify the Domino login form as per Jake Howlett's Suggestion (which is a not always permitted)
I was wondering now if there are more elegant solutions to this.
I would take the first option in your case. But instead of handling the url change at the client-side, I would handle this on the server-side. Otherwise, client will load the initial page once and submit an additional request to the server.
On the beforePageLoad event:
var url:XSPUrl=context.getUrl();
if(url.hasParameter("pageName")) {
var pageName=url.getParameter("pageName");
url.removeParameter("pageName");
facesContext.getExternalContext().redirect(url.toString()+"#"+pageName)
}
This will do the redirection before loading the page.

MODX Login Chunk for isLoggedIn

I am using isLoggedIn Script for a members only site. It seems to work nearly perfect besides one thing: I am using the normal Chunk (lgnLoginTpl) which comes with the complete Login package. There in the form an action with an url/domain etc. is needed but i need it dynamic. This means if you enter an protected page you are redirected to login and after logging in back to protected page. I have more than one protected page so it must be some kind of "intelligent".
&redirectToPrior=1 is also not working in this case.
Can anybody help and tell me how to set up lgnLoginTpl correctly to get isLoggedIn script to work as I need?
It sounds like the first part of your problem, redirecting to login page when needed, is solved simply by setting the correct "unauthorized page" in your system settings. &redirectToPrior should be what you need, by "not working in this case", do you mean it's not working at all or not redirecting back to the previous page, regardless of what it was?
If it is the fact that it simply isn't working, thats probably because it relies on $_SERVER['HTTP_REFERER']`, ie a php server variable telling the page which page redirected to it. This will however not always be set depending mostly on browser. A workaround i've implemented is to place a little snippet at the top of the Unathorized page
$_SERVER['HTTP_REFERER'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
This will "manually" set the HTTP_REFERER to the current URL, which is what you want, as the URL doesn't change after you've been redirected from a protected page to the unauthorized URL. And then you can use the &redirectToPrior param of the Login? snippet.
Thank you for your answer.
I tried some things and I now it seems I have only one problem which is that I am redirected to unauthorized page (login page), that works fine but after logging in I am not redirected to the right protected page.
I made to different pages test1 and test2. If I am loggin in I am always redirected to test1.
test1 login -> test1
test2 login -> test1
It seems &redirectToPrior=1 is overriden by &loginResourceId=12. I am calling the plugin with following code:
[[!Login? &loginTpl=`lgnLoginTpl` &logoutTpl=`lgnLogoutTpl` &errTpl=`lgnErrTpl` &logoutResourceId=`0` &loginResourceId=`12` &redirectToPrior=`1`]]
Removing &loginResourceId=12 (ID 12 is test1) doesn't work at all and redirects my to my 404 page (home).
&loginResourceId=12 should be removed but what to insert instead?

Accessing a url directly without login

I am considering doing this -
Any url (excecpt those I disallow specifically) can be accessed directly without signing-in, however if you click on any of the links on the page, it will redirect you to the sign-up page
I am thinking of several ways of doing it, but neither is flexible enough to work with devise
Create a new link_to_not_registered helper which I will use on every link_to and it will check if the user is logged in or not
create a before_filter to check if the user is logged in. This is a bit problematic, as I don't know how to create a filter only when linking and not when directly accessing a page
Have an external flag to test if the user is logged in and change the page accordingly.
neither way helps me redirect the user after sign-in/sign up (new helper links to sign up, before filter becomes too complex, flags are too simple)
Is there a way to create a functionality of direct access to show actions while clicking on links requires login?
I think the best approach is a before_filter. You can check previous page by request.referrer, so if it's a page inside your app, you redirect user to signin path
def to_signin
redirect_to singin_path if request.referrer["http://myapp.com"]
end

Plone make login mandatory on every page but main page

A customer wants to have its Plone site behave like this:
the main page can be seen by anonymous users
you have to be registered and authenticated to see any other page
the site is open for registration, so the forms to authenticate and register should also be made visible by anonymous
How one can approach this?
Is there a way to hook a python script/class/method/function to any request made by the user? Overriding the main_template.pt and adding a TAL call to a method that does these checks would be enough?
The tricky part is that even if the anonymous can only visit the main page, the main page in itself is made up of other content types which should be only viewed by authenticated users (by their restrictions, not because of workflow).
I ruled out, maybe a mistake?, workflow because then everything should be made private but still the global_nav is made out of folders which, if the workflow approach was taken, should be private/non-viewable by anonymous.
Cheers,
Try this:
add a state internally published to your workflow
copy permissions configuration from the "public" state into the new state and than remove the "View" permission from the Anonymous User (maintain the 'Access content information', that's the key). Then add all needed transitions.
put your home page in "public" state
put everything else in 'internally published' state
This should work because if you link content's information in the homepage, this will work, but if someone try to access a content he will miss the "view" permission and will be redirected to the login.