Login Integration
Posted by tlshaheen
|
Login Integration May 11, 2010 01:46PM |
Registered: 16 years ago Posts: 23 |
I've successfully integrated user registration and activation using the External Authentication module. However, I can now not figure out how to integrate my login/logout process! I've searched these forums, and the best I could come up with is to include the cookietest before submitting the login function. But what function do I use, how do I call it, and what parameters does it take to log in a user? Is there a document explaining this? I've looked through login.php, but I'm not sure how to populate the $_POST fields or how to call the login portion of the file.
Thanks
Thanks
|
Re: Login Integration May 11, 2010 02:01PM |
Moderator Registered: 19 years ago Posts: 1,301 |
You should be able to use the External Authentication module to login your users as well. Using the user_session_restore hook, you will look to see if the user is logged in to your main site (via cookie, session, whatever you are using on your main site). If the user is logged in to your main site, you will set their Phorum session cookie, if not, you will do nothing (if they have no Phorum cookie) or delete the Phorum cookie (if they still had a Phorum session).
Joe Curia (aka Azumandias)
Modules: l0Admin Mass Email00000000l000000Automatic Time Zones000ll.l00000Enhanced Custom Profiles0.00Google Calendar0000l.l000000Post Previews
000000000Admin Security Suite000000000000Check Modules for Upgrades0000External Authentication000000Group Auto-Email00000.00000Private Message Alerts
000000000Attachment Download Counter0000Custom Attachment Icons000ll.ll00Favorite Forums000000.00000Highlighted Search Terms0000Self-Delete Posts Option
000000000Attachment Watermarks0l00000000Custom Language Database00l.l.0Forum Lockdown00000.00000Ignore Forums0000000000000Threaded Tree View
000000000Automatic Message Pruning00.llll.00Easy Color Scheme Manager0l.l00Forum Subscriptions0000lll000Moderated User Group
Templates:lGeneric Integration000000000 0000Simple Rounded000000 00000000Tabbed Emerald
Joe Curia (aka Azumandias)
Modules: l0Admin Mass Email00000000l000000Automatic Time Zones000ll.l00000Enhanced Custom Profiles0.00Google Calendar0000l.l000000Post Previews
000000000Admin Security Suite000000000000Check Modules for Upgrades0000External Authentication000000Group Auto-Email00000.00000Private Message Alerts
000000000Attachment Download Counter0000Custom Attachment Icons000ll.ll00Favorite Forums000000.00000Highlighted Search Terms0000Self-Delete Posts Option
000000000Attachment Watermarks0l00000000Custom Language Database00l.l.0Forum Lockdown00000.00000Ignore Forums0000000000000Threaded Tree View
000000000Automatic Message Pruning00.llll.00Easy Color Scheme Manager0l.l00Forum Subscriptions0000lll000Moderated User Group
Templates:lGeneric Integration000000000 0000Simple Rounded000000 00000000Tabbed Emerald
|
Re: Login Integration May 11, 2010 05:56PM |
Registered: 16 years ago Posts: 23 |
I understand from the example code that in order to log in a user, I can set $sessions array as
[code="php"]
$sessions[PHORUM_SESSION_SHORT_TERM] = $phorum_user_id;
$sessions[PHORUM_SESSION_LONG_TERM] = $phorum_user_id;
[/code]
but what do I pass $sessions to then to log in the user? I guess I don't understand how/what cookie I set or delete to login/logout. Thanks again
[code="php"]
$sessions[PHORUM_SESSION_SHORT_TERM] = $phorum_user_id;
$sessions[PHORUM_SESSION_LONG_TERM] = $phorum_user_id;
[/code]
but what do I pass $sessions to then to log in the user? I guess I don't understand how/what cookie I set or delete to login/logout. Thanks again
|
Re: Login Integration May 11, 2010 06:05PM |
Admin Registered: 22 years ago Posts: 8,532 |
There is no need to set a cookie when using user_session_restore(). The thing is that Phorum will normally try to restore a session by checking its cookie. But user_session_restore() can be used to intercept that process and to tell Phorum what user is logged in at the moment. Therefore, a Phorum cookie is not needed here. Only a cookie or whatever session mechanism you use in the authentication master application.
Looking at this code, it would fit in the user_session_restore hook, like:
Assumption here is that the user id from your master system ($master_id) is equal to the user_id of the Phorum users that you created. If not, then you would have to implement some mapping between master app user ids and Phorum user ids (which the external auth module does too).
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
Looking at this code, it would fit in the user_session_restore hook, like:
function your_user_session_restore_module_function($sessions);
{
$master_id = your_master_app_auth_id_get();
if ($master_id) {
$sessions[PHORUM_SESSION_SHORT_TERM] = $master_id;
$sessions[PHORUM_SESSION_LONG_TERM] = $master_id;
}
return $sessions; // <-- so here you pass the sessions to Phorum
}
Assumption here is that the user id from your master system ($master_id) is equal to the user_id of the Phorum users that you created. If not, then you would have to implement some mapping between master app user ids and Phorum user ids (which the external auth module does too).
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
|
Re: Login Integration May 11, 2010 06:35PM |
Registered: 16 years ago Posts: 23 |
Thanks for the assistance guys, but I'm still a little lost at returning $sessions. I set $sessions, but theres no code after setting it to do anything with it. I know I'm missing something, just not sure what. Sorry this is so simple and I'm missing it guys!
Relevant code from my main sites login:
[code="php"]
if (mysql_real_escape_string(salt($password)) == $result['Password']) {
//Set session variables
session_start();
$_SESSION['HTTP_USER_AGENT'] = salt($_SERVER['HTTP_USER_AGENT']);
$_SESSION['username'] = $result['Username'];
$_SESSION['user_id'] = $result['ID'];
$_SESSION['access_level'] = $result['Level_access'];
$_SESSION['f_name'] = $result['F_Name'];
$_SESSION['l_name'] = $result['L_Name'];
//Login to Phorum
$curcwd = getcwd();
chdir("/home/swimuto1/public_html/forum");
include($_SERVER["DOCUMENT_ROOT"] . "/forum/common.php");
$sessions[PHORUM_SESSION_SHORT_TERM] = $_SESSION['user_id'];
$sessions[PHORUM_SESSION_LONG_TERM] = $_SESSION['user_id'];
chdir($curcwd);
$errorMessage = "None";
}
[/code]
Relevant code from my main sites login:
[code="php"]
if (mysql_real_escape_string(salt($password)) == $result['Password']) {
//Set session variables
session_start();
$_SESSION['HTTP_USER_AGENT'] = salt($_SERVER['HTTP_USER_AGENT']);
$_SESSION['username'] = $result['Username'];
$_SESSION['user_id'] = $result['ID'];
$_SESSION['access_level'] = $result['Level_access'];
$_SESSION['f_name'] = $result['F_Name'];
$_SESSION['l_name'] = $result['L_Name'];
//Login to Phorum
$curcwd = getcwd();
chdir("/home/swimuto1/public_html/forum");
include($_SERVER["DOCUMENT_ROOT"] . "/forum/common.php");
$sessions[PHORUM_SESSION_SHORT_TERM] = $_SESSION['user_id'];
$sessions[PHORUM_SESSION_LONG_TERM] = $_SESSION['user_id'];
chdir($curcwd);
$errorMessage = "None";
}
[/code]
|
Re: Login Integration May 12, 2010 02:32AM |
Admin Registered: 22 years ago Posts: 8,532 |
You're doing things up-side-down here. It's true that a lot of systems will force you into this kind of coding for integrating authentication. The login / session code from the main application then has to set a cookie for the bridged application to recognize the user.
That method is considered seriously flawed by the Phorum team. There is no tight coupling at all between the session from the main application and the session from the bridged application (Phorum in this case).
In Phorum we provide functionality by means of the "user_session_restore" hook to make it possible through a module to let Phorum investigate the session of the master application. BTW: this is done at the start of every request (just like the main app would do). If the module finds an active session there, it can set the user_id to use for the Phorum session. Look ma, no cookie!
The big advantage here is that you do not have to hack the main application code and that the moment that the main application session ends, Phorum will follow in its footsteps.
Based on your code, an example module for specifically this job might look like this:
[code=php]
<?php
/* phorum module info
hook: user_session_restore|phorum_mod_your_user_session_restore
title: Inherit session from my application
desc: This module will inherit the user_id session from the main application.
*/
function phorum_mod_your_user_session_restore($sessions)
{
// Phorum does not start a session, so we have to start it
// here to be able to access the main application's $_SESSION data.
// The session_id() is checked to not to call session_start() twice.
if (!session_id()) session_start();
// If the user is logged into the main session, then tell Phorum
// to use the user_id from the session.
if (!empty($_SESSION['user_id'])) {
$sessions[PHORUM_SESSION_SHORT_TERM] = $_SESSION['user_id'];
$sessions[PHORUM_SESSION_LONG_TERM] = $_SESSION['user_id'];
}
return $sessions;
}
?>
[/code]
The only part that has to be taken care of in addition, is creating the user with the same user_id in Phorum using phorum_api_user_save(). After the user is created, the above module should work (untested, but I hope the idea is clear).
If you are not synchronizing the user_ids, then the hook code from above could for example lookup the Phorum user_id by searching for $_SESSION['username']. However, the user_id method would be best performance-wise, since it would not require an additional SQL query to lookup the Phorum user_id.
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
That method is considered seriously flawed by the Phorum team. There is no tight coupling at all between the session from the main application and the session from the bridged application (Phorum in this case).
In Phorum we provide functionality by means of the "user_session_restore" hook to make it possible through a module to let Phorum investigate the session of the master application. BTW: this is done at the start of every request (just like the main app would do). If the module finds an active session there, it can set the user_id to use for the Phorum session. Look ma, no cookie!
The big advantage here is that you do not have to hack the main application code and that the moment that the main application session ends, Phorum will follow in its footsteps.
Based on your code, an example module for specifically this job might look like this:
[code=php]
<?php
/* phorum module info
hook: user_session_restore|phorum_mod_your_user_session_restore
title: Inherit session from my application
desc: This module will inherit the user_id session from the main application.
*/
function phorum_mod_your_user_session_restore($sessions)
{
// Phorum does not start a session, so we have to start it
// here to be able to access the main application's $_SESSION data.
// The session_id() is checked to not to call session_start() twice.
if (!session_id()) session_start();
// If the user is logged into the main session, then tell Phorum
// to use the user_id from the session.
if (!empty($_SESSION['user_id'])) {
$sessions[PHORUM_SESSION_SHORT_TERM] = $_SESSION['user_id'];
$sessions[PHORUM_SESSION_LONG_TERM] = $_SESSION['user_id'];
}
return $sessions;
}
?>
[/code]
The only part that has to be taken care of in addition, is creating the user with the same user_id in Phorum using phorum_api_user_save(). After the user is created, the above module should work (untested, but I hope the idea is clear).
If you are not synchronizing the user_ids, then the hook code from above could for example lookup the Phorum user_id by searching for $_SESSION['username']. However, the user_id method would be best performance-wise, since it would not require an additional SQL query to lookup the Phorum user_id.
Maurice Makaay
Phorum Development Team
my blog
linkedin profile
secret sauce
Sorry, only registered users may post in this forum.