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 ApiBase;
6use 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    /** @var CentralAuthForcedLocalCreationService */
18    private $forcedLocalCreationService;
19
20    /**
21     * @param ApiMain $mainModule
22     * @param string $moduleName
23     * @param CentralAuthForcedLocalCreationService $forcedLocalCreationService
24     */
25    public function __construct(
26        ApiMain $mainModule,
27        $moduleName,
28        CentralAuthForcedLocalCreationService $forcedLocalCreationService
29    ) {
30        parent::__construct( $mainModule, $moduleName );
31        $this->forcedLocalCreationService = $forcedLocalCreationService;
32    }
33
34    public function execute() {
35        $this->checkUserRightsAny( 'centralauth-createlocal' );
36
37        $params = $this->extractRequestParams();
38
39        $username = $params['username'];
40        $reason = $params['reason'];
41
42        $status = $this->forcedLocalCreationService
43            ->attemptAutoCreateLocalUserFromName( $username, $this->getUser(), $reason );
44
45        if ( $status->isGood() ) {
46            $this->getResult()->addValue( null, $this->getModuleName(), [
47                'username' => $username,
48                'reason' => $reason
49            ] );
50        } else {
51            $error = $this->getErrorFormatter()->arrayFromStatus( $status );
52            $this->getResult()->addValue( 'error', null, $error );
53        }
54    }
55
56    /** @inheritDoc */
57    public function getAllowedParams() {
58        return [
59            'username' => [
60                ParamValidator::PARAM_TYPE => 'string',
61                ParamValidator::PARAM_REQUIRED => true
62            ],
63            'reason' => [
64                ParamValidator::PARAM_TYPE => 'string',
65            ],
66        ];
67    }
68
69    /** @inheritDoc */
70    protected function getExamplesMessages() {
71        return [
72            'action=createlocalaccount&username=Example&reason=Because+I+can' => 'apihelp-createlocalaccount-example-1',
73        ];
74    }
75
76    /** @inheritDoc */
77    public function mustBePosted() {
78        return true;
79    }
80
81    /** @inheritDoc */
82    public function isWriteMode() {
83        return true;
84    }
85
86    /** @inheritDoc */
87    public function needsToken() {
88        return 'csrf';
89    }
90}