Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
AttachLdapUser | |
0.00% |
0 / 28 |
|
0.00% |
0 / 2 |
12 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
6 |
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 | if ( getenv( 'MW_INSTALL_PATH' ) ) { |
23 | $IP = getenv( 'MW_INSTALL_PATH' ); |
24 | } else { |
25 | $IP = __DIR__ . '/../../..'; |
26 | } |
27 | require_once "$IP/maintenance/Maintenance.php"; |
28 | |
29 | use MediaWiki\Context\RequestContext; |
30 | use MediaWiki\Maintenance\Maintenance; |
31 | use MediaWiki\MediaWikiServices; |
32 | use MediaWiki\Permissions\UltimateAuthority; |
33 | use MediaWiki\User\User; |
34 | |
35 | /** |
36 | * Attach an existing LDAP account to the local wiki with all expected log |
37 | * events created as well. |
38 | * |
39 | * Originally developed as a component of the MediaWiki OpenStackManager |
40 | * extension. |
41 | * |
42 | * @copyright © 2018 Wikimedia Foundation and contributors |
43 | */ |
44 | class AttachLdapUser extends Maintenance { |
45 | public function __construct() { |
46 | parent::__construct(); |
47 | $this->addDescription( "Attach an existing LDAP user to the local wiki" ); |
48 | $this->addOption( 'user', 'Username', true, true ); |
49 | $this->addOption( 'email', 'Email address', true, true ); |
50 | $this->addOption( 'domain', 'LDAP domain', false, true ); |
51 | $this->requireExtension( 'LDAP Authentication Plugin' ); |
52 | } |
53 | |
54 | public function execute() { |
55 | // Setup the internal state of LdapAuthenticationPlugin as though the |
56 | // login form was used. Ugly but this is what LdapAuthentication |
57 | // forces us to do. |
58 | $ldap = LdapAuthenticationPlugin::getInstance(); |
59 | $ldap->LDAPUsername = $this->getOption( 'user' ); |
60 | $ldap->email = $this->getOption( 'email' ); |
61 | $domain = $this->getOption( 'domain', $ldap->getDomain() ); |
62 | $ldap->setDomain( $domain ); |
63 | $_SESSION['wsDomain'] = $domain; |
64 | |
65 | $user = User::newFromName( $ldap->LDAPUsername, 'creatable' ); |
66 | $authManager = MediaWikiServices::getInstance()->getAuthManager(); |
67 | $status = $authManager->autoCreateUser( |
68 | $user, |
69 | LdapPrimaryAuthenticationProvider::class, |
70 | /* $login */ false, |
71 | /* log */ true, |
72 | new UltimateAuthority( User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ) ) |
73 | ); |
74 | |
75 | if ( !$status->isGood() ) { |
76 | $this->fatalError( |
77 | MediaWikiServices::getInstance() |
78 | ->getFormatterFactory() |
79 | ->getStatusFormatter( RequestContext::getMain() ) |
80 | ->getWikiText( $status ) |
81 | ); |
82 | } |
83 | } |
84 | } |
85 | |
86 | $maintClass = AttachLdapUser::class; |
87 | require_once RUN_MAINTENANCE_IF_MAIN; |