On Wed, Apr 30, 2014 at 11:52 AM, Glen glen.84@gmail.com wrote:
Hi,
I want to log a user into our wiki if they are logged into our website.
I assume that I need to use the UserLoadFromSession hook, but at this point in the code, the MW session is already active. Does it make sense to close the MW session, configure & start my website session in order to access the user ID, and then re-open the MW session using wfSetupSession(), or is this not recommended?
Sortof. The UserLoadFromSession hook is called before mediawiki sets up the user object, so when the hook is called, mediawiki hasn't setup the user's session yet.
I hate to use it as an example, but with CentralAuth (see CentralAuthHooks::onUserLoadFromSession()), we use our own cookie to track the user's session, and load that session in the UserLoadFromSession hook. This is probably close to what you want to do, since we also handle if the user needs to be autocreated there too.
I see that some people access their website username from another cookie, but I'd like to avoid this if possible.
This is the rough idea that seems to be working, I think:
$wgHooks['UserLoadFromSession'][] = function($user, &$result) { // Check if the user is already logged into MW. // How do I do this???
// Close MW session. session_write_close(); // Configure website session. session_name('x'); session_save_path('x'); ini_set('session.cookie_domain', '.x'); session_start(); // Check if user is logged in to website. if (isset($_SESSION['Zend_Auth'])) { $userId = (int)$_SESSION['Zend_Auth']['storage']; // Close website session. session_write_close(); // temp, look up username in DB. if ($userId == 123) { $username = 'xyz'; } $user = User::newFromName($username); // Log user in. $user->setToken(); $user->saveSettings(); wfSetupSession(); $user->setCookies(); } else { wfSetupSession(); } return true;
};
I'm not sure if this is the best option, and also how to bypass this code if the user is already logged into the wiki.
I also need to auto-create an account for users without MW accounts, but I think I can use $user->addToDatabase() for that.
Any help would be greatly appreciated.
Glen.
MediaWiki-l mailing list MediaWiki-l@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-l