Skip to content

Instantly share code, notes, and snippets.

@slootjes
Created February 7, 2019 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slootjes/30b1d552d736b0d833be63fdee568064 to your computer and use it in GitHub Desktop.
Save slootjes/30b1d552d736b0d833be63fdee568064 to your computer and use it in GitHub Desktop.
MediaWiki GoogleLogin extension to automatically create new users
// in getClient() replace:
$client->addScope( 'email' );
// with:
$client->addScope( ['email', 'profile']);
// in getUserFromToken() replace the last occurence of:
return null;
// with the following code snippet:
// make sure we have a valid name for this user
$name = $token['name'];
$tries = 0;
verify_name:
$name = User::getCanonicalName($name, 'creatable');
if ($name === false) {
// for some reason the name is considered to be invalid
switch ($tries) {
// create a safe name instead by the users email address
// should be OK since we're using a "firstname.lastname@domain.tld" format
case 0:
$name = trim(ucfirst(str_replace('.', ' ', explode('@', $token['email'])[0])));
break;
case 1:
// it could be that the user was removed from the Google domain
// but is added as a new user, then the name will collide with the previous account
$name = $token['name'].' '.date('Y-m-d');
break;
case 2:
// I give up...we just use someones Google ID then instead
$name = $token['sub'];
break;
default:
die('Impossible to create a username for you...please contact your local wiki administrator');
}
$tries++;
goto verify_name;
}
// create new user if it doesn't exist
$user = User::createNew($name, [
'real_name' => $token['name'],
'email' => $token['email'],
'email_authenticated' => wfTimestamp(TS_MW),
]);
// store the google id and user id connection
$this->match($user, $token);
return $user;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment