Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 51 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CentralAuthCreateLocalAccountJob | |
0.00% |
0 / 51 |
|
0.00% |
0 / 2 |
56 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
run | |
0.00% |
0 / 50 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | /** |
3 | * @section LICENSE |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License along |
15 | * with this program; if not, write to the Free Software Foundation, Inc., |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * http://www.gnu.org/copyleft/gpl.html |
18 | * |
19 | * @file |
20 | */ |
21 | |
22 | namespace MediaWiki\Extension\CentralAuth\User; |
23 | |
24 | use Exception; |
25 | use Job; |
26 | use MediaWiki\Context\RequestContext; |
27 | use MediaWiki\Extension\CentralAuth\CentralAuthServices; |
28 | use MediaWiki\Logger\LoggerFactory; |
29 | use MediaWiki\User\User; |
30 | use MediaWiki\WikiMap\WikiMap; |
31 | use Wikimedia\ScopedCallback; |
32 | |
33 | /** |
34 | * Creates a local account and connects it to the global account. |
35 | * Used to ensure that all users have an attached local account on certain wikis which have some |
36 | * special "central" role (such as $wgMWOAuthCentralWiki for the OAuth extension). |
37 | * @see $wgCentralAuthAutoCreateWikis |
38 | */ |
39 | class CentralAuthCreateLocalAccountJob extends Job { |
40 | /** |
41 | * @param array $params name => user name, from => wiki where the job is created, |
42 | * [session] => session data from RequestContext::exportSession() |
43 | */ |
44 | public function __construct( $params ) { |
45 | parent::__construct( 'CentralAuthCreateLocalAccountJob', $params ); |
46 | } |
47 | |
48 | /** |
49 | * Try to create and attach the user. |
50 | * @throws Exception |
51 | * @return bool Success |
52 | */ |
53 | public function run() { |
54 | $username = $this->params['name']; |
55 | $from = $this->params['from']; |
56 | $wiki = WikiMap::getCurrentWikiId(); |
57 | |
58 | if ( isset( $this->params['session'] ) ) { |
59 | // restore IP and other request data |
60 | $this->params['session']['userId'] = 0; |
61 | $this->params['session']['sessionId'] = ''; |
62 | $callback = RequestContext::importScopedSession( $this->params['session'] ); |
63 | $this->addTeardownCallback( static function () use ( &$callback ) { |
64 | ScopedCallback::consume( $callback ); |
65 | } ); |
66 | } |
67 | |
68 | $user = User::newFromName( $username ); |
69 | $centralUser = CentralAuthUser::getInstance( $user ); |
70 | $logger = LoggerFactory::getInstance( 'CentralAuth' ); |
71 | |
72 | if ( $user->getId() !== 0 ) { |
73 | $logger->info( |
74 | __CLASS__ . ': tried to create local account for {username} ' |
75 | . 'on {wiki} from {from} but one already exists', |
76 | [ |
77 | 'username' => $username, |
78 | 'wiki' => $wiki, |
79 | 'from' => $from, |
80 | ] |
81 | ); |
82 | return true; |
83 | } elseif ( !$centralUser->exists() ) { |
84 | $logger->info( |
85 | __CLASS__ . ': tried to create local account for {username} ' |
86 | . 'on {wiki} from {from} but no global account exists', |
87 | [ |
88 | 'username' => $username, |
89 | 'wiki' => $wiki, |
90 | 'from' => $from, |
91 | ] |
92 | ); |
93 | return true; |
94 | } elseif ( $centralUser->attachedOn( $wiki ) ) { |
95 | $logger->info( |
96 | __CLASS__ . ': tried to create local account for {username} ' |
97 | . 'on {wiki} from {from} but an attached local account already exists', |
98 | [ |
99 | 'username' => $username, |
100 | 'wiki' => $wiki, |
101 | 'from' => $from, |
102 | ] |
103 | ); |
104 | return true; |
105 | } |
106 | |
107 | $success = CentralAuthServices::getUtilityService()->autoCreateUser( $user )->isGood(); |
108 | if ( $success ) { |
109 | $centralUser->invalidateCache(); |
110 | } |
111 | |
112 | return true; |
113 | } |
114 | } |