Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiCreateLocalAccount
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 7
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 mustBePosted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\CentralAuth\Api;
4
5use MediaWiki\Api\ApiBase;
6use MediaWiki\Api\ApiMain;
7use MediaWiki\Extension\CentralAuth\User\CentralAuthForcedLocalCreationService;
8use Wikimedia\ParamValidator\ParamValidator;
9
10/**
11 * API module that can be used to manually create a local account for a global account.
12 *
13 * @author Taavi "Majavah" Väänänen
14 * @since 1.36
15 */
16class ApiCreateLocalAccount extends ApiBase {
17
18    private CentralAuthForcedLocalCreationService $forcedLocalCreationService;
19
20    public function __construct(
21        ApiMain $mainModule,
22        string $moduleName,
23        CentralAuthForcedLocalCreationService $forcedLocalCreationService
24    ) {
25        parent::__construct( $mainModule, $moduleName );
26        $this->forcedLocalCreationService = $forcedLocalCreationService;
27    }
28
29    public function execute() {
30        $this->checkUserRightsAny( 'centralauth-createlocal' );
31
32        $params = $this->extractRequestParams();
33
34        $username = $params['username'];
35        $reason = $params['reason'];
36
37        $status = $this->forcedLocalCreationService
38            ->attemptAutoCreateLocalUserFromName( $username, $this->getUser(), $reason );
39
40        if ( $status->isGood() ) {
41            $this->getResult()->addValue( null, $this->getModuleName(), [
42                'username' => $username,
43                'reason' => $reason
44            ] );
45        } else {
46            $error = $this->getErrorFormatter()->arrayFromStatus( $status );
47            $this->getResult()->addValue( 'error', null, $error );
48        }
49    }
50
51    /** @inheritDoc */
52    public function getAllowedParams() {
53        return [
54            'username' => [
55                ParamValidator::PARAM_TYPE => 'string',
56                ParamValidator::PARAM_REQUIRED => true
57            ],
58            'reason' => [
59                ParamValidator::PARAM_TYPE => 'string',
60            ],
61        ];
62    }
63
64    /** @inheritDoc */
65    protected function getExamplesMessages() {
66        return [
67            'action=createlocalaccount&username=Example&reason=Because+I+can' => 'apihelp-createlocalaccount-example-1',
68        ];
69    }
70
71    /** @inheritDoc */
72    public function mustBePosted() {
73        return true;
74    }
75
76    /** @inheritDoc */
77    public function isWriteMode() {
78        return true;
79    }
80
81    /** @inheritDoc */
82    public function needsToken() {
83        return 'csrf';
84    }
85}