Last modified: 2011-04-04 21:28:28 UTC
I am having a problem with the UserLoadFromSession hook. In the hook, I use User::newFromName() to obtain the user object and if it's a new user, I authenticate it with our SSO service and add it to the database; otherwise I update $user and return, leaving $result as null. I've battled with this code a fair bit, but have learned from looking at CASAuthentication's use of this hook and believe that I'm now doing thins "the right way". However, although $user is set up as expected when I leave the hook, $this in loadFromSession doesn't contain the values I've set up in the hook. I've even used the ugliness of var_dump to splat the (empty) User array onto the screen and everything is null. Here's the relevant part of the code and the deugging output. I'll attach my modified User.php and an anonymised version of the hook as attachments. 1. Bits of the hook: function fnUserAuthTGST($user, &$result) { $fname = "D3UserAuthTGST::fnUserAuthTGST"; $SSO = "https://xxxxxx.com/login/sso/SSOService?app=d3wiki&returnURL="; $errpage = "http://d3wiki.nott.ime.xxxxx.com/d3wikiaccess.php"; $allowed = 300; # Five minutes diff is allowed. logTGST("Page: " . $_SERVER['REQUEST_URI']); .... // This returns the user object if it exists: $u = User::newFromName($userName); $uid = $u->getId(); logTGST("After newFromName, uid is $uid"); if ( $uid == 0 ) { ..... $user = $u; $myid = $user->getId(); // Trap these for debugging purposes ... $myfrom = $user->mFrom; logTGST("About to exit hook with user->mId = $myid and user->mFrom = $myfrom"); return true; // To continue loading the user... } // fnUserAuthTGST 2. Hacked section of User.php: private function loadFromSession() { global $wgMemc, $wgCookiePrefix; $result = null; wfRunHooks( 'UserLoadFromSession', array( $this, &$result ) ); wfDebugLog('TGST', ' TGST: loadFromSession result is ' . $result . "\n"); wfDebugLog('TGST', " TGST: user id in \$this is $this->mId and name is $this->mName\n"); if ( $result !== null ) { 3. Output from logTGST and wfDebugLog (user->mFrom setting is useless and will be removed) : 2009-02-09 14:17:04 Cookie exploded: 8009449, Sam.Sexton@xxxx.com, Sam, Sexton 2009-02-09 14:17:04 After newFromName, uid is 3 2009-02-09 14:17:04 DB entry found for employee id nnnnnn with user id 3 and name Sam.Sexton 2009-02-09 14:17:04 About to exit hook with user->mId = 3 and user->mFrom = name 2009-02-09 14:17:04 wikidbD3-d3_: TGST: loadFromSession result is 2009-02-09 14:17:04 wikidbD3-d3_: TGST: user id in $this is and name is I really can't see what I'm doing wrong - can anyone else? Or is it a real bug?
Created attachment 5792 [details] UserLoadFromSession hook code
Created attachment 5793 [details] Slightly modified User.php from 1.13.0 I've added initialisation of an extra field, which isn't really relevant to this problem, but also a couple of lines in loadFromSession to display stuff after returning from the hook.
I thought I'd try loading the user within the hook and setting $result so that loadFromSession didn't continue and do that work itself, but (not at all surprisingly), as $this was empty, I was presented with an anonymous user's version of the main page. This problem with passing $user back to User (whether it's my fault or a bug) is really the nub of the matter, so I'm resetting the Severity to blocker, as I had hoped to have this facility finished some weeks ago! ;-)
This is not a support forum. Please use Bugzilla only for reporting bugs and requesting features in MediaWiki. See <http://www.mediawiki.org/wiki/Communication>.
Tim, I have tried the MW Users Forum and Project:Support recently and had no response whatsoever and am getting rather desperate to solve this problem - and trying to maintain my sanity! ;-) I've now got clear and succinct evidence of $user as amended in the hook not getting back to the calling code ( User::loadFromSession() ). This may indeed be my fault, but I've read various documentation several times (at one point I thought perhaps the wgHooks definition may have been wrong, but I've verified that it's not (included below) and I've also shadowed as closely as possible the way that CASAuthenticate works - so I'm not convinced that this isn't a bug. global $wgHooks; $wgHooks['UserLoadFromSession'][] = 'fnUserAuthTGST'; I would be grateful if someone could assure me that it isn't a bug, but I've now also raised this in the Project:Support desk as http://www.mediawiki.org/wiki/Project:Support_desk/Sections/Extension#Problem_passing_.24user_back_from_UserLoadFromSession_hook . It's arguable whether it should be reported as an extension (although that's how I've implemented it), but I suspect that the people reading that section are more likely to have experience in this area. Regards, Sam
Hmm - I've just realised that the updates to $user are not intended to be passed back, as the hook is called with a copy of the array rather than a reference to it. Sigh - I'm now rather perplexed, but at least this confusion is out of the way.