Closed lukecarr closed 3 years ago
Thanks @lukecarr - I've got a site using oauth that is about to use this plugin pretty soon - feel free to submit a PR otherwise I'm likely to need to look at this at some point soon.
@danmarsden I'm not too well versed in Moodle plugin development, so I wouldn't know where to start!
Do you think you'd be able to have a fix ready by October 9th? I've got a Moodle site currently succumbing to this error that I'd like to push to production on that date.
Looking at this now. It appears that the issue might be to do with format_string()
requiring a context that it is not able to pick up automatically when an event is triggered during OAuth2 negotiation.
Unfortunately I'm not able to replicate this error on the 3.9 site I have available to me (Version 3.9.2+ (Build: 20200929) (2020061502.03))
I think we could maybe fix this by explicitly passing the system context through to the format_string()
calls that are initiated by the event observer. E.g. in the user_profile_hook()
function, changing the line:
$cohortslist[$cohort->id] = format_string($cohort->name);;
to:
$cohortslist[$cohort->id] = format_string($cohort->name, true, array('context' => $context));
This should pick up the $context = context_system::instance();
earlier in the function.
@lukecarr, are you in a position to make this change locally, and see if it helps on your site?
Thanks for the reply @DrCuriosity. Unfortunately, after implementing your proposed fix locally, the issue still persists. The error message hasn't changed at all. I can still click 'Continue' to log in successfully, the same as before.
Curses. Same stack trace and everything?
Unfortunately I still haven't been able to replicate the issue locally. I'll see if I can find another approach to try on (UTC+13) Monday.
No luck, I'm afraid. Even testing the event trigger via a CLI script, it seems to work fine here.
The only thing I can think of is that somewhere along the line, the format_string()
function is trying to do something else that relies on an implicit context being set outside of the explicit system context we're giving it, when the event is triggered via OAuth2. If this is the case, it might be best to try the "dumb" context-free fallback option of using strip_tags()
, as per this snippet from weblib.php
:
if (!$options['context']) {
// We did not find any context? weird.
return $string = strip_tags($string);
}
i.e.:
$cohortslist[$cohort->id] = strip_tags($cohort->name);
It's not an elegant fix but it might be worth trying out, if only to see if the problem goes away or not.
Just chiming in to mention that I believe this issue was actually a result of me having set Moodle's message debugging level to DEVELOPER
. Turning that off, I no longer had the errors, and sign in functionality worked as intended, leading me to believe that the "errors" I had were mere warnings.
After installing this plugin, I'm getting an error when I try to login with an OAuth2 account. I'm not sure if this plugin is causing the error, but it is referenced in the error output:
I get this error message immediately after logging in, however, I can click 'Continue' and access the site normally.
The URL of the page this error appears on is:
/auth/oauth2/login.php?wantsurl=...&sesskey=...&id=...&oauth2code=...
(I've removed the actual values of the query string)