落雨楓 2 years ago
commit ec85da6f9a

@ -30,7 +30,7 @@
"GetPreferences": "Isekai\\OIDC\\IsekaiOIDCAuthHooks::onGetPreferences", "GetPreferences": "Isekai\\OIDC\\IsekaiOIDCAuthHooks::onGetPreferences",
"SkinTemplateNavigation::Universal": "Isekai\\OIDC\\IsekaiOIDCAuthHooks::onSkinTemplateUniversalNavigation", "SkinTemplateNavigation::Universal": "Isekai\\OIDC\\IsekaiOIDCAuthHooks::onSkinTemplateUniversalNavigation",
"ResourceLoaderGetConfigVars": "Isekai\\OIDC\\IsekaiOIDCAuthHooks::onResourceLoaderGetConfigVars", "ResourceLoaderGetConfigVars": "Isekai\\OIDC\\IsekaiOIDCAuthHooks::onResourceLoaderGetConfigVars",
"Isekai::GetUserAvatar": "Isekai\\OIDC\\IsekaiOIDCAuthHooks::onGetUserAvatar" "Isekai::GetUsersAvatar": "Isekai\\OIDC\\IsekaiOIDCAuthHooks::onGetUsersAvatar"
}, },
"ExtensionMessagesFiles": { "ExtensionMessagesFiles": {
"IsekaiOIDCAlias": "IsekaiOIDC.alias.php" "IsekaiOIDCAlias": "IsekaiOIDC.alias.php"

@ -38,12 +38,15 @@ class ApiOIDCAvatar extends ApiQueryBase {
$size = $this->getParameter('size'); $size = $this->getParameter('size');
$avatarUrl = ''; $avatars = [];
$hookContainer = $services->getHookContainer(); $hookContainer = $services->getHookContainer();
$hookContainer->run('Isekai::GetUserAvatar', [&$avatarUrl, $size, $user]); $hookContainer->run('Isekai::GetUsersAvatar', [&$avatars, [$user], $size]);
if (empty($avatarUrl)) { $avatarUrl = '';
if (isset($avatars[$user->getId()])) {
$avatarUrl = $avatars[$user->getId()];
} else {
if (isset($wgIsekaiOIDC['defaultAvatarUrl'])) { if (isset($wgIsekaiOIDC['defaultAvatarUrl'])) {
$avatarUrl = $wgIsekaiOIDC['defaultAvatarUrl']; $avatarUrl = $wgIsekaiOIDC['defaultAvatarUrl'];
} else { } else {

@ -454,6 +454,28 @@ class IsekaiOIDCAuth extends AbstractPrimaryAuthenticationProvider {
} }
} }
public static function findOidcSubjectsByUserIds( array $userIds ) {
$dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getMaintenanceConnectionRef( DB_REPLICA );
$rows = $dbr->select(
[
self::OIDC_TABLE
],
[
'oidc_user',
'oidc_subject'
],
[
'oidc_user' => $userIds
],
__METHOD__
);
$subjects = [];
foreach ( $rows as $row ) {
$subjects[$row->oidc_user] = $row->oidc_subject;
}
return $subjects;
}
private static function getPreferredUsername( $config, $oidc, $realname, $email ) { private static function getPreferredUsername( $config, $oidc, $realname, $email ) {
if ( isset( $config['preferred_username'] ) ) { if ( isset( $config['preferred_username'] ) ) {
wfDebugLog( self::LOG_TAG, 'Using ' . $config['preferred_username'] . wfDebugLog( self::LOG_TAG, 'Using ' . $config['preferred_username'] .
@ -555,18 +577,19 @@ class IsekaiOIDCAuth extends AbstractPrimaryAuthenticationProvider {
$preferred_username = 'User'; $preferred_username = 'User';
} }
if ( MediaWikiServices::getInstance() $userIdentityLookup = MediaWikiServices::getInstance()->getUserIdentityLookup();
->getUserIdentityLookup() $userIdentity = $userIdentityLookup->getUserIdentityByName( $preferred_username );
->getUserIdentityByName( $preferred_username ) ) { if ( !$userIdentity || !$userIdentity->isRegistered() ) {
return $preferred_username; return $preferred_username;
} }
$count = 1; $count = 1;
while ( MediaWikiServices::getInstance() while ( true ) {
->getUserIdentityLookup() $userIdentity = $userIdentityLookup->getUserIdentityByName( $preferred_username . $count );
->getUserIdentityByName( $preferred_username . $count ) ) { if ( !$userIdentity || !$userIdentity->isRegistered() ) {
break;
}
$count ++; $count ++;
} }
return $preferred_username . $count; return $preferred_username . $count;

@ -171,13 +171,34 @@ class IsekaiOIDCAuthHooks {
return true; return true;
} }
public static function onGetUserAvatar(&$avatarUrl, $size, $user) { /**
* Get avatar url for users
* @param array &$avatars
* @param \User[] $users
* @param int $size
*/
public static function onGetUsersAvatar(&$avatars, array $users, $size = 128) {
if (!is_array($avatars)) {
$avatars = [];
}
$services = MediaWikiServices::getInstance(); $services = MediaWikiServices::getInstance();
$config = $services->getMainConfig(); $config = $services->getMainConfig();
$wgIsekaiOIDC = $config->get('IsekaiOIDC'); $wgIsekaiOIDC = $config->get('IsekaiOIDC');
if (isset($wgIsekaiOIDC['avatarUrl'])) { if (isset($wgIsekaiOIDC['avatarUrl'])) {
list($subject, $accessToken, $refreshToken) = IsekaiOIDCAuth::findOidcDataByUserId($user->getId()); $userIds = array_map(function($user) {
return $user->getId();
}, $users);
$subjects = IsekaiOIDCAuth::findOidcSubjectsByUserIds($userIds);
foreach ($users as $user) {
$subject = null;
if (isset($subjects[$user->getId()])) {
$subject = $subjects[$user->getId()];
}
$avatarFinded = false; $avatarFinded = false;
if ($subject) { if ($subject) {
$avatarUrl = str_replace(['{openid}', '{username}'], [urlencode($subject), urlencode($user->getName())], $wgIsekaiOIDC['avatarUrl']); $avatarUrl = str_replace(['{openid}', '{username}'], [urlencode($subject), urlencode($user->getName())], $wgIsekaiOIDC['avatarUrl']);
@ -199,6 +220,9 @@ class IsekaiOIDCAuthHooks {
} else { } else {
$avatarUrl = "$avatarUrl?size=xxl"; $avatarUrl = "$avatarUrl?size=xxl";
} }
$avatars[$user->getId()] = $avatarUrl;
}
} }
} }
return true; return true;

Loading…
Cancel
Save