简体   繁体   中英

How to authorize user in MediaWiki 1.19 without password?

In my plugin I have only username or email and I have to authorize that user. I found next issue, but it didn't work for me:

    class ApiPlugin extends ApiBase {
        public function execute() {
            $params = $this->extractRequestParams();

            switch ( $params['do'] ) {
                case 'login':
                    // Registering. Works fine.
                    $user = User::newFromName( 'admin' );
                    $user->setEmail( admin@email.com );
                    $user->setRealName( 'admin' );
                    $uid = $user->idForName();

                    if ( $uid === 0 ) {
                        $user->addToDatabase();
                        $user->setPassword( generate_password() );
                        $user->saveSettings();
                    }
                    $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
                    $ssu->doUpdate();

                    if ($user->isLoggedIn()) $user->doLogout();

                    //Logging in.
                    $id = User::idFromName('admin');
                    $user->setID($id);
                    $user->loadFromId();

                    $user->setToken();
                    $user->saveSettings();

                    wfSetupSession();
                    $user->setCookies();
                    break;
            }

        }
    }

Also, another issue was getting password hash straight from DB, but it is barbarism...
Thanks in advance!

After few days I tried to separate code and... profit!

class ApiPlugin extends ApiBase {
    public function execute() {
        $params = $this->extractRequestParams();

        switch ( $params['do'] ) {
            case 'register':
                // Registering. Works fine.
                $user = User::newFromName( 'admin' );
                $user->setEmail( admin@email.com );
                $user->setRealName( 'admin' );
                $uid = $user->idForName();

                if ( $uid === 0 ) {
                    $user->addToDatabase();
                    $user->setPassword( generate_password() );
                    $user->saveSettings();
                }
                $ssu = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
                $ssu->doUpdate();
            case 'authorise':
                if ($user->isLoggedIn()) $user->doLogout();
                wfSetupSession();
                //Logging in.
                $id = User::idFromName('admin');
                $user->setID($id);
                $user->loadFromId();

                $user->setToken();
                $user->saveSettings();

                $user->setCookies();
                break;
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM