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