Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.

Login system outside Q&A directory

+3 votes
2,358 views
asked Feb 9, 2011 in Q2A Core by William Orellana
I know is part of the advance section, but can anyone help me or give me an idea how can I add the Q&A login system to my other directorys. I have already search the function or piece of code but I can't find anything.
commented Mar 16, 2011 by Mélanie
Hi snoopy!
I've just followed the direction about Q2A single sign on there: http://www.question2answer.org/advanced.php and everything worked fine.
commented Mar 16, 2011 by snoopy0815
Hi Mèlanie - thanx for your quick reply!
Unfortunately (or luckily...) I just get an user-name from external.
Based on this I would like to automatically log in  q2a based on the q2a user-profiles.  If I understood the "q2a single-sign on module" the right way, than this requires, that all user data (profile, picture etc) are provided by the external source - or I'm I wrong?
So what I'm seeking for is: Use Q2A internal profiles -- but instead of manual user login: Take the system information about the user who gets identified by the NTLM info (NT-user logged in at a domain).
What I have e.g. is a php function which returns a user name from external. Where should I go with this information to automatically log in a user who opens q2a?  
--> if the user is known in the q2a database (same name): just login
--> if the user is unknown: open initial profile page to let the user add profile information...

what I'm playing around with is the mechanism used to read out the cookie and replace something in there -- but this feels like a dirty hack - isn't it?  :(

...sorry - difficult to explain with just few words...
commented Mar 16, 2011 by Mélanie
I understand what you want to achieve.. I guess it is feasible, but would required some work... As a hint, look at qa-app-users.php, this is the one the either use the external or internal information.
 I think this would worth being a whole new question!

1 Answer

+5 votes
answered Feb 10, 2011 by gidgreen
 
Best answer

Interesting question. Here's how you do it in your PHP code:

define('QA_BASE_DIR', '/PATH/TO/Q2A/');
 
require_once QA_BASE_DIR.'qa-include/qa-base.php';
require_once QA_BASE_DIR.'qa-include/qa-app-users.php';
require_once QA_BASE_DIR.'qa-include/qa-db.php';
 
qa_base_db_connect(null);
 
echo qa_get_logged_in_userid().' '.qa_get_logged_in_handle().' '.qa_get_logged_in_email().' '.qa_get_logged_in_level();
 
Replace /PATH/TO/Q2A/ above with the full path to your Q2A directory (the directory that includes qa-config.php). The last line shows the kind of information you can extract about the logged in user. If no user is logged in, all of these functions (defined in qa-app-users.php) return null.
commented Feb 10, 2011 by William Orellana
Thank you!!

Works great, and thank you for this great product.
commented Feb 6, 2012 by William Orellana
edited Feb 6, 2012 by William Orellana
Thanks again for the code.

The code worked great with 1.4, but stop working with the 1.5 beta and today I upgrade to 1.5 and still doesn't work. Any ideas??

UPDATE:

Looks like the "qa_base_db_connect(null);" function was delete from 1.5 what use now?
commented Feb 7, 2012 by gidgreen
You don't need to replace qa_base_db_connect(...) with anything and can just leave it out.
...