Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.87% |
26 / 31 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CentralAuthForcedLocalCreationService | |
83.87% |
26 / 31 |
|
0.00% |
0 / 2 |
11.51 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
attemptAutoCreateLocalUserFromName | |
89.66% |
26 / 29 |
|
0.00% |
0 / 1 |
10.11 |
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 ManualLogEntry; |
25 | use MediaWiki\Deferred\SiteStatsUpdate; |
26 | use MediaWiki\Extension\CentralAuth\CentralAuthUtilityService; |
27 | use MediaWiki\Permissions\Authority; |
28 | use MediaWiki\Status\Status; |
29 | use MediaWiki\User\UserFactory; |
30 | |
31 | /** |
32 | * Service for forcing a local account to be created. |
33 | * |
34 | * @since 1.36 |
35 | * @author Taavi "Majavah" Väänänen |
36 | */ |
37 | class CentralAuthForcedLocalCreationService { |
38 | |
39 | private UserFactory $userFactory; |
40 | private CentralAuthUtilityService $utilityService; |
41 | |
42 | public function __construct( |
43 | UserFactory $userFactory, |
44 | CentralAuthUtilityService $utilityService |
45 | ) { |
46 | $this->userFactory = $userFactory; |
47 | $this->utilityService = $utilityService; |
48 | } |
49 | |
50 | /** |
51 | * Attempt to create a local user for the specified username. |
52 | * @param string $username |
53 | * @param Authority|null $performer |
54 | * @param string|null $reason |
55 | * @return Status |
56 | */ |
57 | public function attemptAutoCreateLocalUserFromName( |
58 | string $username, |
59 | ?Authority $performer = null, |
60 | $reason = null |
61 | ): Status { |
62 | $user = $this->userFactory->newFromName( $username ); |
63 | |
64 | if ( !$user ) { |
65 | // invalid username |
66 | return Status::newFatal( 'centralauth-createlocal-no-global-account' ); |
67 | } |
68 | |
69 | if ( $user->getId() ) { |
70 | return Status::newFatal( 'centralauth-createlocal-already-exists' ); |
71 | } |
72 | |
73 | $centralUser = CentralAuthUser::getInstance( $user ); |
74 | |
75 | if ( !$centralUser->exists() ) { |
76 | return Status::newFatal( 'centralauth-createlocal-no-global-account' ); |
77 | } |
78 | |
79 | if ( $centralUser->isSuppressed() ) { |
80 | $canSuppress = $performer && $performer->isAllowed( 'centralauth-suppress' ); |
81 | |
82 | return Status::newFatal( $canSuppress |
83 | ? 'centralauth-createlocal-suppressed' |
84 | : 'centralauth-createlocal-no-global-account' ); |
85 | } |
86 | |
87 | $status = $this->utilityService->autoCreateUser( $user, false, $performer ); |
88 | if ( !$status->isGood() ) { |
89 | return Status::wrap( $status ); |
90 | } |
91 | |
92 | // Add log entry |
93 | if ( $performer ) { |
94 | $logEntry = new ManualLogEntry( 'newusers', 'forcecreatelocal' ); |
95 | $logEntry->setPerformer( $performer->getUser() ); |
96 | $logEntry->setTarget( $user->getUserPage() ); |
97 | if ( $reason !== null ) { |
98 | $logEntry->setComment( $reason ); |
99 | } |
100 | $logEntry->setParameters( [ |
101 | '4::userid' => $user->getId(), |
102 | ] ); |
103 | |
104 | $logId = $logEntry->insert(); |
105 | $logEntry->publish( $logId ); |
106 | } |
107 | |
108 | // Update user count |
109 | SiteStatsUpdate::factory( [ 'users' => 1 ] )->doUpdate(); |
110 | |
111 | return Status::newGood(); |
112 | } |
113 | } |