Skip to content

Commit

Permalink
Inject CentralAuth services again (#81)
Browse files Browse the repository at this point in the history
* Reverts #72 but uses optional_services instead to prevent fataling on
non-CentralAuth wikis
  • Loading branch information
Universal-Omega committed Mar 3, 2024
1 parent b6ce2ff commit 09855af
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
7 changes: 6 additions & 1 deletion extension.json
Expand Up @@ -8,7 +8,7 @@
"license-name": "GPL-3.0-or-later",
"type": "specialpage",
"requires": {
"MediaWiki": ">= 1.38.0"
"MediaWiki": ">= 1.41.0"
},
"MessagesDirs": {
"RemovePII": [
Expand Down Expand Up @@ -57,6 +57,11 @@
"HttpRequestFactory",
"JobQueueGroupFactory",
"UserFactory"
],
"optional_services": [
"CentralAuth.CentralAuthAntiSpoofManager",
"CentralAuth.CentralAuthDatabaseManager",
"CentralAuth.GlobalRenameUserValidator"
]
}
},
Expand Down
68 changes: 38 additions & 30 deletions includes/SpecialRemovePII.php
Expand Up @@ -9,18 +9,35 @@
use FormSpecialPage;
use Html;
use ManualLogEntry;
use MediaWiki\Extension\CentralAuth\CentralAuthDatabaseManager;
use MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUser;
use MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUserDatabaseUpdates;
use MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUserStatus;
use MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUserValidator;
use MediaWiki\Extension\CentralAuth\User\CentralAuthAntiSpoofManager;
use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
use MediaWiki\Extension\CentralAuth\Widget\HTMLGlobalUserTextField;
use MediaWiki\Http\HttpRequestFactory;
use MediaWiki\JobQueue\JobQueueGroupFactory;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserFactory;
use SpecialPage;
use Status;
use WikiMap;

class SpecialRemovePII extends FormSpecialPage {

/** @var CentralAuthAntiSpoofManager|null */
private $centralAuthAntiSpoofManager;

/** @var CentralAuthDatabaseManager|null */
private $centralAuthDatabaseManager;

/** @var Config */
private $config;

/** @var GlobalRenameUserValidator|null */
private $globalRenameUserValidator;

/** @var HttpRequestFactory */
private $httpRequestFactory;

Expand All @@ -35,15 +52,23 @@ class SpecialRemovePII extends FormSpecialPage {
* @param HttpRequestFactory $httpRequestFactory
* @param JobQueueGroupFactory $jobQueueGroupFactory
* @param UserFactory $userFactory
* @param ?CentralAuthAntiSpoofManager $centralAuthAntiSpoofManager
* @param ?CentralAuthDatabaseManager $centralAuthDatabaseManager
* @param ?GlobalRenameUserValidator $globalRenameUserValidator
*/
public function __construct(
ConfigFactory $configFactory,
HttpRequestFactory $httpRequestFactory,
JobQueueGroupFactory $jobQueueGroupFactory,
UserFactory $userFactory
UserFactory $userFactory,
?CentralAuthAntiSpoofManager $centralAuthAntiSpoofManager,
?CentralAuthDatabaseManager $centralAuthDatabaseManager,
?GlobalRenameUserValidator $globalRenameUserValidator
) {
parent::__construct( 'RemovePII', 'handle-pii' );

$this->centralAuthAntiSpoofManager = $centralAuthAntiSpoofManager;
$this->centralAuthDatabaseManager = $centralAuthDatabaseManager;
$this->config = $configFactory->makeConfig( 'RemovePII' );
$this->httpRequestFactory = $httpRequestFactory;
$this->jobQueueGroupFactory = $jobQueueGroupFactory;
Expand Down Expand Up @@ -87,7 +112,7 @@ protected function getFormFields() {
];

$formDescriptor['oldname'] = [
'class' => \MediaWiki\Extension\CentralAuth\Widget\HTMLGlobalUserTextField::class,
'class' => HTMLGlobalUserTextField::class,
'required' => true,
'label-message' => 'removepii-oldname-label',
];
Expand Down Expand Up @@ -158,18 +183,12 @@ public function validateCentralAuth( array $formData ) {
return Status::newFatal( 'removepii-centralauth-notinstalled' );
}

if ( version_compare( MW_VERSION, '1.40', '<' ) &&
!ExtensionRegistry::getInstance()->isLoaded( 'Renameuser' )
) {
return Status::newFatal( 'centralauth-rename-notinstalled' );
}

$oldUser = $this->userFactory->newFromName( $formData['oldname'] );
if ( !$oldUser ) {
return Status::newFatal( 'centralauth-rename-doesnotexist' );
}

$oldCentral = \MediaWiki\Extension\CentralAuth\User\CentralAuthUser::getInstanceByName( $formData['oldname'] );
$oldCentral = CentralAuthUser::getInstanceByName( $formData['oldname'] );
$canSuppress = $this->getUser() && $this->getUser()->isAllowed( 'centralauth-suppress' );

if ( ( $oldCentral->isSuppressed() || $oldCentral->isHidden() ) &&
Expand All @@ -187,10 +206,7 @@ public function validateCentralAuth( array $formData ) {
return Status::newFatal( 'centralauth-rename-badusername' );
}

$globalRenameUserValidator = MediaWikiServices::getInstance()->getService(
'CentralAuth.GlobalRenameUserValidator'
);
return $globalRenameUserValidator->validate( $oldUser, $newUser );
return $this->globalRenameUserValidator->validate( $oldUser, $newUser );
}

/**
Expand Down Expand Up @@ -218,25 +234,17 @@ public function onSubmit( array $formData ) {
return Status::newFatal( 'unknown-error' );
}

$caDbManager = MediaWikiServices::getInstance()->getService(
'CentralAuth.CentralAuthDatabaseManager'
);

$caAntiSpoofManager = MediaWikiServices::getInstance()->getService(
'CentralAuth.CentralAuthAntiSpoofManager'
);

$globalRenameUser = new \MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUser(
$globalRenameUser = new GlobalRenameUser(
$this->getUser(),
$oldUser,
\MediaWiki\Extension\CentralAuth\User\CentralAuthUser::getInstance( $oldUser ),
CentralAuthUser::getInstance( $oldUser ),
$newUser,
\MediaWiki\Extension\CentralAuth\User\CentralAuthUser::getInstance( $newUser ),
new \MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUserStatus( $newUser->getName() ),
CentralAuthUser::getInstance( $newUser ),
new GlobalRenameUserStatus( $newUser->getName() ),
$this->jobQueueGroupFactory,
new \MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUserDatabaseUpdates( $caDbManager ),
new GlobalRenameUserDatabaseUpdates( $this->centralAuthDatabaseManager ),
new RemovePIIGlobalRenameUserLogger( $this->getUser() ),
$caAntiSpoofManager
$this->centralAuthAntiSpoofManager
);

$globalRenameUser->rename(
Expand All @@ -262,9 +270,9 @@ public function onSubmit( array $formData ) {
'newname' => $newName,
];

$oldCentral = \MediaWiki\Extension\CentralAuth\User\CentralAuthUser::getInstanceByName( $oldName );
$oldCentral = CentralAuthUser::getInstanceByName( $oldName );

$newCentral = \MediaWiki\Extension\CentralAuth\User\CentralAuthUser::getInstanceByName( $newName );
$newCentral = CentralAuthUser::getInstanceByName( $newName );

if ( $oldCentral->renameInProgress() ) {
$out->addHTML(
Expand Down

0 comments on commit 09855af

Please sign in to comment.