| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
namespace MediaWiki\Extension\OATHAuth\Api\Module; |
| 20 |
|
| 21 |
use ManualLogEntry; |
| 22 |
use MediaWiki\Api\ApiQuery; |
| 23 |
use MediaWiki\Api\ApiQueryBase; |
| 24 |
use MediaWiki\Api\ApiResult; |
| 25 |
use MediaWiki\Extension\OATHAuth\OATHUserRepository; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use Wikimedia\ParamValidator\ParamValidator; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
class ApiQueryOATH extends ApiQueryBase { |
| 40 |
private OATHUserRepository $oathUserRepository; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function __construct( |
| 48 |
$query, |
| 49 |
$moduleName, |
| 50 |
OATHUserRepository $oathUserRepository |
| 51 |
) { |
| 52 |
parent::__construct( $query, $moduleName, 'oath' ); |
| 53 |
$this->oathUserRepository = $oathUserRepository; |
| 54 |
} |
| 55 |
|
| 56 |
public function execute() { |
| 57 |
|
| 58 |
|
| 59 |
$this->checkUserRightsAny( [ 'oathauth-api-all', 'oathauth-verify-user' ] ); |
| 60 |
|
| 61 |
$params = $this->extractRequestParams(); |
| 62 |
|
| 63 |
$hasOAthauthApiAll = $this->getPermissionManager() |
| 64 |
->userHasRight( |
| 65 |
$this->getUser(), |
| 66 |
'oathauth-api-all' |
| 67 |
); |
| 68 |
|
| 69 |
$reasonProvided = $params['reason'] !== null && $params['reason'] !== ''; |
| 70 |
if ( !$hasOAthauthApiAll && !$reasonProvided ) { |
| 71 |
$this->dieWithError( [ 'apierror-missingparam', 'reason' ] ); |
| 72 |
} |
| 73 |
|
| 74 |
if ( $params['user'] === null ) { |
| 75 |
$user = $this->getUser(); |
| 76 |
} else { |
| 77 |
$user = MediaWikiServices::getInstance()->getUserFactory() |
| 78 |
->newFromName( $params['user'] ); |
| 79 |
if ( $user === null ) { |
| 80 |
$this->dieWithError( 'noname' ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$result = $this->getResult(); |
| 85 |
$data = [ |
| 86 |
ApiResult::META_BC_BOOLS => [ 'enabled' ], |
| 87 |
'enabled' => false, |
| 88 |
]; |
| 89 |
|
| 90 |
if ( $user->isNamed() ) { |
| 91 |
$authUser = $this->oathUserRepository->findByUser( $user ); |
| 92 |
$data['enabled'] = $authUser && $authUser->isTwoFactorAuthEnabled(); |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
if ( !$hasOAthauthApiAll || $reasonProvided ) { |
| 97 |
$logEntry = new ManualLogEntry( 'oath', 'verify' ); |
| 98 |
$logEntry->setPerformer( $this->getUser() ); |
| 99 |
$logEntry->setTarget( $user->getUserPage() ); |
| 100 |
$logEntry->setComment( $params['reason'] ); |
| 101 |
$logEntry->insert(); |
| 102 |
} |
| 103 |
} |
| 104 |
$result->addValue( 'query', $this->getModuleName(), $data ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function getCacheMode( $params ) { |
| 113 |
return 'private'; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
public function isInternal() { |
| 118 |
return true; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function getAllowedParams() { |
| 125 |
return [ |
| 126 |
'user' => [ |
| 127 |
ParamValidator::PARAM_TYPE => 'user', |
| 128 |
], |
| 129 |
'reason' => [ |
| 130 |
ParamValidator::PARAM_TYPE => 'string', |
| 131 |
], |
| 132 |
]; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
protected function getExamplesMessages() { |
| 139 |
return [ |
| 140 |
'action=query&meta=oath' |
| 141 |
=> 'apihelp-query+oath-example-1', |
| 142 |
'action=query&meta=oath&oathuser=Example' |
| 143 |
=> 'apihelp-query+oath-example-2', |
| 144 |
]; |
| 145 |
} |
| 146 |
} |