ApiQueryOATH: do not use module to check enablement

Bug: T242031
Change-Id: Icafde71f6e58b24e8917b42a28b8f398aa28df20
This commit is contained in:
Taavi Väänänen 2023-11-23 20:58:29 +02:00
parent 1df74c2e2b
commit 809576b671
No known key found for this signature in database
GPG key ID: EF242F709F912FBE
2 changed files with 18 additions and 7 deletions

View file

@ -171,7 +171,12 @@
"oathvalidate": "\\MediaWiki\\Extension\\OATHAuth\\Api\\Module\\ApiOATHValidate"
},
"APIMetaModules": {
"oath": "\\MediaWiki\\Extension\\OATHAuth\\Api\\Module\\ApiQueryOATH"
"oath": {
"class": "\\MediaWiki\\Extension\\OATHAuth\\Api\\Module\\ApiQueryOATH",
"services": [
"OATHUserRepository"
]
}
},
"RateLimits": {
"badoath": {

View file

@ -22,6 +22,7 @@ use ApiQuery;
use ApiQueryBase;
use ApiResult;
use ManualLogEntry;
use MediaWiki\Extension\OATHAuth\OATHUserRepository;
use MediaWiki\MediaWikiServices;
use Wikimedia\ParamValidator\ParamValidator;
@ -36,12 +37,20 @@ use Wikimedia\ParamValidator\ParamValidator;
* @ingroup Extensions
*/
class ApiQueryOATH extends ApiQueryBase {
private OATHUserRepository $oathUserRepository;
/**
* @param ApiQuery $query
* @param string $moduleName
* @param OATHUserRepository $oathUserRepository
*/
public function __construct( $query, $moduleName ) {
public function __construct(
$query,
$moduleName,
OATHUserRepository $oathUserRepository
) {
parent::__construct( $query, $moduleName, 'oath' );
$this->oathUserRepository = $oathUserRepository;
}
public function execute() {
@ -77,11 +86,8 @@ class ApiQueryOATH extends ApiQueryBase {
];
if ( !$user->isAnon() ) {
$userRepo = MediaWikiServices::getInstance()->getService( 'OATHUserRepository' );
$authUser = $userRepo->findByUser( $user );
$data['enabled'] = $authUser &&
$authUser->getModule() !== null &&
$authUser->getModule()->isEnabled( $authUser );
$authUser = $this->oathUserRepository->findByUser( $user );
$data['enabled'] = $authUser && $authUser->isTwoFactorAuthEnabled();
// Log if the user doesn't have oathauth-api-all or if a reason is provided
if ( !$hasOAthauthApiAll || $reasonProvided ) {